Skip to content

Commit 098ce1d

Browse files
committed
refactor: remove multiple loops
remove multiple loops in recursive token
1 parent 917c754 commit 098ce1d

File tree

3 files changed

+213
-155
lines changed

3 files changed

+213
-155
lines changed

token/recursive.go

+23-39
Original file line numberDiff line numberDiff line change
@@ -20,81 +20,65 @@ func (token *recursiveToken) Type() string {
2020
}
2121

2222
func (token *recursiveToken) Apply(root, current interface{}, next []Token) (interface{}, error) {
23+
return token.recursiveApply(root, current, next), nil
24+
}
2325

24-
elements := flatten(current)
25-
26-
if len(next) > 0 {
27-
nextToken := next[0]
28-
futureTokens := next[1:]
26+
func (token *recursiveToken) recursiveApply(root, current interface{}, next []Token) []interface{} {
2927

30-
results := make([]interface{}, 0)
28+
slice := make([]interface{}, 0)
3129

32-
for _, item := range elements {
33-
result, _ := nextToken.Apply(root, item, futureTokens)
34-
objType, objVal := getTypeAndValue(result)
35-
if objType == nil {
36-
continue
37-
}
30+
objType, objVal := getTypeAndValue(current)
31+
if objType == nil {
32+
return slice
33+
}
3834

35+
if len(next) > 0 {
36+
result, _ := next[0].Apply(root, objVal.Interface(), next[1:])
37+
objType, objVal := getTypeAndValue(result)
38+
if objType != nil {
3939
switch objType.Kind() {
4040
case reflect.Array, reflect.Slice:
4141
length := objVal.Len()
4242
for i := 0; i < length; i++ {
43-
results = append(results, objVal.Index(i).Interface())
43+
slice = append(slice, objVal.Index(i).Interface())
4444
}
4545
break
4646
default:
47-
results = append(results, result)
47+
slice = append(slice, objVal.Interface())
4848
break
4949
}
50-
5150
}
52-
53-
return results, nil
51+
} else {
52+
slice = append(slice, objVal.Interface())
5453
}
55-
return elements, nil
56-
}
5754

58-
func flatten(obj interface{}) []interface{} {
59-
slice := make([]interface{}, 0)
60-
61-
objType, objVal := getTypeAndValue(obj)
62-
if objType == nil {
63-
return slice
64-
}
65-
66-
slice = append(slice, objVal.Interface())
67-
68-
elements := make([]interface{}, 0)
6955
switch objType.Kind() {
7056
case reflect.Map:
7157
keys := objVal.MapKeys()
7258
sortMapKeys(keys)
7359
for _, kv := range keys {
7460
value := objVal.MapIndex(kv).Interface()
75-
elements = append(elements, value)
61+
result := token.recursiveApply(root, value, next)
62+
slice = append(slice, result...)
7663
}
7764
case reflect.Array, reflect.Slice:
7865
length := objVal.Len()
7966
for i := 0; i < length; i++ {
8067
value := objVal.Index(i).Interface()
81-
elements = append(elements, value)
68+
result := token.recursiveApply(root, value, next)
69+
slice = append(slice, result...)
8270
}
8371
case reflect.Struct:
8472
fields := getStructFields(objVal, true)
8573
for _, field := range fields {
8674
value := objVal.FieldByName(field.Name).Interface()
87-
elements = append(elements, value)
75+
result := token.recursiveApply(root, value, next)
76+
slice = append(slice, result...)
77+
8878
}
8979
default:
9080
break
9181
}
9282

93-
if len(elements) > 0 {
94-
for _, sObj := range elements {
95-
slice = append(slice, flatten(sObj)...)
96-
}
97-
}
98-
9983
return slice
10084
}

0 commit comments

Comments
 (0)