forked from PaesslerAG/jsonpath
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
106 lines (91 loc) · 2.19 KB
/
parser.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
package jsonpath
import (
"context"
"github.com/PaesslerAG/gval"
)
type parser struct {
*gval.Parser
single
multis multis
}
//multi evaluate wildcard
type multi func(c context.Context, r, v interface{}, m match)
type multis []multi
type match func(key, v interface{})
//single evaluate exactly one result
type single func(c context.Context, r, v interface{}) (interface{}, error)
func (p *parser) newSingleStage(next single) {
if p.single == nil {
p.single = next
return
}
last := p.single
p.single = func(c context.Context, r, v interface{}) (interface{}, error) {
v, err := last(c, r, v)
if err != nil {
return nil, err
}
return next(c, r, v)
}
}
func (p *parser) newMultiStage(next multi) {
if p.single != nil {
s := p.single
p.single = nil
p.multis = append(p.multis, func(c context.Context, r, v interface{}, m match) {
v, err := s(c, r, v)
if err != nil {
return
}
next(c, r, v, m)
})
return
}
p.multis = append(p.multis, next)
}
func (p *parser) evaluable() gval.Evaluable {
if p.multis == nil {
return p.single.evaluable
}
return p.getMultis().evaluable
}
func (p *parser) getMultis() multis {
last := len(p.multis) - 1
if p.single != nil {
p.multis[last] = p.multis[last].append(p.single)
p.single = nil
}
return p.multis
}
func (ms multis) visitMatchs(c context.Context, r, v interface{}, visit func(keys []interface{}, match interface{})) {
if len(ms) == 0 {
visit(nil, v)
return
}
ms[0](c, r, v, func(key, v interface{}) {
ms[1:].visitMatchs(c, r, v, func(keys []interface{}, match interface{}) {
visit(append([]interface{}{key}, keys...), match)
})
})
}
func (ms multis) evaluable(c context.Context, v interface{}) (interface{}, error) {
matchs := []interface{}{}
ms.visitMatchs(c, v, v, func(keys []interface{}, match interface{}) {
matchs = append(matchs, match)
})
return matchs, nil
}
func (s single) evaluable(c context.Context, v interface{}) (interface{}, error) {
return s(c, v, v)
}
func (multi multi) append(s single) multi {
return func(c context.Context, r, v interface{}, m match) {
multi(c, r, v, func(key, v interface{}) {
v, err := s(c, r, v)
if err != nil {
return
}
m(key, v)
})
}
}