forked from covalenthq/lenspath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenspath2.go
172 lines (140 loc) · 3.95 KB
/
lenspath2.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
package lenspath
import (
"fmt"
"reflect"
)
type Lens = string
type LeafCallback = func(any) any // called when a leaf (of the lenspath) is reached; return value is set to the leaf (in case of setter)
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
}
type TraversalDetails struct {
callback LeafCallback
settable bool
}
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) {
if lp.isArrBased() {
return nil, NewInvalidLensPathErr(-1, PathContainsArrErr)
}
var rdata any
_, err := lp.recurse(data, 0, &TraversalDetails{callback: func(data any) any {
rdata = data
return nil
}, settable: false})
return rdata, err
}
func (lp *Lenspath) Getter(data any, callback LeafCallback) error {
_, err := lp.recurse(data, 0, &TraversalDetails{callback: callback, settable: false})
return err
}
func (lp *Lenspath) Set(data any, value any) error {
if lp.isArrBased() {
return NewInvalidLensPathErr(-1, PathContainsArrErr)
}
_, err := lp.recurse(data, 0, &TraversalDetails{callback: func(data any) any {
return value
}, settable: true})
return err
}
func (lp *Lenspath) Setter(data any, callback LeafCallback) error {
_, err := lp.recurse(data, 0, &TraversalDetails{callback: callback, settable: true})
return err
}
func (lp *Lenspath) recurse(data any, view int, details *TraversalDetails) (any, error) {
if view == lp.len() {
return details.callback(data), nil
} else if data == nil {
return nil, nil
}
kind := reflect.TypeOf(data).Kind()
switch kind {
case reflect.Map:
lp.traverseMap(data, view, details)
case reflect.Slice, reflect.Array:
if lp.path(view) == "*" {
lp.traverseSlice(data, view, details)
} else {
return nil, NewInvalidLensPathErr(view, ArrayExpectedErr)
}
case reflect.Struct:
nestv := reflect.ValueOf(data).FieldByName(lp.path(view))
if !nestv.IsValid() || nestv.IsZero() {
if lp.atLeaf(view) {
details.callback(nil)
}
} else {
lp.recurse(nestv.Interface(), view+1, details)
}
case reflect.Ptr:
lp.recurse(reflect.ValueOf(data).Elem().Interface(), view, details)
default:
return nil, fmt.Errorf("unhandled case: %T", data)
}
return nil, nil
}
func (lp *Lenspath) traverseSlice(value any, view int, details *TraversalDetails) {
// 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(value)
if arr.Len() == 0 {
lp.recurse(nil, view+1, details)
return
}
for j := 0; j < arr.Len(); j++ {
lp.recurse(arr.Index(j).Interface(), view+1, details)
}
}
func (lp *Lenspath) traverseMap(value any, view int, details *TraversalDetails) (any, error) {
key := reflect.ValueOf((lp.lens[view]))
keyv := reflect.ValueOf(value).MapIndex(key)
var val any
var err error
if !keyv.IsValid() || keyv.IsZero() {
if !lp.atLeaf(view) {
return nil, nil
}
val = details.callback(nil)
} else {
val, err = lp.recurse(keyv.Interface(), view+1, details)
}
if err != nil {
return nil, err
}
if details.settable && lp.atLeaf(view) {
reflect.ValueOf(value).SetMapIndex(key, reflect.ValueOf(val))
}
return nil, nil
}
func (lp *Lenspath) len() int {
return len(lp.lens)
}
func (lp *Lenspath) path(view int) string {
return string(lp.lens[view])
}
func (lp *Lenspath) atLeaf(view int) bool {
return view >= lp.len()-1
}
func (lp *Lenspath) isArrBased() bool {
for _, lensv := range lp.lens {
if lensv == "*" {
return true
}
}
return false
}