-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjsonpath.go
More file actions
36 lines (31 loc) · 813 Bytes
/
jsonpath.go
File metadata and controls
36 lines (31 loc) · 813 Bytes
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
package jsonpath
import (
"fmt"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath/config"
"github.com/speakeasy-api/jsonpath/pkg/jsonpath/token"
"gopkg.in/yaml.v3"
)
func NewPath(input string, opts ...config.Option) (*JSONPath, error) {
tokenizer := token.NewTokenizer(input, opts...)
tokens := tokenizer.Tokenize()
for i := 0; i < len(tokens); i++ {
if tokens[i].Token == token.ILLEGAL {
return nil, fmt.Errorf(tokenizer.ErrorString(&tokens[i], "unexpected token"))
}
}
parser := newParserPrivate(tokenizer, tokens, opts...)
err := parser.parse()
if err != nil {
return nil, err
}
return parser, nil
}
func (p *JSONPath) Query(root *yaml.Node) []*yaml.Node {
return p.ast.Query(root, root)
}
func (p *JSONPath) String() string {
if p == nil {
return ""
}
return p.ast.ToString()
}