-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.go
458 lines (404 loc) · 7.81 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package degob
import (
"fmt"
"sort"
)
// WireType is basically the same as the `encoding/gob` wiretype with a few
// fields added and exported. It is also a `Stringer` so the types can be
// printed for better viewing.
type WireType struct {
ArrayT *ArrayType
SliceT *SliceType
StructT *StructType
MapT *MapType
GobEncoderT *GobEncoderType
BinaryMarshalerT *BinaryMarshalerType
TextMarshalerT *TextMarshalerType
}
type GobEncoderType struct {
CommonType
}
type BinaryMarshalerType struct {
CommonType
}
type TextMarshalerType struct {
CommonType
}
// ArrayType represents a fixed size array
type ArrayType struct {
CommonType
Elem typeId
ElemTypeString string
Len int
}
// CommonType has information common to all base types
type CommonType struct {
Name string
Id int
}
// SliceType represents a slice
type SliceType struct {
CommonType
Elem typeId
ElemTypeString string
}
type StructType struct {
CommonType
Field []*FieldType
}
// FieldType is the information for a struct field
type FieldType struct {
Name string
TypeString string
Id int
}
// MapType is the information for a map
type MapType struct {
CommonType
Key typeId
KeyTypeString string
Elem typeId
ElemTypeString string
}
const (
// builtin types
_bool_id typeId = 1
_int_id typeId = 2
_uint_id typeId = 3
_float_id typeId = 4
_bytes_id typeId = 5
_string_id typeId = 6
_complex_id typeId = 7
_interface_id typeId = 8
// reserved types
_reserved1_id typeId = 9
_reserved2_id typeId = 10
_reserved3_id typeId = 11
_reserved4_id typeId = 12
_reserved5_id typeId = 13
_reserved6_id typeId = 14
_reserved7_id typeId = 15
)
func (t typeId) String() string {
switch t {
case _bool_id:
return "1 (bool)"
case _int_id:
return "2 (int)"
case _uint_id:
return "3 (uint)"
case _float_id:
return "4 (float)"
case _bytes_id:
return "5 (bytes)"
case _string_id:
return "6 (string)"
case _complex_id:
return "7 (complex)"
case _interface_id:
return "8 (interface)"
case _reserved1_id:
return "9 (reserved)"
case _reserved2_id:
return "10 (reserved)"
case _reserved3_id:
return "11 (reserved)"
case _reserved4_id:
return "12 (reserved)"
case _reserved5_id:
return "13 (reserved)"
case _reserved6_id:
return "14 (reserved)"
case _reserved7_id:
return "15 (reserved)"
default:
return fmt.Sprintf("%d (user defined)", t)
}
}
// Value is a reprsentation of a Go value. You can test for equality and
// Display them stylized
type Value interface {
// Equal tests for equality to another value
Equal(Value) bool
// Display shows the value accoring to the chosen style
Display(sty style) string
}
type sliceValue struct {
elemType string
values []Value
}
func (v sliceValue) Equal(o Value) bool {
ov, ok := o.(*sliceValue)
if !ok {
return false
}
if v.elemType != ov.elemType {
return false
}
if len(v.values) != len(ov.values) {
return false
}
for i, v := range v.values {
if !v.Equal(ov.values[i]) {
return false
}
}
return true
}
type arrayValue struct {
elemType string
length int
values []Value
}
func (v arrayValue) Equal(o Value) bool {
ov, ok := o.(*arrayValue)
if !ok {
return false
}
if v.elemType != ov.elemType {
return false
}
if v.length != ov.length {
return false
}
for i, v := range v.values {
if !v.Equal(ov.values[i]) {
return false
}
}
return true
}
type mapEntry struct {
key Value
elem Value
}
type mapValue struct {
keyType string
elemType string
values []mapEntry
}
func (v mapValue) Equal(o Value) bool {
ov, ok := o.(*mapValue)
if !ok {
return false
}
if v.keyType != ov.keyType {
return false
}
if v.elemType != ov.elemType {
return false
}
if len(v.values) != len(ov.values) {
return false
}
// TODO: At some point it'd be nice to sort `values` for each one to make
// this comparison more efficient. The problem is how I would define `Less`
// for Values that aren't the same type. I'd probably have to resort
// to comparing TypeIds but then I'd have to give each Value a TypeId method
// so I'm saving it for some other time.
for _, vval := range v.values {
found := false
for _, ovval := range ov.values {
if vval.key.Equal(ovval.key) && vval.elem.Equal(ovval.elem) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
type opaqueEncodedValue struct {
name string
value _bytes_type
}
func (oev *opaqueEncodedValue) Equal(o Value) bool {
other, ok := o.(*opaqueEncodedValue)
if !ok {
return false
}
return oev.name == other.name && oev.value.Equal(other.value)
}
type structField struct {
name string
value Value
}
type structFields []structField
func (s structFields) Len() int { return len(s) }
func (s structFields) Less(i, j int) bool { return s[i].name < s[j].name }
func (s structFields) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type structValue struct {
name string
sorted bool
fields structFields
}
func (s *structValue) Equal(o Value) bool {
v, ok := o.(*structValue)
if !ok {
return false
}
if s.name != v.name {
return false
}
if len(s.fields) != len(v.fields) {
return false
}
if !s.sorted {
sort.Sort(s.fields)
s.sorted = true
}
if !v.sorted {
sort.Sort(v.fields)
v.sorted = true
}
for fno := range s.fields {
sf := s.fields[fno]
vf := v.fields[fno]
if sf.name != vf.name {
return false
}
if !sf.value.Equal(vf.value) {
return false
}
}
return true
}
type _bool_type bool
func (v _bool_type) Equal(o Value) bool {
ov, ok := o.(_bool_type)
if !ok {
return false
}
return v == ov
}
type _int_type int64
func (v _int_type) Equal(o Value) bool {
ov, ok := o.(_int_type)
if !ok {
return false
}
return v == ov
}
type _uint_type uint64
func (v _uint_type) Equal(o Value) bool {
ov, ok := o.(_uint_type)
if !ok {
return false
}
return v == ov
}
type _float_type float64
func (v _float_type) Equal(o Value) bool {
ov, ok := o.(_float_type)
if !ok {
return false
}
return v == ov
}
type _bytes_type []byte
func (v _bytes_type) Equal(o Value) bool {
ov, ok := o.(_bytes_type)
if !ok {
return false
}
if len(v) != len(ov) {
return false
}
for i, b := range v {
if b != ov[i] {
return false
}
}
return true
}
type _string_type string
func (v _string_type) Equal(o Value) bool {
ov, ok := o.(_string_type)
if !ok {
return false
}
return v == ov
}
type _complex_type complex128
func (v _complex_type) Equal(o Value) bool {
ov, ok := o.(_complex_type)
if !ok {
return false
}
return v == ov
}
type _nil_value struct{}
func (v _nil_value) Equal(o Value) bool {
_, ok := o.(_nil_value)
return ok
}
type interfaceValue struct {
name string
value Value
}
func (v interfaceValue) Equal(o Value) bool {
ov, ok := o.(interfaceValue)
if !ok {
return false
}
if v.name != ov.name {
return false
}
return v.value.Equal(ov.value)
}
func valueFor(id typeId) Value {
switch id {
case _bool_id:
var v _bool_type
return v
case _int_id:
var v _int_type
return v
case _uint_id:
var v _uint_type
return v
case _float_id:
var v _float_type
return v
case _bytes_id:
var v _bytes_type
return v
case _string_id:
var v _string_type
return v
case _complex_id:
var v _complex_type
return v
case _interface_id:
return interfaceValue{value: _nil_value{}}
default:
panic("unknown id")
}
}
func isBuiltin(id typeId) bool {
return (id >= 1) && (id <= 8)
}
func (t typeId) name() string {
switch t {
case _bool_id:
return "bool"
case _int_id:
return "int64"
case _uint_id:
return "uint64"
case _float_id:
return "float64"
case _bytes_id:
return "[]byte"
case _string_id:
return "string"
case _complex_id:
return "complex128"
case _interface_id:
return "interface{}"
default:
panic("unknown id")
}
}