Skip to content

Commit b2740d7

Browse files
author
Eric Hayes
committed
Added whereExist and whereNotExist. Updated tests.
1 parent 4def90a commit b2740d7

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ Within those templates, the object emitted by docker-gen will have [this structu
135135
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
136136
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
137137
* *`where $containers $fieldPath $value`*: Filters an array of `RuntimeContainer` instances based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value, which must be a string. Returns an array of containers having that value.
138+
* *`whereExist $containers $fieldPath`*: Like `where`, but returns only containers where `$fieldPath` exists (is not nil).
139+
* *`whereNotExist $containers $fieldPath`*: Like `where`, but returns only containers where `$fieldPath` does not exist (is nil).
138140
* *`whereAny $containers $fieldPath $sep $values`*: Like `where`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. The comparison value is a string slice with possible matches. Returns containers which OR intersect these values.
139141
* *`whereAll $containers $fieldPath $sep $values`*: Like `whereAny`, except all `$values` must exist in the `$fieldPath`.
140142

template.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ func where(entries []*RuntimeContainer, key string, cmp string) []*RuntimeContai
7777
return selection
7878
}
7979

80+
// selects entries where a key exists
81+
func whereExist(entries []*RuntimeContainer, key string) []*RuntimeContainer {
82+
selection := []*RuntimeContainer{}
83+
for _, v := range entries {
84+
value := deepGet(*v, key)
85+
if value != nil {
86+
selection = append(selection, v)
87+
}
88+
}
89+
return selection
90+
}
91+
92+
// selects entries where a key does not exist
93+
func whereNotExist(entries []*RuntimeContainer, key string) []*RuntimeContainer {
94+
selection := []*RuntimeContainer{}
95+
for _, v := range entries {
96+
value := deepGet(*v, key)
97+
if value == nil {
98+
selection = append(selection, v)
99+
}
100+
}
101+
return selection
102+
}
103+
80104
// selects entries based on key. Assumes key is delimited and breaks it apart before comparing
81105
func whereAny(entries []*RuntimeContainer, key, sep string, cmp []string) []*RuntimeContainer {
82106
selection := []*RuntimeContainer{}
@@ -284,6 +308,8 @@ func generateFile(config Config, containers Context) bool {
284308
"trimPrefix": trimPrefix,
285309
"trimSuffix": trimSuffix,
286310
"where": where,
311+
"whereExist": whereExist,
312+
"whereNotExist": whereNotExist,
287313
"whereAny": whereAny,
288314
"whereAll": whereAll,
289315
}).ParseFiles(templatePath)

template_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,100 @@ func TestWhere(t *testing.T) {
221221
}
222222
}
223223

224+
func TestWhereExist(t *testing.T) {
225+
containers := []*RuntimeContainer{
226+
&RuntimeContainer{
227+
Env: map[string]string{
228+
"VIRTUAL_HOST": "demo1.localhost",
229+
"VIRTUAL_PATH": "/api",
230+
},
231+
ID: "1",
232+
},
233+
&RuntimeContainer{
234+
Env: map[string]string{
235+
"VIRTUAL_HOST": "demo2.localhost",
236+
},
237+
ID: "2",
238+
},
239+
&RuntimeContainer{
240+
Env: map[string]string{
241+
"VIRTUAL_HOST": "demo3.localhost",
242+
"VIRTUAL_PATH": "/api",
243+
},
244+
ID: "3",
245+
},
246+
&RuntimeContainer{
247+
Env: map[string]string{
248+
"VIRTUAL_PROTO": "https",
249+
},
250+
ID: "4",
251+
},
252+
}
253+
254+
if len(whereExist(containers, "Env.VIRTUAL_HOST")) != 3 {
255+
t.Fatalf("Env.VIRTUAL_HOST expected 3 matches")
256+
}
257+
258+
if len(whereExist(containers, "Env.VIRTUAL_PATH")) != 2 {
259+
t.Fatalf("Env.VIRTUAL_PATH expected 2 matches")
260+
}
261+
262+
if len(whereExist(containers, "Env.NOT_A_KEY")) != 0 {
263+
t.Fatalf("Env.NOT_A_KEY expected 0 matches")
264+
}
265+
266+
if len(whereExist(containers, "Env.VIRTUAL_PROTO")) != 1 {
267+
t.Fatalf("Env.VIRTUAL_PROTO expected 1 matche")
268+
}
269+
}
270+
271+
func TestWhereNotExist(t *testing.T) {
272+
containers := []*RuntimeContainer{
273+
&RuntimeContainer{
274+
Env: map[string]string{
275+
"VIRTUAL_HOST": "demo1.localhost",
276+
"VIRTUAL_PATH": "/api",
277+
},
278+
ID: "1",
279+
},
280+
&RuntimeContainer{
281+
Env: map[string]string{
282+
"VIRTUAL_HOST": "demo2.localhost",
283+
},
284+
ID: "2",
285+
},
286+
&RuntimeContainer{
287+
Env: map[string]string{
288+
"VIRTUAL_HOST": "demo3.localhost",
289+
"VIRTUAL_PATH": "/api",
290+
},
291+
ID: "3",
292+
},
293+
&RuntimeContainer{
294+
Env: map[string]string{
295+
"VIRTUAL_PROTO": "https",
296+
},
297+
ID: "4",
298+
},
299+
}
300+
301+
if len(whereNotExist(containers, "Env.VIRTUAL_HOST")) != 1 {
302+
t.Fatalf("Env.VIRTUAL_HOST expected 1 match")
303+
}
304+
305+
if len(whereNotExist(containers, "Env.VIRTUAL_PATH")) != 2 {
306+
t.Fatalf("Env.VIRTUAL_PATH expected 2 matches")
307+
}
308+
309+
if len(whereNotExist(containers, "Env.NOT_A_KEY")) != 4 {
310+
t.Fatalf("Env.NOT_A_KEY expected 4 matches")
311+
}
312+
313+
if len(whereNotExist(containers, "Env.VIRTUAL_PROTO")) != 3 {
314+
t.Fatalf("Env.VIRTUAL_PROTO expected 3 matches")
315+
}
316+
}
317+
224318
func TestWhereSomeMatch(t *testing.T) {
225319
containers := []*RuntimeContainer{
226320
&RuntimeContainer{

0 commit comments

Comments
 (0)