...
1 package jsonpath
2
3
4 type pathNode struct {
5 matchOn interface{}
6 childNodes []pathNode
7 action DecodeAction
8 }
9
10
11 func (n *pathNode) match(path JsonPath) *pathNode {
12 var node *pathNode = n
13 for _, ps := range path {
14 found := false
15 for i, n := range node.childNodes {
16 if n.matchOn == ps {
17 node = &node.childNodes[i]
18 found = true
19 break
20 } else if _, ok := ps.(int); ok && n.matchOn == AnyIndex {
21 node = &node.childNodes[i]
22 found = true
23 break
24 }
25 }
26 if !found {
27 return nil
28 }
29 }
30 return node
31 }
32
33
34
35 type PathActions struct {
36 node pathNode
37 }
38
39
40 type DecodeAction func(d *Decoder) error
41
42
43 func (je *PathActions) Add(action DecodeAction, path ...interface{}) {
44
45 var node *pathNode = &je.node
46 for _, ps := range path {
47 found := false
48 for i, n := range node.childNodes {
49 if n.matchOn == ps {
50 node = &node.childNodes[i]
51 found = true
52 break
53 }
54 }
55 if !found {
56 node.childNodes = append(node.childNodes, pathNode{matchOn: ps})
57 node = &node.childNodes[len(node.childNodes)-1]
58 }
59 }
60 node.action = action
61 }
62
View as plain text