-
Notifications
You must be signed in to change notification settings - Fork 79
/
type.go
90 lines (83 loc) · 1.74 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package stackitem
import "errors"
// ErrInvalidType is returned on attempts to deserialize some unknown item type.
var ErrInvalidType = errors.New("invalid type")
// Type represents type of the stack item.
type Type byte
// This block defines all known stack item types.
const (
AnyT Type = 0x00
PointerT Type = 0x10
BooleanT Type = 0x20
IntegerT Type = 0x21
ByteArrayT Type = 0x28
BufferT Type = 0x30
ArrayT Type = 0x40
StructT Type = 0x41
MapT Type = 0x48
InteropT Type = 0x60
InvalidT Type = 0xFF
)
// String implements fmt.Stringer interface.
func (t Type) String() string {
switch t {
case AnyT:
return "Any"
case PointerT:
return "Pointer"
case BooleanT:
return "Boolean"
case IntegerT:
return "Integer"
case ByteArrayT:
return "ByteString"
case BufferT:
return "Buffer"
case ArrayT:
return "Array"
case StructT:
return "Struct"
case MapT:
return "Map"
case InteropT:
return "Interop"
default:
return "INVALID"
}
}
// IsValid checks if s is a well defined stack item type.
func (t Type) IsValid() bool {
switch t {
case AnyT, PointerT, BooleanT, IntegerT, ByteArrayT, BufferT, ArrayT, StructT, MapT, InteropT:
return true
default:
return false
}
}
// FromString returns stackitem type from string.
func FromString(s string) (Type, error) {
switch s {
case "Any":
return AnyT, nil
case "Pointer":
return PointerT, nil
case "Boolean":
return BooleanT, nil
case "Integer":
return IntegerT, nil
case "ByteString":
return ByteArrayT, nil
case "Buffer":
return BufferT, nil
case "Array":
return ArrayT, nil
case "Struct":
return StructT, nil
case "Map":
return MapT, nil
case "Interop":
return InteropT, nil
default:
return 0xFF, ErrInvalidType
}
}