-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.go
239 lines (215 loc) · 4.89 KB
/
logic.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
package goramda
import (
"reflect"
"strconv"
)
// TODO: following have to implement
/*
And
Or
Not
IfElse
HasPath
PathOr
PathEq
PathSatisfies
PropSatisfies
May implement
until
when
*/
func And(expressions ...bool) bool {
return !Includes(expressions, false)
}
func Or(expressions ...bool) bool {
return !Includes(expressions, true)
}
func Not(expression bool) bool {
return !expression
}
func IfElse(cond bool, trueBlock interface{}, falseBlock interface{}) interface{} {
if cond == true {
return trueBlock
}
return falseBlock
}
func Path(path []string, data interface{}) interface{} {
var result interface{}
if IsEmpty(data) {
return nil
}
f := DeepFields(0, path, data, &result)
if Equals(path, f) {
return result
}
return nil
}
func PathOr(defaultResult interface{}, path []string, data interface{}) interface{} {
var result interface{}
if IsEmpty(data) {
return defaultResult
}
f := DeepFields(0, path, data, &result)
if Equals(path, f) {
return result
}
return defaultResult
}
func IsInteger(s string) bool {
_, err := strconv.Atoi(s)
if err != nil {
return false
}
return true
}
func DeepFields(index int, path []string, iface interface{}, result interface{}) (field []string) {
setResult := func() {
val := reflect.ValueOf(result)
if result != nil {
val.Elem().Set(reflect.ValueOf(iface))
}
}
if len(path) <= index {
setResult()
return
}
ifv := reflect.ValueOf(iface)
switch ifv.Kind() {
case reflect.Struct:
v := ifv.FieldByName(path[index])
if !v.IsValid() {
return
}
field = append(field, path[index])
subField := DeepFields(index+1, path, v.Interface(), result)
if len(subField) > 0 {
field = append(field, subField...)
}
case reflect.Slice:
sindex, err := strconv.Atoi(path[index])
if err != nil {
return
}
if len(path) == index+1 && sindex >= 0 {
if ifv.IsValid() && result != nil && ifv.Len() > sindex {
val := reflect.ValueOf(result)
field = append(field, path[index])
setResult()
val.Elem().Set(ifv.Index(sindex))
}
return
}
field = append(field, path[index])
if ifv.Len() > sindex {
subField := DeepFields(index+1, path, ifv.Index(sindex).Interface(), result)
if len(subField) > 0 {
field = append(field, subField...)
}
}
return
case reflect.Ptr:
subField := DeepFields(index, path,
ifv.Elem().Interface(),
result)
if len(subField) > 0 {
field = append(field, subField...)
}
case reflect.Map:
for _, e := range ifv.MapKeys() {
if Equals(e.Interface(), path[index]) {
mv := ifv.MapIndex(e)
switch mv.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64, reflect.String, reflect.Float64:
val := reflect.ValueOf(result)
if result != nil {
val.Elem().Set(mv)
if mv.IsValid() {
field = append(field, path[index])
}
}
case reflect.Struct:
if !mv.IsValid() {
return
}
field = append(field, path[index])
subField := DeepFields(index+1, path, mv.Interface(), result)
if len(subField) > 0 {
field = append(field, subField...)
}
case reflect.Ptr:
field = append(field, path[index])
subField := DeepFields(index+1, path, mv.Interface(), result)
if len(subField) > 0 {
field = append(field, subField...)
}
default:
return
}
}
}
default:
v := ifv.FieldByName(path[index])
if v.IsValid() {
field = append(field, path[index])
}
setResult()
}
return
}
type DeciderFun func(interface{}) bool
func PathEq(path []string, elm interface{}) DeciderFun {
return func(data interface{}) bool {
eq := func(d interface{}) bool { return Equals(d, elm) }
return PathSatisfies(eq, path, data)
}
}
func HasPath(path []string, dd interface{}) bool {
f := DeepFields(0, path, dd, nil)
return Equals(path, f)
}
func PathSatisfies(fn DeciderFun, path []string, data interface{}) bool {
var result interface{}
f := DeepFields(0, path, data, &result)
if Equals(path, f) {
return fn(result)
}
return false
}
func PropSatisfies(fn DeciderFun, path string, d interface{}) bool {
return PathSatisfies(fn, []string{path}, d)
}
func getDefaultValueOf(d interface{}) interface{} {
if IsNil(d) {
return nil
}
ifv := reflect.ValueOf(d)
var defaultValue interface{}
switch ifv.Kind() {
case reflect.Ptr:
elmType := reflect.TypeOf(d).Elem()
elmPtr2 := reflect.New(elmType)
defaultValue = elmPtr2.Interface()
default:
ptrOfElm := reflect.New(reflect.TypeOf(d))
defaultValue = ptrOfElm.Elem().Interface()
}
return defaultValue
}
func IsNil(d interface{}) bool {
if d == nil {
return true
}
switch reflect.TypeOf(d).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(d).IsNil()
}
return false
}
// IsEmpty will return true if it has its default value
func IsEmpty(d interface{}) bool {
if IsNil(d) {
return true
}
defaultValue := getDefaultValueOf(d)
return Equals(d, defaultValue)
}