forked from PaesslerAG/jsonpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
103 lines (87 loc) · 2.69 KB
/
path.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
package jsonpath
import "context"
type path interface {
evaluate(c context.Context, parameter interface{}) (interface{}, error)
visitMatchs(c context.Context, r interface{}, visit pathMatcher)
withPlainSelector(plainSelector) path
withAmbiguousSelector(ambiguousSelector) path
}
type plainPath []plainSelector
type ambiguousMatcher func(key, v interface{})
func (p plainPath) evaluate(ctx context.Context, root interface{}) (interface{}, error) {
return p.evaluatePath(ctx, root, root)
}
func (p plainPath) evaluatePath(ctx context.Context, root, value interface{}) (interface{}, error) {
var err error
for _, sel := range p {
value, err = sel(ctx, root, value)
if err != nil {
return nil, err
}
}
return value, nil
}
func (p plainPath) matcher(ctx context.Context, r interface{}, match ambiguousMatcher) ambiguousMatcher {
if len(p) == 0 {
return match
}
return func(k, v interface{}) {
res, err := p.evaluatePath(ctx, r, v)
if err == nil {
match(k, res)
}
}
}
func (p plainPath) visitMatchs(ctx context.Context, r interface{}, visit pathMatcher) {
res, err := p.evaluatePath(ctx, r, r)
if err == nil {
visit(nil, res)
}
}
func (p plainPath) withPlainSelector(selector plainSelector) path {
return append(p, selector)
}
func (p plainPath) withAmbiguousSelector(selector ambiguousSelector) path {
return &ambiguousPath{
parent: p,
branch: selector,
}
}
type ambiguousPath struct {
parent path
branch ambiguousSelector
ending plainPath
}
func (p *ambiguousPath) evaluate(ctx context.Context, parameter interface{}) (interface{}, error) {
matchs := []interface{}{}
p.visitMatchs(ctx, parameter, func(keys []interface{}, match interface{}) {
matchs = append(matchs, match)
})
return matchs, nil
}
func (p *ambiguousPath) visitMatchs(ctx context.Context, r interface{}, visit pathMatcher) {
p.parent.visitMatchs(ctx, r, func(keys []interface{}, v interface{}) {
p.branch(ctx, r, v, p.ending.matcher(ctx, r, visit.matcher(keys)))
})
}
func (p *ambiguousPath) branchMatcher(ctx context.Context, r interface{}, m ambiguousMatcher) ambiguousMatcher {
return func(k, v interface{}) {
p.branch(ctx, r, v, m)
}
}
func (p *ambiguousPath) withPlainSelector(selector plainSelector) path {
p.ending = append(p.ending, selector)
return p
}
func (p *ambiguousPath) withAmbiguousSelector(selector ambiguousSelector) path {
return &ambiguousPath{
parent: p,
branch: selector,
}
}
type pathMatcher func(keys []interface{}, match interface{})
func (m pathMatcher) matcher(keys []interface{}) ambiguousMatcher {
return func(key, match interface{}) {
m(append(keys, key), match)
}
}