-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfield.go
427 lines (404 loc) · 10.2 KB
/
field.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
package structexplorer
import (
"fmt"
"log/slog"
"reflect"
"sort"
"strconv"
"strings"
"unsafe"
"github.com/mitchellh/hashstructure/v2"
)
var maxFieldValueStringLength = 64
var sliceOrArrayRangeLength = 50
type fieldAccess struct {
owner any
// key is the name of field in struct
// or the string index in a slice or array
// or the encoded key hash in a map
key string
label string
Type string
}
func (f fieldAccess) displayKey() string {
if f.label != "" {
return f.label
}
return f.key
}
func (f fieldAccess) value() any {
rv := reflect.ValueOf(f.owner)
if rv.Kind() == reflect.Interface || rv.Kind() == reflect.Pointer {
// is a pointer
rv = rv.Elem()
} else {
// not a pointer, create an addressable copy
tmp := reflect.New(rv.Type()) // create zero value of same type as v
tmp.Elem().Set(rv)
rv = tmp.Elem()
}
var rf reflect.Value
if rv.Type().Kind() == reflect.Slice || rv.Type().Kind() == reflect.Array {
// check for range: <int>..<int>
if interv := parseInterval(f.key); interv.to != 0 {
// elements may no longer be there
if interv.from < rv.Len() {
if interv.to < rv.Len() {
return rv.Slice(interv.from, interv.to).Interface()
} else {
// to out of bounds
return rv.Slice(interv.from, rv.Len()).Interface()
}
} else {
// from out of bounds
return rv.Slice(0, 0).Interface()
}
} else {
i, _ := strconv.Atoi(f.key)
// element may no longer be there
if i < rv.Len() {
rf = rv.Index(i)
}
}
}
if rv.Type().Kind() == reflect.Map {
// shortcut for string and int keys
keyType := rv.Type().Key()
if keyType.Kind() == reflect.String {
mv := rv.MapIndex(reflect.ValueOf(f.key))
// f.key could be a hash because the key contained a path separator
// then mv is not valid and the fallback is needed to get the actual key.
// todo: f.key != f.label test?
if mv.IsValid() {
return mv.Interface()
}
}
switch keyType.Kind() {
case reflect.Int:
i, _ := strconv.Atoi(f.key)
mv := rv.MapIndex(reflect.ValueOf(i))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
case reflect.Int8:
i, _ := strconv.ParseInt(f.key, 10, 8)
mv := rv.MapIndex(reflect.ValueOf(int8(i)))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
case reflect.Int16:
i, _ := strconv.ParseInt(f.key, 10, 16)
mv := rv.MapIndex(reflect.ValueOf(int16(i)))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
case reflect.Int32:
i, _ := strconv.ParseInt(f.key, 10, 32)
mv := rv.MapIndex(reflect.ValueOf(int32(i)))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
case reflect.Int64:
i, _ := strconv.ParseInt(f.key, 10, 64)
mv := rv.MapIndex(reflect.ValueOf(i))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
case reflect.Uint:
i, _ := strconv.ParseUint(f.key, 10, 0)
return rv.MapIndex(reflect.ValueOf(uint(i))).Interface()
case reflect.Uint8:
i, _ := strconv.ParseUint(f.key, 10, 8)
mv := rv.MapIndex(reflect.ValueOf(uint8(i)))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
case reflect.Uint16:
i, _ := strconv.ParseUint(f.key, 10, 16)
mv := rv.MapIndex(reflect.ValueOf(uint16(i)))
if mv.IsZero() {
return nil
}
return mv.Interface()
case reflect.Uint32:
i, _ := strconv.ParseUint(f.key, 10, 32)
mv := rv.MapIndex(reflect.ValueOf(uint32(i)))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
case reflect.Uint64:
i, _ := strconv.ParseUint(f.key, 10, 64)
mv := rv.MapIndex(reflect.ValueOf(uint64(i)))
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
}
// fallback: name is hash of key
key := stringToReflectMapKey(f.key, rv)
mv := rv.MapIndex(key)
if mv.IsZero() || !mv.CanInterface() {
return nil
}
return mv.Interface()
}
if rv.Type().Kind() == reflect.Struct {
// name is field
rf = rv.FieldByName(f.key)
}
if !rf.IsValid() {
return nil
}
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
if rf.CanInterface() {
return rf.Interface()
}
return nil
}
// pre: canExplore(v)
// post: sorted by label
func newFields(v any) []fieldAccess {
list := []fieldAccess{}
if v == nil {
return list
}
var rt reflect.Type
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Interface || rv.Kind() == reflect.Pointer {
rv = rv.Elem()
if !rv.IsValid() {
return list
}
rt = rv.Type()
} else {
rt = reflect.TypeOf(v)
}
if rt.Kind() == reflect.Struct {
for i := range rt.NumField() {
list = append(list, fieldAccess{
Type: rt.Field(i).Type.String(),
owner: v,
key: rt.Field(i).Name,
})
}
sortEntries(list)
return list
}
if rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array {
rts := rt.Elem().String()
// check if we need ranges
if rv.Len() > sliceOrArrayRangeLength {
// add range keys for subslices
for from, len := 0, rv.Len(); from < len; from += sliceOrArrayRangeLength {
to := from + sliceOrArrayRangeLength
if to > len {
to = len
}
list = append(list, fieldAccess{
Type: rts,
owner: v,
key: makeIntervalKey(from, to),
})
}
} else {
// one by one
for i := 0; i < rv.Len(); i++ {
list = append(list, fieldAccess{
Type: rts,
owner: v,
key: strconv.Itoa(i),
})
}
}
return list
}
if rt.Kind() == reflect.Map {
for _, key := range rv.MapKeys() {
list = append(list, fieldAccess{
Type: rt.Elem().String(),
owner: v,
label: printString(key.Interface()),
key: reflectMapKeyToString(key),
})
}
sortEntries(list)
return list
}
slog.Warn("[structexplorer] no fields for non struct", "value", v, "type", fmt.Sprintf("%T", v))
return list
}
func sortEntries(entries []fieldAccess) {
sort.Slice(entries, func(i, j int) bool {
return entries[i].label < entries[j].label
})
}
func applyFieldNamePadding(list []fieldEntry) []fieldEntry {
// longest field name
maxlength := 0
for _, each := range list {
if l := len(each.Label); l > maxlength {
maxlength = l
}
}
// set padding
for i := 0; i < len(list); i++ {
list[i] = list[i].withPaddingTo(maxlength)
}
return list
}
func valueAtAccessPath(value any, path []string) any {
if value == nil {
return nil
}
if len(path) == 0 {
return value
}
if path[0] == "" {
return valueAtAccessPath(value, path[1:])
}
// field name, index or hash of map key
fa := fieldAccess{owner: value, key: path[0]}
// check for range
if isIntervalKey(fa.key) {
if len(path) > 1 { // continues after range
return valueAtAccessPath(value, path[1:])
}
}
return valueAtAccessPath(fa.value(), path[1:])
}
func printString(v any) string {
if v == nil {
return "nil"
}
switch tv := v.(type) {
case string:
return strconv.Quote(tv)
case *string:
if tv == nil {
return "nil"
}
return "*" + strconv.Quote(*tv)
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16:
return fmt.Sprintf("%d", v)
case uint8:
return fmt.Sprintf("%3d (%s)", v, string(v.(uint8)))
case *int, *int64, *int32, *int16, *int8, *uint, *uint64, *uint32, *uint16, *uint8:
rv := reflect.ValueOf(v).Elem()
if !rv.IsValid() {
return "nil"
}
return fmt.Sprintf("*%d", rv.Int())
case bool:
return strconv.FormatBool(tv)
case *bool:
if tv == nil {
return "nil"
}
return "*" + strconv.FormatBool(*tv)
case float64, float32:
return fmt.Sprintf("%f", v)
case *float64, *float32:
rv := reflect.ValueOf(v).Elem()
if !rv.IsValid() {
return "nil"
}
return fmt.Sprintf("*%f", rv.Float())
case reflect.Value:
if !tv.IsValid() || tv.IsZero() {
return "~nil"
}
return "~" + printString(tv.Interface())
}
// can return string?
if s, ok := v.(fmt.Stringer); ok {
return s.String()
}
if s, ok := v.(fmt.GoStringer); ok {
return s.GoString()
}
return fallbackPrintString(v)
}
func fallbackPrintString(v any) string {
rt := reflect.TypeOf(v)
// see if we can tell the size
if rt.Kind() == reflect.Map || rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array {
rv := reflect.ValueOf(v)
return fmt.Sprintf("%T (%d)", v, rv.Len())
}
if rt.Kind() == reflect.Pointer {
rv := reflect.ValueOf(v).Elem()
if !rv.IsValid() || rv.IsZero() {
return "nil"
}
}
return fmt.Sprintf("%[1]T", v)
}
func ellipsis(s string) string {
if size := len(s); size > maxFieldValueStringLength {
suffix := fmt.Sprintf("...(%d)", size)
return s[:maxFieldValueStringLength-len(suffix)] + suffix
}
return s
}
func reflectMapKeyToString(key reflect.Value) string {
// string and integer keys
if key.Kind() == reflect.String {
s := key.String()
// check for path separator
if !strings.Contains(s, ".") {
return s
}
}
if key.Kind() == reflect.Int || key.Kind() == reflect.Int8 || key.Kind() == reflect.Int16 || key.Kind() == reflect.Int32 || key.Kind() == reflect.Int64 {
return strconv.Itoa(int(key.Int()))
}
if key.Kind() == reflect.Uint || key.Kind() == reflect.Uint8 || key.Kind() == reflect.Uint16 || key.Kind() == reflect.Uint32 || key.Kind() == reflect.Uint64 {
return strconv.FormatUint(uint64(key.Uint()), 10)
}
// fallback to hash of key
hash, _ := hashstructure.Hash(key, hashstructure.FormatV2, nil)
return strconv.FormatUint(hash, 16)
}
func stringToReflectMapKey(hash string, m reflect.Value) reflect.Value {
for _, each := range m.MapKeys() {
cmp := reflectMapKeyToString(each)
if cmp == hash {
return each
}
}
// not found is actually a bug
return reflect.ValueOf(nil)
}
func isZeroPrintstring(s string) bool {
switch s {
case `""`, "0", "false", "nil", "0.000000", "0.000":
return true
}
return false
}
type interval struct {
from int // inclusive
to int // exclusive
}
func (i interval) size() int { return i.to - i.from }
func isIntervalKey(k string) bool { return strings.Contains(k, ":") }
func makeIntervalKey(from, to int) string {
return fmt.Sprintf("%d:%d", from, to)
}
var zeroInterval interval
func parseInterval(k string) interval {
dots := strings.Index(k, ":")
if dots == -1 {
return zeroInterval
}
from, _ := strconv.Atoi(k[:dots])
to, _ := strconv.Atoi(k[dots+1:])
return interval{from, to}
}