/
find-by-selector.go
131 lines (121 loc) · 3.28 KB
/
find-by-selector.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package json_traversal
import (
"github.com/mattfenwick/kube-utils/go/pkg/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"regexp"
"strconv"
)
type Selector struct {
IsGlob bool
IsArray bool
Key *string
}
type Result struct {
Path []*PathComponent
Value interface{}
}
func JsonFindBySelector(obj interface{}, selector []*Selector, context []*PathComponent) []*Result {
logrus.Debugf("JsonFindBySelector: %s", PathString(context))
if len(selector) == 0 {
return []*Result{{Path: context, Value: obj}}
}
next := selector[0]
var results []*Result
if next.IsGlob {
logrus.Debugf("searching under glob")
switch o := obj.(type) {
case []interface{}:
logrus.Debugf("searching under glob array")
for i, e := range o {
index := i
results = append(results,
JsonFindBySelector(e, selector[1:], append(context, NewArrayPathComponent(index)))...)
}
case map[string]interface{}:
logrus.Debugf("searching under glob map")
for k, v := range o {
results = append(results,
JsonFindBySelector(v, selector[1:], append(context, NewMapValuePathComponent(k)))...)
}
default:
panic(errors.Errorf("can only glob slice or map, found %T", o))
}
} else {
logrus.Debugf("searching under key")
switch o := obj.(type) {
case []interface{}:
if next.IsArray {
logrus.Debugf("searching under array")
index64, err := strconv.ParseInt(*next.Key, 10, 32)
index := int(index64)
utils.DoOrDie(errors.Wrapf(err, "unable to ParseInt from %s", *next.Key))
if index < len(o) {
results = append(results,
JsonFindBySelector(o[index], selector[1:], append(context, NewArrayPathComponent(index)))...)
}
}
case map[string]interface{}:
if !next.IsArray {
logrus.Debugf("searching under map")
v, ok := o[*next.Key]
logrus.Debugf("key '%s' in map? %t", *next.Key, ok)
if ok {
results = append(results,
JsonFindBySelector(v, selector[1:], append(context, NewMapValuePathComponent(*next.Key)))...)
} else {
logrus.Infof("did not find key %s; keys: %+v", *next.Key, maps.Keys(o))
}
for k := range o {
logrus.Debugf("key in map: %s", k)
}
}
default:
logrus.Debugf("skipping type %T", o)
}
}
return results
}
type KeyMatch struct {
Path []*PathComponent
Value string
}
func (k *KeyMatch) PathString() []string {
return PathString(k.Path)
}
func JsonFindByRegex(obj interface{}, path []*PathComponent, re *regexp.Regexp) []*KeyMatch {
switch o := obj.(type) {
case string:
if re.FindString(o) != "" {
return []*KeyMatch{{
Path: path,
Value: o,
}}
}
return nil
case []interface{}:
var matches []*KeyMatch
for i, e := range o {
index := i
matches = append(matches, JsonFindByRegex(e, append(path, &PathComponent{ArrayIndex: &index}), re)...)
}
return matches
case map[string]interface{}:
var matches []*KeyMatch
for k, v := range o {
key := k
if re.FindString(k) != "" {
matches = append(matches, &KeyMatch{
Path: append(path, &PathComponent{MapKey: &key}),
Value: key,
})
}
matches = append(matches, JsonFindByRegex(v, append(path, &PathComponent{MapValue: &key}), re)...)
}
return matches
default:
logrus.Tracef("nothing to find: %T (path: %+v)", o, PathString(path))
return nil
}
}