-
Notifications
You must be signed in to change notification settings - Fork 7
/
kind.go
198 lines (182 loc) · 4.66 KB
/
kind.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package pack
import (
"math/rand"
"reflect"
"strings"
"github.com/renproject/surge"
)
// A Kind is an optionally abstract type identifier. It can be thought of as the
// "type of a type", or the "constructor for a type". For example, a struct is
// an abstract type identifier, because it does not bind the respective value to
// have any specific fields (or types for those fields). Similarly, a list is
// an abstract type identifier, because it does not bind the respective value to
// a specific type of element.
type Kind uint8
const (
// KindNil should never be used.
KindNil = Kind(0)
// KindBool is the kind of all Bool values.
KindBool = Kind(1)
// KindU8 is the kind of all U8 values.
KindU8 = Kind(2)
// KindU16 is the kind of all U16 values.
KindU16 = Kind(3)
// KindU32 is the kind of all U32 values.
KindU32 = Kind(4)
// KindU64 is the kind of all U64 values.
KindU64 = Kind(5)
// KindU128 is the kind of all U128 values.
KindU128 = Kind(6)
// KindU256 is the kind of all U256 values.
KindU256 = Kind(7)
// KindString is the kind of all utf8 strings.
KindString = Kind(10)
// KindBytes is the kind of all dynamic byte arrays.
KindBytes = Kind(11)
// KindBytes32 is the kind of all 32-byte arrays.
KindBytes32 = Kind(12)
// KindBytes65 is the kind of all 65-byte arrays.
KindBytes65 = Kind(13)
// KindStruct is the kind of all struct values. It is abstract, because it does
// not specify the fields in the struct.
KindStruct = Kind(20)
// KindList is the kind of all list values. It is abstract, because it does
// not specify the type of the elements in the list.
KindList = Kind(21)
)
func (kind Kind) String() string {
switch kind {
// Scalar
case KindBool:
return "bool"
case KindU8:
return "u8"
case KindU16:
return "u16"
case KindU32:
return "u32"
case KindU64:
return "u64"
case KindU128:
return "u128"
case KindU256:
return "u256"
// Bytes
case KindString:
return "string"
case KindBytes:
return "bytes"
case KindBytes32:
return "bytes32"
case KindBytes65:
return "bytes65"
// Abstract
case KindStruct:
return "struct"
case KindList:
return "list"
default:
return "nil"
}
}
// SizeHint returns the number of bytes required to represent the kind in
// binary.
func (kind Kind) SizeHint() int {
return surge.SizeHintU8
}
// Marshal the kind into binary.
func (kind Kind) Marshal(buf []byte, rem int) ([]byte, int, error) {
return surge.MarshalU8(uint8(kind), buf, rem)
}
// Unmarshal the kind from binary.
func (kind *Kind) Unmarshal(buf []byte, rem int) ([]byte, int, error) {
return surge.UnmarshalU8((*uint8)(kind), buf, rem)
}
// MarshalText from the kind. Unrecognised kinds will be marshaled into the
// "nil" string.
func (kind Kind) MarshalText() ([]byte, error) {
return []byte(kind.String()), nil
}
// UnmarshalText into the kind. Unrecognised text will be unmarshaled into the
// KindNil, and will be considered invalid.
func (kind *Kind) UnmarshalText(text []byte) error {
switch strings.ToLower(string(text)) {
case KindBool.String():
*kind = KindBool
return nil
case KindU8.String():
*kind = KindU8
return nil
case KindU16.String():
*kind = KindU16
return nil
case KindU32.String():
*kind = KindU32
return nil
case KindU64.String():
*kind = KindU64
return nil
case KindU128.String():
*kind = KindU128
return nil
case KindU256.String():
*kind = KindU256
return nil
case KindString.String():
*kind = KindString
return nil
case KindBytes.String():
*kind = KindBytes
return nil
case KindBytes32.String():
*kind = KindBytes32
return nil
case KindBytes65.String():
*kind = KindBytes65
return nil
case KindStruct.String():
*kind = KindStruct
return nil
case KindList.String():
*kind = KindList
return nil
default:
*kind = KindNil
return nil
}
}
// Generate a random kind. This method is implemented for use in quick tests.
// See https://golang.org/pkg/testing/quick/#Generator for more information.
func (kind Kind) Generate(r *rand.Rand, size int) reflect.Value {
switch r.Int() % 12 {
case 0:
return reflect.ValueOf(KindBool)
case 1:
return reflect.ValueOf(KindU8)
case 2:
return reflect.ValueOf(KindU16)
case 3:
return reflect.ValueOf(KindU32)
case 4:
return reflect.ValueOf(KindU64)
case 5:
return reflect.ValueOf(KindU128)
case 6:
return reflect.ValueOf(KindU256)
case 7:
return reflect.ValueOf(KindString)
case 8:
return reflect.ValueOf(KindBytes)
case 9:
return reflect.ValueOf(KindBytes32)
case 10:
return reflect.ValueOf(KindBytes65)
case 11:
return reflect.ValueOf(KindStruct)
case 12:
return reflect.ValueOf(KindList)
default:
// It is intentional that this case never happens.
return reflect.ValueOf(KindNil)
}
}