Skip to content

Commit af3c843

Browse files
Ted Chenjwilder
Ted Chen
authored andcommitted
Added template function whereSomeMatch
1 parent 2d43983 commit af3c843

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Within those templates, the object emitted by docker-gen will have [this structu
133133
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
134134
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
135135
* *`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.
136+
* *`whereSomeMatch $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.
136137

137138
===
138139

template.go

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

80+
// selects entries based on key. Assumes key is delimited and breaks it apart before comparing
81+
func whereSomeMatch(entries []*RuntimeContainer, key, sep string, cmp []string) []*RuntimeContainer {
82+
selection := []*RuntimeContainer{}
83+
for _, v := range entries {
84+
value := deepGet(*v, key)
85+
if value != nil {
86+
items := strings.Split(value.(string), sep)
87+
if len(intersect(cmp, items)) > 0 {
88+
selection = append(selection, v)
89+
}
90+
}
91+
}
92+
return selection
93+
}
94+
8095
// hasPrefix returns whether a given string is a prefix of another string
8196
func hasPrefix(prefix, s string) bool {
8297
return strings.HasPrefix(s, prefix)
@@ -253,6 +268,7 @@ func generateFile(config Config, containers Context) bool {
253268
"trimPrefix": trimPrefix,
254269
"trimSuffix": trimSuffix,
255270
"where": where,
271+
"whereSomeMatch": whereSomeMatch,
256272
}).ParseFiles(templatePath)
257273
if err != nil {
258274
log.Fatalf("unable to parse template: %s", err)

template_test.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,64 @@ func TestWhere(t *testing.T) {
205205
}
206206

207207
if len(where(containers, "Env.VIRTUAL_HOST", "demo1.localhost")) != 1 {
208-
t.Fatalf("expected 1 match")
208+
t.Fatalf("demo1.localhost expected 1 match")
209209
}
210210

211211
if len(where(containers, "Env.VIRTUAL_HOST", "demo2.localhost")) != 2 {
212-
t.Fatalf("expected 2 matches")
212+
t.Fatalf("demo2.localhost expected 2 matches")
213213
}
214214

215215
if len(where(containers, "Env.VIRTUAL_HOST", "demo3.localhost")) != 1 {
216-
t.Fatalf("expected 1 match")
216+
t.Fatalf("demo3.localhost expected 1 match")
217217
}
218218

219219
if len(where(containers, "Env.NOEXIST", "demo3.localhost")) != 0 {
220-
t.Fatalf("expected 0 match")
220+
t.Fatalf("NOEXIST demo3.localhost expected 0 match")
221+
}
222+
}
223+
224+
func TestWhereSomeMatch(t *testing.T) {
225+
containers := []*RuntimeContainer{
226+
&RuntimeContainer{
227+
Env: map[string]string{
228+
"VIRTUAL_HOST": "demo1.localhost",
229+
},
230+
ID: "1",
231+
},
232+
&RuntimeContainer{
233+
Env: map[string]string{
234+
"VIRTUAL_HOST": "demo2.localhost,demo4.localhost",
235+
},
236+
ID: "2",
237+
},
238+
&RuntimeContainer{
239+
Env: map[string]string{
240+
"VIRTUAL_HOST": "bar,demo3.localhost,foo",
241+
},
242+
ID: "3",
243+
},
244+
&RuntimeContainer{
245+
Env: map[string]string{
246+
"VIRTUAL_HOST": "demo2.localhost",
247+
},
248+
ID: "4",
249+
},
250+
}
251+
252+
if len(whereSomeMatch(containers, "Env.VIRTUAL_HOST", ",", []string{"demo1.localhost"})) != 1 {
253+
t.Fatalf("demo1.localhost expected 1 match")
254+
}
255+
256+
if len(whereSomeMatch(containers, "Env.VIRTUAL_HOST", ",", []string{"demo2.localhost", "lala"})) != 2 {
257+
t.Fatalf("demo2.localhost expected 2 matches")
258+
}
259+
260+
if len(whereSomeMatch(containers, "Env.VIRTUAL_HOST", ",", []string{"something", "demo3.localhost"})) != 1 {
261+
t.Fatalf("demo3.localhost expected 1 match")
262+
}
263+
264+
if len(whereSomeMatch(containers, "Env.NOEXIST", ",", []string{"demo3.localhost"})) != 0 {
265+
t.Fatalf("NOEXIST demo3.localhost expected 0 match")
221266
}
222267
}
223268

0 commit comments

Comments
 (0)