-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_array.go
87 lines (70 loc) · 1.61 KB
/
kind_array.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
package dapper
import (
"encoding/hex"
"reflect"
)
// renderArrayOrSliceKind formats values with a kind of [reflect.Array] or
// [reflect.Slice].
func renderArrayOrSliceKind(r Renderer, v Value) {
if v.Value.Kind() == reflect.Slice && v.Value.IsNil() {
renderNil(r, v)
return
}
if v.IsAmbiguousType() {
r.WriteType(v)
}
if v.Value.Len() == 0 {
r.Print("{}")
return
}
if v.Value.IsZero() {
r.Print("{%s}", r.Config().ZeroValueMarker)
return
}
r.Print("{\n")
r.Indent()
if v.DynamicType.Elem() == typeOf[byte]() {
renderByteArrayElements(r, v)
} else {
renderArrayElements(r, v)
}
r.Outdent()
r.Print("}")
}
func renderArrayType(r Renderer, c Config, t reflect.Type) {
r.Print("[%d]", t.Len())
renderType(r, c, t.Elem())
}
func renderSliceType(r Renderer, c Config, t reflect.Type) {
r.Print("[]")
renderType(r, c, t.Elem())
}
func renderArrayElements(r Renderer, v Value) {
staticType := v.DynamicType.Elem()
isInterface := staticType.Kind() == reflect.Interface
for i := 0; i < v.Value.Len(); i++ {
elem := v.Value.Index(i)
r.WriteValue(
Value{
Value: elem,
DynamicType: elem.Type(),
StaticType: staticType,
IsAmbiguousDynamicType: isInterface,
IsAmbiguousStaticType: false,
IsUnexported: v.IsUnexported,
},
)
r.Print("\n")
}
}
func renderByteArrayElements(r Renderer, v Value) {
d := hex.Dumper(r)
defer d.Close()
data := make([]byte, 1)
for i := 0; i < v.Value.Len(); i++ {
data[0] = byte(v.Value.Index(i).Uint())
if _, err := d.Write(data); err != nil {
panic(panicSentinel{err})
}
}
}