@@ -66,8 +66,8 @@ func groupByKeys(entries []*RuntimeContainer, key string) []string {
66
66
return ret
67
67
}
68
68
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 ) {
71
71
entriesVal := reflect .ValueOf (entries )
72
72
73
73
switch entriesVal .Kind () {
@@ -82,67 +82,58 @@ func where(entries interface{}, key string, cmp interface{}) (interface{}, error
82
82
v := reflect .Indirect (entriesVal .Index (i )).Interface ()
83
83
84
84
value := deepGet (v , key )
85
- if reflect . DeepEqual (value , cmp ) {
85
+ if test (value ) {
86
86
selection = append (selection , v )
87
87
}
88
88
}
89
89
90
90
return selection , nil
91
91
}
92
92
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
+
93
100
// 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
+ })
103
105
}
104
106
105
107
// 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
+ })
115
112
}
116
113
117
114
// 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 {
123
120
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
127
122
}
128
- }
129
- return selection
123
+ })
130
124
}
131
125
132
126
// 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 ) {
135
128
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 {
139
133
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
143
135
}
144
- }
145
- return selection
136
+ })
146
137
}
147
138
148
139
// hasPrefix returns whether a given string is a prefix of another string
0 commit comments