-
Notifications
You must be signed in to change notification settings - Fork 1
/
reflect.go
143 lines (124 loc) · 3.08 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
package inspector
// Reflect inspector. Strongly recommend to not use it. Need only for performance/allocation comparison.
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
type ReflectInspector struct {
BaseInspector
}
func (i ReflectInspector) TypeName() string {
return "reflect"
}
func (i ReflectInspector) Get(src any, path ...string) (any, error) {
var (
r any
c int
k any
)
r = src
for c, k = range path {
r = i.inspect(r, k.(string))
}
if c < len(path)-1 {
r = nil
}
return r, nil
}
func (i ReflectInspector) GetTo(src any, buf *any, path ...string) error {
var err error
*buf, err = i.Get(src, path...)
return err
}
func (i ReflectInspector) Compare(_ any, _ Op, _ string, _ *bool, _ ...string) error {
// Empty method, I'm too lazy to implement it now.
return nil
}
func (i ReflectInspector) Set(_, _ any, _ ...string) error {
// Empty method, there is no way to update data using reflection.
return nil
}
func (i ReflectInspector) SetWithBuffer(_, _ any, _ AccumulativeBuffer, _ ...string) error {
// Empty method, there is no way to update data using reflection.
return nil
}
func (i ReflectInspector) Loop(_ any, _ Iterator, _ *[]byte, _ ...string) (err error) {
// Empty method. todo implement it
return nil
}
func (i ReflectInspector) DeepEqual(l, r any) bool {
return reflect.DeepEqual(l, r)
}
func (i ReflectInspector) DeepEqualWithOptions(l, r any, _ *DEQOptions) bool {
return reflect.DeepEqual(l, r)
}
func (i ReflectInspector) inspect(node any, key string) any {
v := reflect.ValueOf(node)
switch v.Kind() {
case reflect.Ptr:
if elem := v.Elem(); elem.IsValid() && elem.CanInterface() {
node = elem.Interface()
return i.inspect(node, key)
}
case reflect.Map:
kv := reflect.ValueOf(key)
_ = kv
for _, f := range v.MapKeys() {
fv := f.Interface()
if fvs, ok := fv.(string); ok {
if fvs == key {
mv := v.MapIndex(f)
if mv.IsValid() && mv.CanInterface() {
return mv.Interface()
}
return nil
}
}
if fmt.Sprintf("%v", fv) == key {
mv := v.MapIndex(f)
if mv.IsValid() && mv.CanInterface() {
return mv.Interface()
}
}
}
case reflect.Struct:
f := v.FieldByName(key)
if f.IsValid() && f.CanInterface() {
return f.Interface()
}
case reflect.Slice:
if bytes, ok := node.([]byte); ok {
return bytes
}
idx, err := strconv.Atoi(key)
if err != nil {
return nil
}
sv := v.Index(idx)
if sv.IsValid() && sv.CanInterface() {
return sv.Interface()
}
}
return nil
}
func (i ReflectInspector) Unmarshal(p []byte, typ Encoding) (any, error) {
var x any
switch typ {
case EncodingJSON:
err := json.Unmarshal(p, &x)
return x, err
default:
return nil, ErrUnknownEncodingType
}
}
func (i ReflectInspector) Copy(x any) (any, error) {
return x, nil
}
func (i ReflectInspector) CopyTo(_, _ any, _ AccumulativeBuffer) error {
return nil
}
func (i ReflectInspector) Length(_ any, _ *int, _ ...string) error { return nil }
func (i ReflectInspector) Capacity(_ any, _ *int, _ ...string) error { return nil }
func (i ReflectInspector) Reset(_ any) error { return nil }