forked from smarty/assertions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deeprender.go
325 lines (280 loc) · 7.17 KB
/
deeprender.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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package assertions
import (
"bytes"
"fmt"
"reflect"
"sort"
"strconv"
)
var implicitTypeMap = map[reflect.Kind]string{
reflect.Bool: "bool",
reflect.String: "string",
reflect.Int: "int",
reflect.Int8: "int8",
reflect.Int16: "int16",
reflect.Int32: "int32",
reflect.Int64: "int64",
reflect.Uint: "uint",
reflect.Uint8: "uint8",
reflect.Uint16: "uint16",
reflect.Uint32: "uint32",
reflect.Uint64: "uint64",
reflect.Float32: "float32",
reflect.Float64: "float64",
reflect.Complex64: "complex64",
reflect.Complex128: "complex128",
}
// DeepRender converts a structure to a string representation. Unline the "%#v"
// format string, this resolves pointer types' contents in structs, maps, and
// slices/arrays and prints their field values.
func DeepRender(v interface{}) string {
buf := bytes.Buffer{}
s := (*traverseState)(nil)
s.deepRender(&buf, 0, reflect.ValueOf(v))
return buf.String()
}
// renderPointer is overridable by the test suite in order to have deterministic
// pointer values.
var renderPointer = func(buf *bytes.Buffer, p uintptr) {
fmt.Fprintf(buf, "0x%016x", p)
}
// traverseState is used to note and avoid recursion as struct members are being
// traversed.
//
// traverseState is allowed to be nil. Specifically, the root state is nil.
type traverseState struct {
parent *traverseState
ptr uintptr
}
func (s *traverseState) forkFor(ptr uintptr) *traverseState {
for cur := s; cur != nil; cur = cur.parent {
if ptr == cur.ptr {
return nil
}
}
fs := &traverseState{
parent: s,
ptr: ptr,
}
return fs
}
func (s *traverseState) deepRender(buf *bytes.Buffer, ptrs int, v reflect.Value) {
if v.Kind() == reflect.Invalid {
buf.WriteString("nil")
return
}
vt := v.Type()
// If the type being rendered is a potentially recursive type (a type that
// can contain itself as a member), we need to avoid recursion.
//
// If we've already seen this type before, mark that this is the case and
// write a recursion placeholder instead of actually rendering it.
//
// If we haven't seen it before, fork our `seen` tracking so any higher-up
// renderers will also render it at least once, then mark that we've seen it
// to avoid recursing on lower layers.
pe := uintptr(0)
vk := vt.Kind()
switch vk {
case reflect.Ptr:
// Since structs and arrays aren't pointers, they can't directly be
// recursed, but they can contain pointers to themselves. Record their
// pointer to avoid this.
switch v.Elem().Kind() {
case reflect.Struct, reflect.Array:
pe = v.Pointer()
}
case reflect.Slice, reflect.Map:
pe = v.Pointer()
}
if pe != 0 {
s = s.forkFor(pe)
if s == nil {
buf.WriteString("<REC(")
writeType(buf, ptrs, vt)
buf.WriteString(")>")
return
}
}
switch vk {
case reflect.Struct:
writeType(buf, ptrs, vt)
buf.WriteRune('{')
for i := 0; i < vt.NumField(); i++ {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(vt.Field(i).Name)
buf.WriteRune(':')
s.deepRender(buf, 0, v.Field(i))
}
buf.WriteRune('}')
case reflect.Slice:
if v.IsNil() {
writeType(buf, ptrs, vt)
buf.WriteString("(nil)")
return
}
fallthrough
case reflect.Array:
writeType(buf, ptrs, vt)
buf.WriteString("{")
for i := 0; i < v.Len(); i++ {
if i > 0 {
buf.WriteString(", ")
}
s.deepRender(buf, 0, v.Index(i))
}
buf.WriteRune('}')
case reflect.Map:
writeType(buf, ptrs, vt)
if v.IsNil() {
buf.WriteString("(nil)")
} else {
buf.WriteString("{")
mkeys := v.MapKeys()
tryAndSortMapKeys(vt, mkeys)
for i, mk := range mkeys {
if i > 0 {
buf.WriteString(", ")
}
s.deepRender(buf, 0, mk)
buf.WriteString(":")
s.deepRender(buf, 0, v.MapIndex(mk))
}
buf.WriteRune('}')
}
case reflect.Ptr:
ptrs++
fallthrough
case reflect.Interface:
if v.IsNil() {
writeType(buf, ptrs, v.Type())
buf.WriteRune('(')
fmt.Fprint(buf, "nil")
buf.WriteRune(')')
} else {
s.deepRender(buf, ptrs, v.Elem())
}
case reflect.Chan, reflect.Func, reflect.UnsafePointer:
writeType(buf, ptrs, vt)
buf.WriteRune('(')
renderPointer(buf, v.Pointer())
buf.WriteRune(')')
default:
tstr := vt.String()
implicit := ptrs == 0 && implicitTypeMap[vk] == tstr
if !implicit {
writeType(buf, ptrs, vt)
buf.WriteRune('(')
}
switch vk {
case reflect.String:
fmt.Fprintf(buf, "%q", v.String())
case reflect.Bool:
fmt.Fprintf(buf, "%v", v.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fmt.Fprintf(buf, "%d", v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fmt.Fprintf(buf, "%d", v.Uint())
case reflect.Float32, reflect.Float64:
fmt.Fprintf(buf, "%g", v.Float())
case reflect.Complex64, reflect.Complex128:
fmt.Fprintf(buf, "%g", v.Complex())
}
if !implicit {
buf.WriteRune(')')
}
}
}
func writeType(buf *bytes.Buffer, ptrs int, t reflect.Type) {
parens := ptrs > 0
switch t.Kind() {
case reflect.Chan, reflect.Func, reflect.UnsafePointer:
parens = true
}
if parens {
buf.WriteRune('(')
for i := 0; i < ptrs; i++ {
buf.WriteRune('*')
}
}
switch t.Kind() {
case reflect.Ptr:
if ptrs == 0 {
// This pointer was referenced from within writeType (e.g., as part of
// rendering a list), and so hasn't had its pointer asterisk accounted
// for.
buf.WriteRune('*')
}
writeType(buf, 0, t.Elem())
case reflect.Interface:
if n := t.Name(); n != "" {
buf.WriteString(t.String())
} else {
buf.WriteString("interface{}")
}
case reflect.Array:
buf.WriteRune('[')
buf.WriteString(strconv.FormatInt(int64(t.Len()), 10))
buf.WriteRune(']')
writeType(buf, 0, t.Elem())
case reflect.Slice:
if t == reflect.SliceOf(t.Elem()) {
buf.WriteString("[]")
writeType(buf, 0, t.Elem())
} else {
// Custom slice type, use type name.
buf.WriteString(t.String())
}
case reflect.Map:
if t == reflect.MapOf(t.Key(), t.Elem()) {
buf.WriteString("map[")
writeType(buf, 0, t.Key())
buf.WriteRune(']')
writeType(buf, 0, t.Elem())
} else {
// Custom map type, use type name.
buf.WriteString(t.String())
}
default:
buf.WriteString(t.String())
}
if parens {
buf.WriteRune(')')
}
}
type sortableValueSlice struct {
kind reflect.Kind
elements []reflect.Value
}
func (s *sortableValueSlice) Len() int {
return len(s.elements)
}
func (s *sortableValueSlice) Less(i, j int) bool {
switch s.kind {
case reflect.String:
return s.elements[i].String() < s.elements[j].String()
case reflect.Int:
return s.elements[i].Int() < s.elements[j].Int()
default:
panic(fmt.Errorf("unsupported sort kind: %s", s.kind))
}
}
func (s *sortableValueSlice) Swap(i, j int) {
s.elements[i], s.elements[j] = s.elements[j], s.elements[i]
}
func tryAndSortMapKeys(mt reflect.Type, k []reflect.Value) {
// Try our stock sortable values.
switch mt.Key().Kind() {
case reflect.String, reflect.Int:
vs := &sortableValueSlice{
kind: mt.Key().Kind(),
elements: k,
}
sort.Sort(vs)
}
}