forked from mcuadros/go-lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
243 lines (199 loc) · 5.15 KB
/
lookup.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
/*
Small library on top of reflect for make lookups to Structs or Maps. Using a
very simple DSL you can access to any property, key or value of any value of Go.
*/
package lookup
import (
"errors"
"reflect"
"strconv"
"strings"
)
const (
SplitToken = "."
IndexCloseChar = "]"
IndexOpenChar = "["
)
var (
ErrMalformedIndex = errors.New("Malformed index key")
ErrInvalidIndexUsage = errors.New("Invalid index key usage")
ErrKeyNotFound = errors.New("Unable to find the key")
)
// LookupString performs a lookup into a value, using a string. Same as `Loookup`
// but using a string with the keys separated by `.`
func LookupString(i interface{}, path string) (reflect.Value, error) {
return Lookup(i, strings.Split(path, SplitToken)...)
}
// Lookup performs a lookup into a value, using a path of keys. The key should
// match with a Field or a MapIndex. For slice you can use the syntax key[index]
// to access a specific index. If one key owns to a slice and an index is not
// specificied the rest of the path will be apllied to evaley value of the
// slice, and the value will be merged into a slice.
func Lookup(i interface{}, path ...string) (reflect.Value, error) {
value := reflect.ValueOf(i)
var parent reflect.Value
var err error
for i, part := range path {
parent = value
value, err = getValueByName(value, part)
if err == nil {
continue
}
if !isAggregable(parent) {
break
}
value, err = aggreateAggregableValue(parent, path[i:])
break
}
return value, err
}
func getValueByName(v reflect.Value, key string) (reflect.Value, error) {
var value reflect.Value
var index int
var err error
key, index, err = parseIndex(key)
if err != nil {
return value, err
}
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
return getValueByName(v.Elem(), key)
case reflect.Struct:
value = v.FieldByName(key)
case reflect.Map:
kValue := reflect.Indirect(reflect.New(v.Type().Key()))
kValue.SetString(key)
value = v.MapIndex(kValue)
}
if !value.IsValid() {
return reflect.Value{}, ErrKeyNotFound
}
if index != -1 {
if value.Type().Kind() != reflect.Slice {
return reflect.Value{}, ErrInvalidIndexUsage
}
value = value.Index(index)
}
if value.Kind() == reflect.Ptr || value.Kind() == reflect.Interface {
value = value.Elem()
}
return value, nil
}
func aggreateAggregableValue(v reflect.Value, path []string) (reflect.Value, error) {
values := make([]reflect.Value, 0)
l := v.Len()
if l == 0 {
ty, ok := lookupType(v.Type(), path...)
if !ok {
return reflect.Value{}, ErrKeyNotFound
}
return reflect.MakeSlice(reflect.SliceOf(ty), 0, 0), nil
}
index := indexFunction(v)
for i := 0; i < l; i++ {
value, err := Lookup(index(i).Interface(), path...)
if err != nil {
return reflect.Value{}, err
}
values = append(values, value)
}
return mergeValue(values), nil
}
func indexFunction(v reflect.Value) func(i int) reflect.Value {
switch v.Kind() {
case reflect.Slice:
return v.Index
case reflect.Map:
keys := v.MapKeys()
return func(i int) reflect.Value {
return v.MapIndex(keys[i])
}
default:
panic("unsuported kind for index")
}
}
func mergeValue(values []reflect.Value) reflect.Value {
values = removeZeroValues(values)
l := len(values)
if l == 0 {
return reflect.Value{}
}
sample := values[0]
mergeable := isMergeable(sample)
t := sample.Type()
if mergeable {
t = t.Elem()
}
value := reflect.MakeSlice(reflect.SliceOf(t), 0, 0)
for i := 0; i < l; i++ {
if !values[i].IsValid() {
continue
}
if mergeable {
value = reflect.AppendSlice(value, values[i])
} else {
value = reflect.Append(value, values[i])
}
}
return value
}
func removeZeroValues(values []reflect.Value) []reflect.Value {
l := len(values)
var v []reflect.Value
for i := 0; i < l; i++ {
if values[i].IsValid() {
v = append(v, values[i])
}
}
return v
}
func isAggregable(v reflect.Value) bool {
k := v.Kind()
return k == reflect.Map || k == reflect.Slice
}
func isMergeable(v reflect.Value) bool {
k := v.Kind()
return k == reflect.Map || k == reflect.Slice
}
func hasIndex(s string) bool {
return strings.Index(s, IndexOpenChar) != -1
}
func parseIndex(s string) (string, int, error) {
start := strings.Index(s, IndexOpenChar)
end := strings.Index(s, IndexCloseChar)
if start == -1 && end == -1 {
return s, -1, nil
}
if (start != -1 && end == -1) || (start == -1 && end != -1) {
return "", -1, ErrMalformedIndex
}
index, err := strconv.Atoi(s[start+1 : end])
if err != nil {
return "", -1, ErrMalformedIndex
}
return s[:start], index, nil
}
func lookupType(ty reflect.Type, path ...string) (reflect.Type, bool) {
if len(path) == 0 {
return ty, true
}
switch ty.Kind() {
case reflect.Slice, reflect.Array, reflect.Map:
if hasIndex(path[0]) {
return lookupType(ty.Elem(), path[1:]...)
}
// Aggregate.
return lookupType(ty.Elem(), path...)
case reflect.Ptr:
return lookupType(ty.Elem(), path...)
case reflect.Interface:
// We can't know from here without a value. Let's just return this type.
return ty, true
case reflect.Struct:
f, ok := ty.FieldByName(path[0])
if ok {
return lookupType(f.Type, path[1:]...)
}
}
return nil, false
}