-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatum.go
502 lines (453 loc) · 9.48 KB
/
datum.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package fgbase
import (
"fmt"
"reflect"
"strconv"
)
// ZeroTester tests if an empty interface represents zero
type ZeroTester interface {
ZeroTest() bool
}
// ZeroTest returns true if empty interface (interface{}) is a numeric zero.
func ZeroTest(a interface{}) bool {
switch v := a.(type) {
case int8:
return v == 0
case uint8:
return v == 0
case int16:
return v == 0
case uint16:
return v == 0
case int32:
return v == 0
case uint32:
return v == 0
case int64:
return v == 0
case uint64:
return v == 0
case int:
return v == 0
case uint:
return v == 0
case float32:
return v == 0.0
case float64:
return v == 0.0
case complex64:
return v == 0.0+0.0i
case complex128:
return v == 0.0+0.0i
default:
if v, ok := a.(ZeroTester); ok {
return v.ZeroTest()
}
}
return false
}
// EqualsTest returns true if two empty interfaces have the same numeric or string value.
func EqualsTest(n *Node, a, b interface{}) bool {
a2, b2, same := Promote(n, a, b)
if !same {
return false
}
return a2 == b2
}
// IsInt returns true if empty interface (interface{}) is an int.
func IsInt(d interface{}) bool {
return reflect.ValueOf(d).Kind() == reflect.Int
}
// IsSlice returns true if empty interface (interface{}) is a slice.
func IsSlice(d interface{}) bool {
return reflect.ValueOf(d).Kind() == reflect.Slice
}
// IsStruct returns true if empty interface (interface{}) is a struct.
func IsStruct(d interface{}) bool {
return reflect.ValueOf(d).Kind() == reflect.Struct
}
// IsPtr returns true if empty interface (interface{}) is a pointer
func IsPtr(d interface{}) bool {
return reflect.ValueOf(d).Kind() == reflect.Ptr
}
// ResolvePtr resolves an empty interface which is a pointer
func ResolvePtr(d interface{}) interface{} {
rv := reflect.ValueOf(d)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
if rv.IsValid() {
return rv.Interface()
}
return nil
}
// Index returns the nth element of an empty interface (interface{}) that is a slice.
func Index(d interface{}, i int) interface{} {
return reflect.ValueOf(d).Index(i).Interface()
}
// Len returns the length of an empty interface (interface{}) if it is a slice.
func Len(d interface{}) int {
if IsSlice(d) {
return reflect.ValueOf(d).Len()
}
return 0
}
// CopySlice returns a copy of a slice from an empty interface (as an empty interface).
func CopySlice(d interface{}) interface{} {
dt := reflect.TypeOf(d)
dv := reflect.ValueOf(d)
r := reflect.MakeSlice(dt, dv.Len(), dv.Cap()).Interface()
reflect.Copy(reflect.ValueOf(r), reflect.ValueOf(d))
return r
}
// String returns a string representation of a interface{} with
// ellipse shortened slices if TraceLevel<VVVV.
func String(d interface{}) string {
d = ResolvePtr(d)
if v, ok := d.(fmt.Stringer); ok {
return /*"AAA"+*/ v.String()
}
if IsSlice(d) {
return /*"BBB"+*/ StringSlice(d)
}
if dd, ok := d.([]interface{}); ok {
var s string
for i := range dd {
if i != 0 {
s += "|"
}
s += String(dd[i])
}
return /*"CCC"+*/ s
}
if dd, ok := d.(ackWrap); ok {
return /*"DDD"+*/ String(dd.datum)
}
if IsStruct(d) {
return /*"EEE"+*/ StringStruct(d)
}
var s string
switch d.(type) {
case bool, int8, int16, int32, int64, int:
{
s = fmt.Sprintf("%v", d)
}
case uint8, uint16, uint32, uint64, uint:
{
w := 2
switch d.(type) {
case uint16:
{
w = 4
}
case uint32:
{
w = 8
}
case uint64, uint:
{
w = 16
}
}
s = fmt.Sprintf("0x%0"+strconv.Itoa(w)+"x", d)
}
case float32, float64:
{
s = fmt.Sprintf("%.4g", d)
}
case complex64, complex128:
{
s = fmt.Sprintf("%.4g", d)
}
case string:
{
s = fmt.Sprintf("%q", d)
}
}
if !TraceTypes && s != "" {
return /*"FFF"+*/ s
}
if s == "" {
s = fmt.Sprintf("%+v", d)
}
if IsInt(d) || !TraceTypes {
return /*"GGG"+*/ fmt.Sprintf("%s", s)
}
return "HHH" + fmt.Sprintf("%T(%s)", d, s)
}
// StringSlice returns a string representation of a slice, ellipse shortened if TraceLevel<VVVV.
func StringSlice(d interface{}) string {
m := 8
l := Len(d)
if l < m || TraceLevel == VVVV {
m = min(l, 1024)
}
dv := reflect.ValueOf(d)
dt := reflect.TypeOf(d)
dts := dt.String()
s := fmt.Sprintf("[:%d]%s{", dv.Len(), dts[2:len(dts)])
for i := 0; i < m; i++ {
if i != 0 {
s += " "
}
s += fmt.Sprintf("%s", String(Index(d, i)))
}
if m < l {
s += " ..."
}
s += "}"
return s
}
// isShadowSlice returns true if the nth field of the struct is a slice that is shadowing
// another field.
func isShadowSlice(d interface{}, nth int) bool {
if !IsStruct(d) {
return false
}
dv := reflect.ValueOf(d)
l := dv.NumField()
if nth >= l {
return false
}
shadow := dv.Type().Field(nth)
// search for shadowing struct
for j := 0; j < l; j++ {
if j == nth {
continue
}
slice := dv.Type().Field(j)
if shadow.Type.String() == slice.Type.String() && shadow.Name == "Shadow"+slice.Name {
return true
}
}
return false
}
// shadowSlice returns the index of a struct field that is a slice that
// is being shadowed by the nth field of the struct (with a "Shadow" prefix and matching type).
// -1 returned if not found
func shadowSlice(d interface{}, nth int) int {
if !IsStruct(d) {
return -1
}
dv := reflect.ValueOf(d)
l := dv.NumField()
if nth >= l {
return -1
}
slice := dv.Type().Field(nth)
// search for shadowed struct
for j := 0; j < l; j++ {
if j == nth {
continue
}
shadow := dv.Type().Field(j)
if shadow.Type.String() == slice.Type.String() && shadow.Name == "Shadow"+slice.Name {
return j
}
}
return -1
}
// shadowString returns a struct-like string for a shadows slice, where the index of a changed
// value proceeds the value and a colon.
func shadowString(d interface{}, sliceIndex, shadowIndex int) string {
if !IsStruct(d) {
return ""
}
dv := reflect.ValueOf(d)
n := dv.NumField()
if sliceIndex >= n || shadowIndex >= n {
return ""
}
slice := dv.Field(sliceIndex).Interface()
shadow := dv.Field(shadowIndex).Interface()
st := reflect.TypeOf(slice).String()
l := Len(slice)
s := fmt.Sprintf("[:%d]%v{", l, st[2:])
var first = true
for i := 0; i < l; i++ {
if !EqualsTest(nil, Index(slice, i), Index(shadow, i)) {
if !first {
s += ", "
} else {
first = false
}
s += fmt.Sprintf("%d:%s", i, String(Index(slice, i)))
}
}
s += "}"
return s
}
// StringStruct returns a string representation of a struct with
// ellipse shortened slices if TraceLevel<VVVV.
func StringStruct(d interface{}) string {
dv := reflect.ValueOf(d)
l := dv.NumField()
var s string
if TraceLevel >= VVVV {
s = fmt.Sprintf("%T", d)
}
s += "{"
if false && reflect.DeepEqual(reflect.Zero(reflect.TypeOf(d)).Interface(), d) {
s += "}"
return s
}
if s, ok := d.(fmt.Stringer); ok {
return s.String()
}
flg := false
for i := 0; i < l; i++ {
ft := dv.Type().Field(i)
if ft.Name[0] >= 'A' && ft.Name[0] <= 'Z' {
if isShadowSlice(d, i) {
continue
}
if flg {
s += " "
} else {
flg = true
}
s += ft.Name
s += ":"
j := shadowSlice(d, i)
if j < 0 {
s += String(dv.Field(i).Interface())
} else {
s += shadowString(d, i, j)
}
}
}
s += "}"
return s
}
// ParseDatum parses a string for numeric constants, otherwise returns the string.
func ParseDatum(s string) interface{} {
var v interface{}
// trim trailing whitespace or comments
var s2 string
for i := range s {
if s[i] == '#' {
break
}
if len(s) > i+1 && s[i:i+2] == "//" {
break
}
if s[i] == ' ' || s[i] == '\t' {
break
}
s2 += s[i : i+1]
}
s = s2
if s == "{}" {
return struct{}{}
}
if len(s) > 2 && s[0:2] == "0x" {
s = s[2:]
if len(s) <= 2 {
u8, err := strconv.ParseUint(s, 16, 8)
if err == nil {
v = uint8(u8)
return v
}
}
if len(s) <= 4 {
u16, err := strconv.ParseUint(s, 16, 16)
if err == nil {
v = uint16(u16)
return v
}
}
if len(s) <= 8 {
u32, err := strconv.ParseUint(s, 16, 32)
if err == nil {
v = uint32(u32)
return v
}
}
u64, err := strconv.ParseUint(s, 16, 64)
if err == nil {
v = u64
return v
}
}
i32, err := strconv.ParseInt(s, 10, 32)
if err == nil {
v = int(i32)
return v
}
i64, err := strconv.ParseInt(s, 10, 64)
if err == nil {
v = i64
return v
}
f32, err := strconv.ParseFloat(s, 32)
if err == nil {
v = f32
return v
}
f64, err := strconv.ParseFloat(s, 64)
if err == nil {
v = f64
return v
}
if s == "true" || s == "false" {
b, err := strconv.ParseBool(s)
if err == nil {
v = b
return v
}
}
v = s
return v
}
// IsNada tests if a interface{} (an empty interface) is a struct{} (an empty struct)
func IsNada(d interface{}) bool {
return reflect.TypeOf(d) == reflect.TypeOf(struct{}{})
}
// IsZero returns true if a interface{} has a golang zero value
func IsZero(d interface{}) bool {
return reflect.DeepEqual(reflect.Zero(reflect.TypeOf(d)).Interface(), d)
}
// Inter converts a value to an int
type Inter interface {
Int() int
}
// Int returns an int version of a value
func Int(a interface{}) int {
switch a.(type) {
case int8:
return a.(int)
case uint8:
return a.(int)
case int16:
return a.(int)
case uint16:
return a.(int)
case int32:
return a.(int)
case uint32:
return a.(int)
case int64:
return a.(int)
case uint64:
return a.(int)
case int:
return a.(int)
case uint:
return a.(int)
case float32:
return a.(int)
case float64:
return a.(int)
case complex64:
return a.(int)
case complex128:
return a.(int)
default:
{
if v, ok := a.(Inter); ok {
return v.Int()
}
}
}
return -1
}