This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
/
reflect.go
360 lines (333 loc) · 8.52 KB
/
reflect.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
package appdash
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"time"
)
func flattenValue(prefix string, v reflect.Value, f func(k, v string)) {
switch o := v.Interface().(type) {
case time.Time:
f(prefix, o.Format(time.RFC3339Nano))
return
case time.Duration:
ms := float64(o.Nanoseconds()) / float64(time.Millisecond)
f(prefix, strconv.FormatFloat(ms, 'f', -1, 64))
return
case fmt.Stringer:
f(prefix, o.String())
return
}
switch v.Kind() {
case reflect.Ptr:
flattenValue(prefix, v.Elem(), f)
case reflect.Bool:
f(prefix, strconv.FormatBool(v.Bool()))
case reflect.Float32, reflect.Float64:
f(prefix, strconv.FormatFloat(v.Float(), 'f', -1, 64))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f(prefix, strconv.FormatInt(v.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
f(prefix, strconv.FormatUint(v.Uint(), 10))
case reflect.String:
f(prefix, v.String())
case reflect.Struct:
for i, name := range fieldNames(v) {
flattenValue(nest(prefix, name), v.Field(i), f)
}
case reflect.Map:
for _, key := range v.MapKeys() {
// small bit of cuteness here: use flattenValue on the key first,
// then on the value
flattenValue("", key, func(_, k string) {
flattenValue(nest(prefix, k), v.MapIndex(key), f)
})
}
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
flattenValue(nest(prefix, strconv.Itoa(i)), v.Index(i), f)
}
default:
f(prefix, fmt.Sprintf("%+v", v.Interface()))
}
}
func mapToKVs(m map[string]string) *[][2]string {
var kvs [][2]string
for k, v := range m {
kvs = append(kvs, [2]string{k, v})
}
sort.Sort(kvsByKey(kvs))
return &kvs
}
type kvsByKey [][2]string
func (v kvsByKey) Len() int { return len(v) }
func (v kvsByKey) Less(i, j int) bool { return v[i][0] < v[j][0] }
func (v kvsByKey) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
type structFieldsByName []reflect.StructField
func (v structFieldsByName) Len() int { return len(v) }
func (v structFieldsByName) Less(i, j int) bool { return fieldName(v[i]) < fieldName(v[j]) }
func (v structFieldsByName) Swap(i, j int) { v[i], v[j] = v[j], v[i] }
func parseValue(as reflect.Type, s string) (reflect.Value, error) {
vp, err := parseValueToPtr(as, s)
if err != nil {
return reflect.Value{}, err
}
if as.Kind() == reflect.Ptr {
return vp, nil
}
return vp.Elem(), nil
}
func parseValueToPtr(as reflect.Type, s string) (reflect.Value, error) {
switch [2]string{as.PkgPath(), as.Name()} {
case [2]string{"time", "Time"}:
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(&t), nil
case [2]string{"time", "Duration"}:
// Multiply by 1000 because flattenValue divides by 1000.
usec, err := strconv.ParseFloat(s, 64)
if err != nil {
return reflect.Value{}, err
}
d := time.Duration(usec * float64(time.Millisecond))
return reflect.ValueOf(&d), nil
}
switch as.Kind() {
case reflect.Ptr:
return parseValueToPtr(as.Elem(), s)
case reflect.Bool:
vv, err := strconv.ParseBool(s)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(&vv), nil
case reflect.Float32, reflect.Float64:
vv, err := strconv.ParseFloat(s, 32)
if err != nil {
return reflect.Value{}, err
}
switch as.Kind() {
case reflect.Float32:
vvv := float32(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Float64:
return reflect.ValueOf(&vv), nil
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
vv, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return reflect.Value{}, err
}
switch as.Kind() {
case reflect.Int:
vvv := int(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Int8:
vvv := int8(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Int16:
vvv := int16(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Int32:
vvv := int32(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Int64:
return reflect.ValueOf(&vv), nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
vv, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return reflect.Value{}, err
}
switch as.Kind() {
case reflect.Uint:
vvv := uint(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Uint8:
vvv := uint8(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Uint16:
vvv := uint16(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Uint32:
vvv := uint32(vv)
return reflect.ValueOf(&vvv), nil
case reflect.Uint64:
return reflect.ValueOf(&vv), nil
}
case reflect.String:
return reflect.ValueOf(&s), nil
}
return reflect.Value{}, nil
}
func unflattenValue(prefix string, v reflect.Value, t reflect.Type, kv *[][2]string) error {
if !sort.IsSorted(kvsByKey(*kv)) {
panic("unflattenValue: kv must be sorted (using kvsByKey)")
}
if len(*kv) == 0 {
return nil
}
if !strings.HasPrefix((*kv)[0][0], prefix) && t.Kind() != reflect.Map { // map can have 0 fields
*kv = (*kv)[1:]
return unflattenValue(prefix, v, t, kv)
}
if v.IsValid() {
treatAsValue := false
switch v.Interface().(type) {
case time.Time:
treatAsValue = true
}
if treatAsValue {
vv, err := parseValue(v.Type(), (*kv)[0][1])
if err != nil {
return err
}
if vv.Type().AssignableTo(v.Type()) {
v.Set(vv)
}
*kv = (*kv)[1:]
return nil
}
}
switch t.Kind() {
case reflect.Ptr:
return unflattenValue(prefix, v, t.Elem(), kv)
case reflect.Interface:
return unflattenValue(prefix, v.Elem(), v.Type(), kv)
case reflect.Bool, reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.String:
vv, err := parseValue(v.Type(), (*kv)[0][1])
if err != nil {
return err
}
v.Set(vv)
*kv = (*kv)[1:]
case reflect.Struct:
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
var vtfs []reflect.StructField
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.PkgPath == "" { // exported
vtfs = append(vtfs, f)
}
}
sort.Sort(structFieldsByName(vtfs))
for _, vtf := range vtfs {
vf := v.FieldByIndex(vtf.Index)
if vf.IsValid() {
fieldPrefix := nest(prefix, fieldName(vtf))
if err := unflattenValue(fieldPrefix, vf, vtf.Type, kv); err != nil {
return err
}
}
}
case reflect.Map:
m := reflect.MakeMap(t)
keyPrefix := prefix + "."
found := 0
for _, kvv := range *kv {
key, val := kvv[0], kvv[1]
if key < keyPrefix {
continue
}
if key > keyPrefix && !strings.HasPrefix(key, keyPrefix) {
break
}
vv, err := parseValue(t.Elem(), val)
if err != nil {
return err
}
m.SetMapIndex(reflect.ValueOf(strings.TrimPrefix(key, keyPrefix)), vv)
*kv = (*kv)[1:]
found++
}
if found > 0 {
v.Set(m)
}
case reflect.Slice, reflect.Array:
keyPrefix := prefix + "."
type elem struct {
i int
s string
}
var elems []elem
maxI := 0
for _, kvv := range *kv {
key, val := kvv[0], kvv[1]
if !strings.HasPrefix(key, keyPrefix) {
break
}
i, err := strconv.Atoi(strings.TrimPrefix(key, keyPrefix))
if err != nil {
return err
}
elems = append(elems, elem{i, val})
if i > maxI {
maxI = i
}
*kv = (*kv)[1:]
}
if v.Kind() == reflect.Slice {
v.Set(reflect.MakeSlice(t, maxI+1, maxI+1))
}
for _, e := range elems {
vv, err := parseValue(t.Elem(), e.s)
if err != nil {
return err
}
v.Index(e.i).Set(vv)
}
}
return nil
}
func fieldNames(v reflect.Value) map[int]string {
t := v.Type()
// check to see if a cached set exists
cachedFieldNamesRW.RLock()
m, ok := cachedFieldNames[t]
cachedFieldNamesRW.RUnlock()
if ok {
return m
}
// otherwise, create it and return it
cachedFieldNamesRW.Lock()
m = make(map[int]string, t.NumField())
for i := 0; i < t.NumField(); i++ {
fld := t.Field(i)
if fld.PkgPath != "" {
continue // ignore all unwant fields
}
m[i] = fieldName(fld)
}
cachedFieldNames[t] = m
cachedFieldNamesRW.Unlock()
return m
}
func fieldName(f reflect.StructField) string {
name := f.Tag.Get("trace")
if name == "" {
name = f.Name
}
// TODO(sqs): check that the field name is only alphanumeric,
// because we sort and assign special meaning to "."
return name
}
var (
cachedFieldNames = make(map[reflect.Type]map[int]string, 20)
cachedFieldNamesRW = new(sync.RWMutex)
)
func nest(prefix, name string) string {
if prefix == "" {
return name
}
return prefix + "." + name
}