]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package jmespath |
2 | ||
3 | import "strconv" | |
4 | ||
107c1cdb | 5 | // JMESPath is the epresentation of a compiled JMES path query. A JMESPath is |
bae9f6d2 JC |
6 | // safe for concurrent use by multiple goroutines. |
7 | type JMESPath struct { | |
8 | ast ASTNode | |
9 | intr *treeInterpreter | |
10 | } | |
11 | ||
12 | // Compile parses a JMESPath expression and returns, if successful, a JMESPath | |
13 | // object that can be used to match against data. | |
14 | func Compile(expression string) (*JMESPath, error) { | |
15 | parser := NewParser() | |
16 | ast, err := parser.Parse(expression) | |
17 | if err != nil { | |
18 | return nil, err | |
19 | } | |
20 | jmespath := &JMESPath{ast: ast, intr: newInterpreter()} | |
21 | return jmespath, nil | |
22 | } | |
23 | ||
24 | // MustCompile is like Compile but panics if the expression cannot be parsed. | |
25 | // It simplifies safe initialization of global variables holding compiled | |
26 | // JMESPaths. | |
27 | func MustCompile(expression string) *JMESPath { | |
28 | jmespath, err := Compile(expression) | |
29 | if err != nil { | |
30 | panic(`jmespath: Compile(` + strconv.Quote(expression) + `): ` + err.Error()) | |
31 | } | |
32 | return jmespath | |
33 | } | |
34 | ||
35 | // Search evaluates a JMESPath expression against input data and returns the result. | |
36 | func (jp *JMESPath) Search(data interface{}) (interface{}, error) { | |
37 | return jp.intr.Execute(jp.ast, data) | |
38 | } | |
39 | ||
40 | // Search evaluates a JMESPath expression against input data and returns the result. | |
41 | func Search(expression string, data interface{}) (interface{}, error) { | |
42 | intr := newInterpreter() | |
43 | parser := NewParser() | |
44 | ast, err := parser.Parse(expression) | |
45 | if err != nil { | |
46 | return nil, err | |
47 | } | |
48 | return intr.Execute(ast, data) | |
49 | } |