Skip to content

Commit bbdeeb9

Browse files
Ted Chenjwilder
Ted Chen
authored andcommitted
Added template function whereRequires
1 parent af3c843 commit bbdeeb9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Within those templates, the object emitted by docker-gen will have [this structu
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.
136136
* *`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.
137+
* *`whereRequires $containers $fieldPath $sep $values`*: Like `whereSomeMatch`, except all `$values` must exist in the `$fieldPath`.
137138

138139
===
139140

template.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ func whereSomeMatch(entries []*RuntimeContainer, key, sep string, cmp []string)
9292
return selection
9393
}
9494

95+
// selects entries based on key. Assumes key is delimited and breaks it apart before comparing
96+
func whereRequires(entries []*RuntimeContainer, key, sep string, cmp []string) []*RuntimeContainer {
97+
selection := []*RuntimeContainer{}
98+
req_count := len(cmp)
99+
for _, v := range entries {
100+
value := deepGet(*v, key)
101+
if value != nil {
102+
items := strings.Split(value.(string), sep)
103+
if len(intersect(cmp, items)) == req_count {
104+
selection = append(selection, v)
105+
}
106+
}
107+
}
108+
return selection
109+
}
110+
95111
// hasPrefix returns whether a given string is a prefix of another string
96112
func hasPrefix(prefix, s string) bool {
97113
return strings.HasPrefix(s, prefix)
@@ -269,6 +285,7 @@ func generateFile(config Config, containers Context) bool {
269285
"trimSuffix": trimSuffix,
270286
"where": where,
271287
"whereSomeMatch": whereSomeMatch,
288+
"whereRequires": whereRequires,
272289
}).ParseFiles(templatePath)
273290
if err != nil {
274291
log.Fatalf("unable to parse template: %s", err)

template_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,51 @@ func TestWhereSomeMatch(t *testing.T) {
266266
}
267267
}
268268

269+
func TestWhereRequires(t *testing.T) {
270+
containers := []*RuntimeContainer{
271+
&RuntimeContainer{
272+
Env: map[string]string{
273+
"VIRTUAL_HOST": "demo1.localhost",
274+
},
275+
ID: "1",
276+
},
277+
&RuntimeContainer{
278+
Env: map[string]string{
279+
"VIRTUAL_HOST": "demo2.localhost,demo4.localhost",
280+
},
281+
ID: "2",
282+
},
283+
&RuntimeContainer{
284+
Env: map[string]string{
285+
"VIRTUAL_HOST": "bar,demo3.localhost,foo",
286+
},
287+
ID: "3",
288+
},
289+
&RuntimeContainer{
290+
Env: map[string]string{
291+
"VIRTUAL_HOST": "demo2.localhost",
292+
},
293+
ID: "4",
294+
},
295+
}
296+
297+
if len(whereRequires(containers, "Env.VIRTUAL_HOST", ",", []string{"demo1.localhost"})) != 1 {
298+
t.Fatalf("demo1.localhost expected 1 match")
299+
}
300+
301+
if len(whereRequires(containers, "Env.VIRTUAL_HOST", ",", []string{"demo2.localhost", "lala"})) != 0 {
302+
t.Fatalf("demo2.localhost,lala expected 0 matches")
303+
}
304+
305+
if len(whereRequires(containers, "Env.VIRTUAL_HOST", ",", []string{"demo3.localhost"})) != 1 {
306+
t.Fatalf("demo3.localhost expected 1 match")
307+
}
308+
309+
if len(whereRequires(containers, "Env.NOEXIST", ",", []string{"demo3.localhost"})) != 0 {
310+
t.Fatalf("NOEXIST demo3.localhost expected 0 match")
311+
}
312+
}
313+
269314
func TestHasPrefix(t *testing.T) {
270315
const prefix = "tcp://"
271316
const str = "tcp://127.0.0.1:2375"

0 commit comments

Comments
 (0)