-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsonpath.go
59 lines (50 loc) · 1.7 KB
/
jsonpath.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
package jsonpath
import (
"github.com/evilmonkeyinc/jsonpath/script/standard"
"github.com/evilmonkeyinc/jsonpath/token"
)
// Compile will compile the JSONPath selector
func Compile(selector string, options ...Option) (*Selector, error) {
jsonPath := &Selector{
selector: selector,
}
for _, option := range options {
if err := option.Apply(jsonPath); err != nil {
return nil, err
}
}
// Set defaults if options were not used
if jsonPath.engine == nil {
jsonPath.engine = new(standard.ScriptEngine)
}
tokenStrings, err := token.Tokenize(jsonPath.selector)
if err != nil {
return nil, getInvalidJSONPathSelectorWithReason(selector, err)
}
tokens := make([]token.Token, len(tokenStrings))
for idx, tokenString := range tokenStrings {
token, err := token.Parse(tokenString, jsonPath.engine, jsonPath.Options)
if err != nil {
return nil, getInvalidJSONPathSelectorWithReason(selector, err)
}
tokens[idx] = token
}
jsonPath.tokens = tokens
return jsonPath, nil
}
// Query will return the result of the JSONPath selector applied against the specified JSON data.
func Query(selector string, jsonData interface{}, options ...Option) (interface{}, error) {
jsonPath, err := Compile(selector, options...)
if err != nil {
return nil, getInvalidJSONPathSelectorWithReason(selector, err)
}
return jsonPath.Query(jsonData)
}
// QueryString will return the result of the JSONPath selector applied against the specified JSON data.
func QueryString(selector string, jsonData string, options ...Option) (interface{}, error) {
jsonPath, err := Compile(selector, options...)
if err != nil {
return nil, getInvalidJSONPathSelectorWithReason(selector, err)
}
return jsonPath.QueryString(jsonData)
}