-
Notifications
You must be signed in to change notification settings - Fork 5
/
lenspath.go.bak
245 lines (203 loc) · 5.97 KB
/
lenspath.go.bak
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
package lenspath
import (
"fmt"
"reflect"
)
type Lens = string
type Lenspath struct {
lens []Lens
lastArrayPos int // last position of array (*) lens in lenspath
assumeNil bool // if lenspath cannot be resolved, assume nil. If false, return error on unresolved lenspath while traversing structures
}
func Create(lens []Lens) (*Lenspath, error) {
if len(lens) == 0 {
return nil, &EmptyLensPathErr{}
}
lastArrPos := -1
for i, lensv := range lens {
if lensv == "*" {
lastArrPos = i
}
}
assumeNil := true // default to assume nil
return &Lenspath{lens, lastArrPos, assumeNil}, nil
}
func (lp *Lenspath) Get(data any) (any, error) {
return lp.get(data, 0)
}
func (lp *Lenspath) Set(data any, value any) (any, error) {
return lp.set(data, value, 0)
}
func (lp *Lenspath) get(data any, view int) (any, error) {
if view == lp.len() {
return data, nil
} else if data == nil {
if lp.assumeNil {
return nil, nil
} else {
return nil, NewInvalidLensPathErr(view, LensPathStoppedErr)
}
}
kind := reflect.TypeOf(data).Kind()
switch kind {
case reflect.Map:
return lp.getFromMap(data, view)
case reflect.Slice, reflect.Array:
if lp.path(view) == "*" {
// return []any if the array is not homogeneous (some lens gets return nil for example
// or the map entries have different types for same keys)
// else if array is homogeneous, return []<type> (e.g. []string)
arr := reflect.ValueOf(data)
if arr.Len() == 0 {
return nil, nil
}
any_slice := make([]any, 0, arr.Len())
consistent_type := true
var prev_type reflect.Type
for j := 0; j < arr.Len(); j++ {
if value, err := lp.get(arr.Index(j).Interface(), view+1); err == nil {
value_type := reflect.TypeOf(value)
any_slice = append(any_slice, value)
if j > 0 {
consistent_type = consistent_type && value_type == prev_type
}
prev_type = value_type
} else {
return nil, err
}
}
if view != lp.lastArrayPos {
// need to unwrap or flatten the any_slice
consistent_type = true
flattened_slice := make([]any, 0)
for i, value := range any_slice {
arrv := reflect.ValueOf(value)
for j := 0; j < arrv.Len(); j++ {
arrv_val := arrv.Index(j).Interface()
arrv_type := reflect.TypeOf(arrv_val)
flattened_slice = append(flattened_slice, arrv_val)
if i > 0 || j > 0 {
consistent_type = consistent_type && arrv_type == prev_type
}
prev_type = arrv_type
}
}
any_slice = flattened_slice
}
if consistent_type && prev_type != nil {
slice := reflect.MakeSlice(reflect.SliceOf(prev_type), 0, arr.Len())
for _, v := range any_slice {
slice = reflect.Append(slice, reflect.ValueOf(v))
}
return slice.Interface(), nil
}
return any_slice, nil
} else {
return nil, NewInvalidLensPathErr(view, ArrayExpectedErr)
}
case reflect.Struct:
nestv := reflect.ValueOf(data).FieldByName(lp.path(view))
if !nestv.IsValid() || nestv.IsZero() {
if lp.assumeNil {
return nil, nil
} else {
return nil, NewInvalidLensPathErr(view, LensPathStoppedErr)
}
}
return lp.get(nestv.Interface(), view+1)
case reflect.Ptr:
return lp.get(reflect.ValueOf(data).Elem().Interface(), view)
default:
return nil, fmt.Errorf("unhandled case: %T", data)
}
}
func (lp *Lenspath) set(data any, value any, view int) (any, error) {
if view == lp.len() {
return value, nil
}
kind := reflect.TypeOf(data).Kind()
switch kind {
case reflect.Map:
return lp.setFromMap(data, value, view)
case reflect.Slice, reflect.Array:
if lp.path(view) == "*" {
arr := reflect.ValueOf(data)
slice := reflect.MakeSlice(arr.Type(), 0, arr.Len())
// check if value is a slice or array; the length should then match
// each value in the array is set to the corresponding value in the data slice
if reflect.TypeOf(value).Kind() != reflect.Slice && reflect.TypeOf(value).Kind() != reflect.Array {
return nil, ArrayParamExpectedErr
}
value_arr := reflect.ValueOf(value)
if arr.Len() != value_arr.Len() {
return nil, ParamSizeMismatchErr
}
for j := 0; j < arr.Len(); j++ {
if v, err := lp.set(arr.Index(j).Interface(), value_arr.Index(j).Interface(), view+1); err == nil {
slice = reflect.Append(slice, reflect.ValueOf(v))
} else {
return nil, err
}
}
return slice.Interface(), nil
} else {
return nil, NewInvalidLensPathErr(view, ArrayExpectedErr)
}
case reflect.Struct:
field := reflect.ValueOf(data).FieldByName(lp.path(view))
if field.IsZero() {
if lp.assumeNil {
return nil, nil
} else {
return nil, NewInvalidLensPathErr(view, LensPathStoppedErr)
}
}
if field.CanSet() {
if val, err := lp.set(field.Interface(), value, view+1); err != nil {
return nil, err
} else {
field.Set(reflect.ValueOf(val))
}
}
return data, nil
case reflect.Ptr:
return lp.set(reflect.ValueOf(data).Elem().Interface(), value, view)
default:
return nil, fmt.Errorf("unhandled case: %T", data)
}
}
func (lp *Lenspath) setFromMap(data any, value any, view int) (any, error) {
key := reflect.ValueOf((lp.lens[view]))
keyv := reflect.ValueOf(data).MapIndex(key)
tosetv := value
if !keyv.IsValid() || keyv.IsZero() {
if view < lp.len()-1 {
return nil, NewInvalidLensPathErr(view, LensPathStoppedErr)
}
} else if val, err := lp.set(keyv.Interface(), value, view+1); err != nil {
return nil, err
} else {
tosetv = val
}
reflect.ValueOf(data).SetMapIndex(key, reflect.ValueOf(tosetv))
return data, nil
}
func (lp *Lenspath) getFromMap(value any, view int) (any, error) {
key := reflect.ValueOf((lp.lens[view]))
keyv := reflect.ValueOf(value).MapIndex(key)
if !keyv.IsValid() || keyv.IsZero() {
if lp.assumeNil {
return nil, nil
} else {
return nil, NewInvalidLensPathErr(view, LensPathStoppedErr)
}
} else {
return lp.get(keyv.Interface(), view+1)
}
}
func (lp *Lenspath) len() int {
return len(lp.lens)
}
func (lp *Lenspath) path(view int) string {
return string(lp.lens[view])
}