forked from jmespath/go-jmespath
-
Notifications
You must be signed in to change notification settings - Fork 10
/
api.go
47 lines (41 loc) · 1.44 KB
/
api.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
package jmespath
import "strconv"
// JMESPath is the representation of a compiled JMES path query. A JMESPath is
// safe for concurrent use by multiple goroutines.
type JMESPath struct {
ast ASTNode
}
// Compile parses a JMESPath expression and returns, if successful, a JMESPath
// object that can be used to match against data.
func Compile(expression string) (*JMESPath, error) {
parser := NewParser()
ast, err := parser.Parse(expression)
if err != nil {
return nil, err
}
jmespath := &JMESPath{ast: ast}
return jmespath, nil
}
// MustCompile is like Compile but panics if the expression cannot be parsed.
// It simplifies safe initialization of global variables holding compiled
// JMESPaths.
func MustCompile(expression string) *JMESPath {
jmespath, err := Compile(expression)
if err != nil {
panic(`jmespath: Compile(` + strconv.Quote(expression) + `): ` + err.Error())
}
return jmespath
}
// Search evaluates a JMESPath expression against input data and returns the result.
func (jp *JMESPath) Search(data interface{}, opts ...InterpreterOption) (interface{}, error) {
intr := NewInterpreter()
return intr.Execute(jp.ast, data, opts...)
}
// Search evaluates a JMESPath expression against input data and returns the result.
func Search(expression string, data interface{}, opts ...InterpreterOption) (interface{}, error) {
compiled, err := Compile(expression)
if err != nil {
return nil, err
}
return compiled.Search(data, opts...)
}