-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpath.go
164 lines (138 loc) · 4.31 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
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
package jsonpath
import (
"bytes"
"context"
)
// Exported for testing purposes
type CollectFullPathsContextKey struct{}
type path interface {
evaluate(c context.Context, parameter interface{}) (interface{}, error)
evaluateWithPaths(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) {
_, value, err := p.evaluatePath(ctx, root, root)
return value, err
}
func (p plainPath) evaluateWithPaths(ctx context.Context, root interface{}) (interface{}, error) {
keys, value, err := p.evaluatePath(ctx, root, root)
m := map[string]interface{}{}
m[toJSONPath(keys)] = value
return m, err
}
func (p plainPath) evaluatePath(ctx context.Context, root, value interface{}) ([]interface{}, interface{}, error) {
keys := []interface{}{}
for _, sel := range p {
k, v, err := sel(ctx, root, value)
if err != nil {
return nil, nil, err
}
if k != nil {
keys = append(keys, k)
}
value = v
}
return keys, 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{}) {
keys := k
collectFullPaths := ctx.Value(CollectFullPathsContextKey{})
ks, res, err := p.evaluatePath(ctx, r, v)
if b, ok := collectFullPaths.(bool); ok && b {
keys = append(ks, k)
}
if err == nil {
match(keys, res)
}
}
}
func (p plainPath) visitMatchs(ctx context.Context, r interface{}, visit pathMatcher) {
keys, res, err := p.evaluatePath(ctx, r, r)
if err == nil {
visit(keys, 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) evaluateWithPaths(ctx context.Context, parameter interface{}) (interface{}, error) {
m := map[string]interface{}{}
p.visitMatchs(ctx, parameter, func(keys []interface{}, match interface{}) {
m[toJSONPath(convertPath(keys))] = match
})
return m, 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)
}
}
func convertPath(segments []interface{}) []interface{} {
paths := []interface{}{}
sCount := len(segments)
for i, sRaw := range segments {
if s, ok := sRaw.([]interface{}); ok {
liIndex := len(s) - 1
if (liIndex >= 0 && i == sCount-1) || (i > 0 && liIndex < sCount) {
s = convertPath(append([]interface{}{s[liIndex]}, s[:liIndex]...))
}
paths = append(paths, convertPath(s)...)
} else {
paths = append(paths, sRaw)
}
}
return paths
}
func toJSONPath(segments []interface{}) string {
sb := bytes.Buffer{}
sb.WriteString("$")
quoteWildcardValues(&sb, convertPath(segments))
return sb.String()
}