-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsyntax_node_identifier_recursive_child.go
91 lines (76 loc) · 2.1 KB
/
syntax_node_identifier_recursive_child.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
package jsonpath
import (
"reflect"
)
type syntaxRecursiveChildIdentifier struct {
*syntaxBasicNode
nextMapRequired bool
nextListRequired bool
}
func (i *syntaxRecursiveChildIdentifier) retrieve(
root, current interface{}, container *bufferContainer) errorRuntime {
switch current.(type) {
case map[string]interface{}, []interface{}:
default:
foundType := msgTypeNull
if current != nil {
foundType = reflect.TypeOf(current).String()
}
return ErrorTypeUnmatched{
errorBasicRuntime: i.errorRuntime,
expectedType: msgTypeObjectOrArray,
foundType: foundType,
}
}
var deepestTextLen int
var deepestError errorRuntime
targetNodes := make([]interface{}, 1, 5)
targetNodes[0] = current
for len(targetNodes) > 0 {
currentNode := targetNodes[len(targetNodes)-1]
targetNodes = targetNodes[:len(targetNodes)-1]
switch typedNodes := currentNode.(type) {
case map[string]interface{}:
if i.nextMapRequired {
if err := i.next.retrieve(root, typedNodes, container); err != nil {
if len(container.result) == 0 {
deepestTextLen, deepestError = i.addDeepestError(err, deepestTextLen, deepestError)
}
}
}
sortKeys := getSortedKeys(typedNodes)
for index := len(typedNodes) - 1; index >= 0; index-- {
node := typedNodes[(*sortKeys)[index]]
switch node.(type) {
case map[string]interface{}, []interface{}:
targetNodes = append(targetNodes, node)
}
}
putSortSlice(sortKeys)
case []interface{}:
if i.nextListRequired {
if err := i.next.retrieve(root, typedNodes, container); err != nil {
if len(container.result) == 0 {
deepestTextLen, deepestError = i.addDeepestError(err, deepestTextLen, deepestError)
}
}
}
for index := len(typedNodes) - 1; index >= 0; index-- {
node := typedNodes[index]
switch node.(type) {
case map[string]interface{}, []interface{}:
targetNodes = append(targetNodes, node)
}
}
}
}
if len(container.result) > 0 {
return nil
}
if deepestError == nil {
return ErrorMemberNotExist{
errorBasicRuntime: i.errorRuntime,
}
}
return deepestError
}