-
Notifications
You must be signed in to change notification settings - Fork 288
/
json_path.go
45 lines (38 loc) · 1.23 KB
/
json_path.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
package k8s
import (
"bytes"
"strings"
"k8s.io/client-go/util/jsonpath"
)
// This is just a wrapper around k8s jsonpath, mostly because k8s jsonpath doesn't produce errors containing
// the problematic path
// (and as long as we're here, deal with some of its other annoyances, like taking a "name" that doesn't do anything,
// and having a separate "Parse" step to make an instance actually useful, and making you use an io.Writer, and
// wrapping string results in quotes)
type JSONPath struct {
jp *jsonpath.JSONPath
path string
}
func NewJSONPath(s string) (JSONPath, error) {
jp := jsonpath.New("jp")
err := jp.Parse(s)
if err != nil {
return JSONPath{}, err
}
return JSONPath{jp, s}, nil
}
// Gets the value at the specified path
// NB: currently strips away surrounding quotes, which the underlying parser includes in its return value
// If, at some point, we want to distinguish between, e.g., ints and strings by the presence of quotes, this
// will need to be revisited.
func (jp JSONPath) Execute(obj interface{}) (string, error) {
buf := &bytes.Buffer{}
err := jp.jp.Execute(buf, obj)
if err != nil {
return "", err
}
return strings.Trim(buf.String(), "\""), nil
}
func (jp JSONPath) String() string {
return jp.path
}