-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
488 lines (427 loc) · 13.8 KB
/
dump.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
package kama
import (
"fmt"
"math"
"reflect"
"sort"
"strings"
"unicode"
"golang.org/x/exp/slices"
)
func (va vari) dump(val reflect.Value, hideZeroValues bool, indentLevel int) string {
/*
`compact` indicates whether the struct should be laid in one line or not
`hideZeroValues` indicates whether to show zeroValued vars
`indentLevel` is the number of spaces from the left-most side of the termninal for struct names
In future(if we ever add compation) we could restrict compaction only to arrays/slices/maps that are of primitive(basic) types
see:
1. https://github.com/sanity-io/litter/pull/43
2. https://github.com/komuw/kama/pull/28
*/
deVal := deInterface(val)
if !deVal.IsValid() {
return "nil"
}
if deVal.Type() == nil {
return "nil"
}
iType := val.Type()
indentLevel = indentLevel + 1
iTypeKind := iType.Kind()
iTypeStr := strings.ReplaceAll(fmt.Sprint(iType), "*", "") // remove the pointer symbol.
deValStr := fmt.Sprint(deVal)
{
if slices.Contains(
// todo: Ideally this should be handled inside the `dumpInterface` func.
[]string{
// This are taken from; https://github.com/golang/go/blob/master/src/context/context.go
"context.emptyCtx",
"context.valueCtx",
"context.backgroundCtx",
"context.todoCtx",
"context.withoutCancelCtx",
"context.timerCtx",
"context.cancelCtx",
},
iTypeStr,
) {
// This could be a context.Context type.
// Let's use the formatting that is provided by the stdlib;
// https://github.com/golang/go/blob/39effbc105f5c54117a6011af3c48e3c8f14eca9/src/context/context.go#L197-L206
//
// This will not handle custom types that implement context.Context
return deValStr
}
if slices.Contains(
// todo: Ideally this should be handled inside the `dumpInterface` func.
[]string{
"errors.errorString",
},
iTypeStr,
) {
// This will not handle custom types that implement Error interface.
return "error(" + deValStr + ")"
}
}
switch iTypeKind { //nolint:exhaustive
case reflect.Invalid:
return "<invalid>"
case reflect.String:
return va.dumpString(val)
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.Uintptr:
return va.dumpNumbers(val)
case reflect.Struct:
// We used to use `sanity-io/litter` to do dumping.
// We however, decided to implement our own dump functionality.
//
// The main reason is because sanity-io/litter has no way to compact
// arrays/slices/maps that are inside structs.
// This logic can be discarded if sanity-io/litter implements similar.
// see: https://github.com/sanity-io/litter/pull/43
fromPtr := false
return va.dumpStruct(val, fromPtr, hideZeroValues, indentLevel)
case reflect.Ptr:
v := val.Elem()
if v.IsValid() {
if v.Type().Kind() == reflect.Struct {
fromPtr := true
// TODO: should we pass in `val`, itself instead of `v`
// that way `val.Pointer()` would happen inside `dumpStruct`
return va.dumpStruct(v, fromPtr, hideZeroValues, indentLevel)
} else {
return va.dumpNonStructPointer(v, hideZeroValues, indentLevel)
}
} else {
// `v.IsValid()` returns false if v is the zero Value.
// If `IsValid` returns false, all other methods except String panic.
return val.Type().String() + "(nil)"
}
case reflect.Array, reflect.Slice:
return va.dumpSlice(val, hideZeroValues, indentLevel)
case reflect.Chan:
return va.dumpChan(val)
case reflect.Map:
return va.dumpMap(val, hideZeroValues, indentLevel)
case reflect.Bool:
return fmt.Sprint(val)
case reflect.Func:
return va.dumpFunc(val)
case reflect.Complex64, reflect.Complex128:
return va.dumpComplexNum(val)
case reflect.UnsafePointer:
// It is not generally safe to do anything with an unsafe.Pointer
// see: https://golang.org/pkg/unsafe/#Pointer
// so we probably want to leave it as is.
// do note that if we wanted we could get a uintptr via `val.Pointer()`
return "unsafe.Pointer"
case reflect.Interface:
return va.dumpInterface(val)
default:
return fmt.Sprintf("%v NotImplemented", iTypeKind)
}
}
func (va vari) dumpString(v reflect.Value) string {
// dumps strings
adder := 1 // this is a custom string type.
if strings.HasPrefix(fmt.Sprintf("%#v", v), `"`) {
adder = 2 // the `+2` is important so that the final quote `"` at end of string is not cut off
}
numEntries := v.Len()
if numEntries > 0 && (fmt.Sprintf("%#v", v)[0] == 34) { // 34 is the char("")
adder = 2
}
newLineCount := strings.Count(fmt.Sprintf("%s", v), "\n")
constraint := int(math.Min(float64(numEntries), float64(va.cfg.MaxLength+50))) + adder + newLineCount
s := fmt.Sprintf("%#v", v)[:constraint]
if numEntries > constraint {
remainder := numEntries - constraint
s = s + fmt.Sprintf(" ...<%d more redacted>..", remainder)
}
if s == "" {
s = `""`
}
return s
}
func (va vari) dumpStruct(v reflect.Value, fromPtr, hideZeroValues bool, indentLevel int) string {
/*
`fromPtr` indicates whether the struct is a value or a pointer; `T{}` vs `&T{}`
`compact` indicates whether the struct should be laid in one line or not
`hideZeroValues` indicates whether to show zeroValued fields
`indentLevel` is the number of spaces from the left-most side of the termninal for struct names
*/
typeName := v.Type().Name()
if fromPtr {
typeName = "&" + typeName
}
if indentLevel > va.cfg.MaxIndentLevel {
return fmt.Sprintf("%v: kama warning(indentation `%d` exceeds max of `%d`. Possible circular reference)", typeName, indentLevel, va.cfg.MaxIndentLevel)
}
sep := "\n"
fieldNameSep := strings.Repeat(" ", indentLevel)
lastBracketSep := strings.Repeat(" ", indentLevel-1)
vt := v.Type()
s := fmt.Sprintf("%s{%s", typeName, sep)
numFields := v.NumField()
for i := 0; i < numFields; i++ {
vtf := vt.Field(i)
fieldd := v.Field(i)
if unicode.IsUpper(rune(vtf.Name[0])) || va.cfg.ShowPrivateFields {
// Only dump public fields, unless the config option for private is turned on.
if hideZeroValues && isZeroValue(fieldd) {
continue
} else {
// when something is inside a struct, that's when we use compact & hideZeroValues
cpt := true
_ = cpt
hzv := true
val := va.dump(fieldd, hzv, indentLevel)
s = s + fieldNameSep + vtf.Name + ": " + val + fmt.Sprintf(",%s", sep)
}
}
}
if v.IsZero() && indentLevel > 1 {
s = strings.TrimRight(s, ",\n")
s = s + "}"
} else {
s = s + lastBracketSep + "}"
}
return s
}
func (va vari) dumpSlice(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// dumps slices & arrays
// In future(if we ever add compation) we could restrict compaction only to arrays/slices/maps that are of primitive(basic) types
// see:
// 1. https://github.com/sanity-io/litter/pull/43
// 2. https://github.com/komuw/kama/pull/28
numEntries := v.Len()
constraint := int(math.Min(float64(numEntries), float64(va.cfg.MaxLength)))
typeName := v.Type().String()
newline := "\n"
if numEntries <= 0 {
// do not use newline.
newline = ""
}
leftSep := " "
s := typeName + "{" + newline
for i := 0; i < constraint; i++ {
elm := v.Index(i)
s = s + leftSep + va.dump(elm, hideZeroValues, indentLevel) + "," + newline
}
if numEntries > constraint {
remainder := numEntries - constraint
ident := strings.Repeat(" ", indentLevel)
s = s + ident + fmt.Sprintf(" ...<%d more redacted>..", remainder) + newline
}
if v.IsZero() {
s = s + "(nil)}"
} else if numEntries <= 0 {
s = s + "}"
} else {
ident := strings.Repeat(" ", indentLevel)
s = strings.TrimRight(s, ",") // maybe use `strings.TrimSuffix`
s = s + ident + "}"
}
return s
}
func (va vari) dumpMap(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// dumps maps
// In future(if we ever add compation) we could restrict compaction only to arrays/slices/maps that are of primitive(basic) types
// see:
// 1. https://github.com/sanity-io/litter/pull/43
// 2. https://github.com/komuw/kama/pull/28
numEntries := v.Len()
constraint := int(math.Min(float64(numEntries), float64(va.cfg.MaxLength)))
typeName := v.Type().String()
newline := "\n"
leftSep := " "
colonSep := " "
s := typeName + "{" + newline
// Lets sort the map based on keys. This is done to introduce stability of the output.
// This is not an important part of the design of kama, however, it makes testing much easier.
keys := v.MapKeys()
sort.Slice(keys,
func(i, j int) bool {
// it's unfortunate that we have to dump twice. In this func and in the `for range` below.
return va.dump(keys[i], hideZeroValues, indentLevel) < va.dump(keys[j], hideZeroValues, indentLevel)
},
)
for count, key := range keys {
mapKey := key
mapVal := v.MapIndex(key)
s = s + leftSep + va.dump(mapKey, hideZeroValues, indentLevel) + ":" + colonSep + va.dump(mapVal, hideZeroValues, indentLevel) + ", " + newline
if count > constraint {
remainder := numEntries - constraint
s = s + fmt.Sprintf("%s...<%d more redacted>..", leftSep, remainder)
break
}
}
s = strings.TrimRight(s, ",\n") // maybe use `strings.TrimSuffix`
if v.IsZero() {
s = s + "(nil)}"
} else if numEntries <= 0 {
s = s + "}"
} else {
ident := strings.Repeat(" ", indentLevel)
s = s + "\n" + ident + "}"
}
return s
}
func (va vari) dumpChan(v reflect.Value) string {
// dumps channels
cap := v.Cap()
len := v.Len()
direction := v.Type().ChanDir()
element := v.Type().Elem()
return fmt.Sprintf("%v %v (len=%d, cap=%d)", direction, element, len, cap)
}
func (va vari) dumpFunc(v reflect.Value) string {
// dumps functions
vType := v.Type()
typeName := vType.String()
if !strings.Contains(typeName, "(") {
// ie, typeName is like `http.HandlerFunc` instead of like `func() (io.ReadCloser, error)`
// we thus need to bring out the actual signature
numIn := vType.NumIn()
numOut := vType.NumOut()
if numIn > 0 {
typeName = typeName + "("
for i := 0; i < numIn; i++ {
arg := vType.In(i)
typeName = typeName + arg.String() + ", "
}
typeName = strings.TrimRight(typeName, ", ") // maybe use `strings.TrimSuffix`
typeName = typeName + ")"
}
if numOut > 0 {
typeName = typeName + " ("
for i := 0; i < numOut; i++ {
arg := vType.Out(i)
typeName = typeName + arg.String() + ", "
}
typeName = strings.TrimRight(typeName, ", ") // maybe use `strings.TrimSuffix`
typeName = typeName + ")"
}
}
return typeName
}
func (va vari) dumpComplexNum(v reflect.Value) string {
// dumps complex64 and complex128 numbers
bits := v.Type().Bits()
cmp := v.Complex() // returns complex128 even for `reflect.Complex64`
if bits == 64 {
return fmt.Sprintf("complex64%v", cmp)
}
return fmt.Sprintf("complex128%v", cmp)
}
func (va vari) dumpNonStructPointer(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// dumps pointer types other than struct.
// ie; someIntEight := int8(14); kama.Dirp(&someIntEight)
// dumping for struct pointers is handled in `dumpStruct()`
pref := "&"
s := va.dump(v, hideZeroValues, indentLevel)
if strings.HasPrefix(s, pref) {
return s
}
return pref + s
}
func (va vari) dumpNumbers(v reflect.Value) string {
// dumps numbers.
iType := v.Type()
iTypeKind := iType.Kind()
switch iTypeKind { //nolint:exhaustive
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.Uintptr:
name := v.Type().String()
return fmt.Sprintf("%s(%v)", name, v)
default:
return fmt.Sprintf("%v NotImplemented", iTypeKind)
}
}
func (va vari) dumpInterface(v reflect.Value) string {
// dump interface
name := v.Type().String() // eg; `io.Reader` or `error`
concrete := ""
actualVal := ""
if !v.IsNil() {
elm := v.Elem()
concrete = elm.Type().String() // eg; `*strings.Reader`
// The fmt package treats `reflect.Value` specially.
// It does not call their String method implicitly but instead prints the concrete values they hold.
switch name {
// TODO: add more cases here as we recognise how to handle them
case "error":
actualVal = fmt.Sprint(elm) // this will be the string content of the error
case "context.Context":
actualVal = fmt.Sprint(elm)
default:
actualVal = fmt.Sprint(elm)
// default:
// panic(fmt.Sprintf("dumpInterface unable to handle: `%v`. please open a github issue.", name))
}
} else {
actualVal = "nil"
}
vv := name
if concrete != "" {
vv = vv + "(" + concrete + ")"
}
if actualVal != "" {
vv = vv + " " + actualVal
}
if name == "error" {
return "error(" + actualVal + ")"
}
return vv // s
}
func isPointerValue(v reflect.Value) bool {
// Taken from https://github.com/sanity-io/litter/blob/v1.5.1/util.go
// under the MIT license;
// https://github.com/sanity-io/litter/blob/v1.5.1/LICENSE
switch v.Kind() { //nolint:exhaustive
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return true
}
return false
}
func isZeroValue(v reflect.Value) bool {
// Taken from https://github.com/sanity-io/litter/blob/v1.5.1/util.go
// under the MIT license;
// https://github.com/sanity-io/litter/blob/v1.5.1/LICENSE
return (isPointerValue(v) && v.IsNil()) ||
(v.IsValid() && v.CanInterface() && reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface()))
}
// deInterface returns values inside of non-nil interfaces when possible.
// This is useful for data types like structs, arrays, slices, and maps which
// can contain varying types packed inside an interface.
func deInterface(v reflect.Value) reflect.Value {
// Taken from https://github.com/sanity-io/litter/blob/v1.5.1/util.go
// under the MIT license;
// https://github.com/sanity-io/litter/blob/v1.5.1/LICENSE
if v.Kind() == reflect.Interface && !v.IsNil() {
v = v.Elem()
}
return v
}