Skip to content

Commit 77ddbbd

Browse files
committed
Use generalizedWhere for all where* functions
1 parent e71c67e commit 77ddbbd

File tree

1 file changed

+32
-41
lines changed

1 file changed

+32
-41
lines changed

template.go

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func groupByKeys(entries []*RuntimeContainer, key string) []string {
6666
return ret
6767
}
6868

69-
// selects entries based on key
70-
func where(entries interface{}, key string, cmp interface{}) (interface{}, error) {
69+
// Generalized where function
70+
func generalizedWhere(entries interface{}, key string, test func(interface{}) bool) (interface{}, error) {
7171
entriesVal := reflect.ValueOf(entries)
7272

7373
switch entriesVal.Kind() {
@@ -82,67 +82,58 @@ func where(entries interface{}, key string, cmp interface{}) (interface{}, error
8282
v := reflect.Indirect(entriesVal.Index(i)).Interface()
8383

8484
value := deepGet(v, key)
85-
if reflect.DeepEqual(value, cmp) {
85+
if test(value) {
8686
selection = append(selection, v)
8787
}
8888
}
8989

9090
return selection, nil
9191
}
9292

93+
// selects entries based on key
94+
func where(entries interface{}, key string, cmp interface{}) (interface{}, error) {
95+
return generalizedWhere(entries, key, func(value interface{}) bool {
96+
return reflect.DeepEqual(value, cmp)
97+
})
98+
}
99+
93100
// selects entries where a key exists
94-
func whereExist(entries []*RuntimeContainer, key string) []*RuntimeContainer {
95-
selection := []*RuntimeContainer{}
96-
for _, v := range entries {
97-
value := deepGet(*v, key)
98-
if value != nil {
99-
selection = append(selection, v)
100-
}
101-
}
102-
return selection
101+
func whereExist(entries interface{}, key string) (interface{}, error) {
102+
return generalizedWhere(entries, key, func(value interface{}) bool {
103+
return value != nil
104+
})
103105
}
104106

105107
// selects entries where a key does not exist
106-
func whereNotExist(entries []*RuntimeContainer, key string) []*RuntimeContainer {
107-
selection := []*RuntimeContainer{}
108-
for _, v := range entries {
109-
value := deepGet(*v, key)
110-
if value == nil {
111-
selection = append(selection, v)
112-
}
113-
}
114-
return selection
108+
func whereNotExist(entries interface{}, key string) (interface{}, error) {
109+
return generalizedWhere(entries, key, func(value interface{}) bool {
110+
return value == nil
111+
})
115112
}
116113

117114
// selects entries based on key. Assumes key is delimited and breaks it apart before comparing
118-
func whereAny(entries []*RuntimeContainer, key, sep string, cmp []string) []*RuntimeContainer {
119-
selection := []*RuntimeContainer{}
120-
for _, v := range entries {
121-
value := deepGet(*v, key)
122-
if value != nil {
115+
func whereAny(entries interface{}, key, sep string, cmp []string) (interface{}, error) {
116+
return generalizedWhere(entries, key, func(value interface{}) bool {
117+
if value == nil {
118+
return false
119+
} else {
123120
items := strings.Split(value.(string), sep)
124-
if len(intersect(cmp, items)) > 0 {
125-
selection = append(selection, v)
126-
}
121+
return len(intersect(cmp, items)) > 0
127122
}
128-
}
129-
return selection
123+
})
130124
}
131125

132126
// selects entries based on key. Assumes key is delimited and breaks it apart before comparing
133-
func whereAll(entries []*RuntimeContainer, key, sep string, cmp []string) []*RuntimeContainer {
134-
selection := []*RuntimeContainer{}
127+
func whereAll(entries interface{}, key, sep string, cmp []string) (interface{}, error) {
135128
req_count := len(cmp)
136-
for _, v := range entries {
137-
value := deepGet(*v, key)
138-
if value != nil {
129+
return generalizedWhere(entries, key, func(value interface{}) bool {
130+
if value == nil {
131+
return false
132+
} else {
139133
items := strings.Split(value.(string), sep)
140-
if len(intersect(cmp, items)) == req_count {
141-
selection = append(selection, v)
142-
}
134+
return len(intersect(cmp, items)) == req_count
143135
}
144-
}
145-
return selection
136+
})
146137
}
147138

148139
// hasPrefix returns whether a given string is a prefix of another string

0 commit comments

Comments
 (0)