-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
197 lines (151 loc) · 3.68 KB
/
iterator.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
package structx
import (
"reflect"
"strconv"
"strings"
)
type Field struct {
Val reflect.Value
Tag reflect.StructTag
}
type FieldFilter func(path string, field Field) (err error)
func IterateObject(obj interface{}, filters ...FieldFilter) (values map[string]Field, err error) {
val := reflect.ValueOf(obj)
if !val.CanInterface() {
return
}
v := reflect.ValueOf(val.Interface())
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
rootPath := ""
if v.Type().Kind() == reflect.Struct {
rootPath = v.Type().Name()
}
tagMap := map[string]reflect.StructTag{}
valMap := map[string]Field{}
err = nested(rootPath, valMap, tagMap, val, filters)
tagMap = nil
if err != nil {
return
}
values = valMap
return
}
func nested(path string, valuesMap map[string]Field, tagMap map[string]reflect.StructTag, val reflect.Value, filters []FieldFilter) (err error) {
if !val.CanInterface() {
return
}
v := reflect.ValueOf(val.Interface())
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
vType := v.Type()
switch v.Kind() {
case reflect.Struct:
vTypeTag, _ := tagMap[path]
if omitnestedField(vTypeTag) {
err = applyFilters(path, valuesMap, tagMap, val, filters)
if err != nil {
return
}
break
}
for i := 0; i < v.NumField(); i++ {
fieldVal := v.Field(i)
fieldTyp := vType.Field(i)
nextPath := path + "." + vType.Field(i).Name
if skipField(fieldTyp.Tag) {
continue
}
tagMap[nextPath] = fieldTyp.Tag
err = nested(nextPath, valuesMap, tagMap, fieldVal, filters)
if err != nil {
return
}
}
case reflect.Map:
mapElem := val.Type()
switch val.Type().Kind() {
case reflect.Ptr, reflect.Array, reflect.Map,
reflect.Slice, reflect.Chan:
mapElem = val.Type().Elem()
if mapElem.Kind() == reflect.Ptr {
mapElem = mapElem.Elem()
}
}
if mapElem.Kind() == reflect.Struct ||
(mapElem.Kind() == reflect.Slice &&
mapElem.Elem().Kind() == reflect.Struct) {
for _, k := range val.MapKeys() {
err = nested(path+"["+k.String()+"]", valuesMap, tagMap, val.MapIndex(k), filters)
if err != nil {
return
}
}
break
}
case reflect.Slice, reflect.Array:
if val.Type().Kind() == reflect.Interface {
break
}
// do not iterate of non struct types, just pass the value. Ie: []int,
// []string, co... We only iterate further if it's a struct.
// i.e []foo or []*foo
if val.Type().Elem().Kind() != reflect.Struct &&
!(val.Type().Elem().Kind() == reflect.Ptr &&
val.Type().Elem().Elem().Kind() == reflect.Struct) {
break
}
for x := 0; x < val.Len(); x++ {
err = nested(path+"["+strconv.Itoa(x)+"]", valuesMap, tagMap, val.Index(x), filters)
if err != nil {
return
}
}
default:
applyFilters(path, valuesMap, tagMap, val, filters)
}
return
}
func applyFilters(path string, valuesMap map[string]Field, tagMap map[string]reflect.StructTag, val reflect.Value, filters []FieldFilter) (err error) {
for i := 0; i < len(filters); i++ {
if val.IsValid() {
tag, _ := tagMap[path]
field := Field{
Val: val,
Tag: tag,
}
err = filters[i](path, field)
if err != nil {
return
}
valuesMap[path] = field
}
}
return
}
func skipField(tag reflect.StructTag) bool {
tagVal, exist := tag.Lookup("structs")
if !exist {
return false
}
tagVals := strings.Split(tagVal, ",")
return hasTagVal(tagVals, "-")
}
func omitnestedField(tag reflect.StructTag) bool {
tagVal, exist := tag.Lookup("structs")
if !exist {
return false
}
tagVals := strings.Split(tagVal, ",")
return hasTagVal(tagVals, "omitnested")
}
func hasTagVal(tagVals []string, findVal string) bool {
for _, val := range tagVals {
if val == findVal {
return true
}
}
return false
}