Skip to content

Commit 2d43983

Browse files
Ted Chenjwilder
Ted Chen
authored andcommitted
Added template function where
1 parent a38bb03 commit 2d43983

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Within those templates, the object emitted by docker-gen will have [this structu
132132
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)
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.
135+
* *`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.
135136

136137
===
137138

template.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ func groupByKeys(entries []*RuntimeContainer, key string) []string {
6565
return ret
6666
}
6767

68+
// selects entries based on key
69+
func where(entries []*RuntimeContainer, key string, cmp string) []*RuntimeContainer {
70+
selection := []*RuntimeContainer{}
71+
for _, v := range entries {
72+
value := deepGet(*v, key)
73+
if value == cmp {
74+
selection = append(selection, v)
75+
}
76+
}
77+
return selection
78+
}
79+
6880
// hasPrefix returns whether a given string is a prefix of another string
6981
func hasPrefix(prefix, s string) bool {
7082
return strings.HasPrefix(s, prefix)
@@ -240,6 +252,7 @@ func generateFile(config Config, containers Context) bool {
240252
"split": strings.Split,
241253
"trimPrefix": trimPrefix,
242254
"trimSuffix": trimSuffix,
255+
"where": where,
243256
}).ParseFiles(templatePath)
244257
if err != nil {
245258
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
@@ -176,6 +176,51 @@ func TestGroupByMulti(t *testing.T) {
176176
}
177177
}
178178

179+
func TestWhere(t *testing.T) {
180+
containers := []*RuntimeContainer{
181+
&RuntimeContainer{
182+
Env: map[string]string{
183+
"VIRTUAL_HOST": "demo1.localhost",
184+
},
185+
ID: "1",
186+
},
187+
&RuntimeContainer{
188+
Env: map[string]string{
189+
"VIRTUAL_HOST": "demo2.localhost",
190+
},
191+
ID: "2",
192+
},
193+
&RuntimeContainer{
194+
Env: map[string]string{
195+
"VIRTUAL_HOST": "demo3.localhost",
196+
},
197+
ID: "3",
198+
},
199+
&RuntimeContainer{
200+
Env: map[string]string{
201+
"VIRTUAL_HOST": "demo2.localhost",
202+
},
203+
ID: "4",
204+
},
205+
}
206+
207+
if len(where(containers, "Env.VIRTUAL_HOST", "demo1.localhost")) != 1 {
208+
t.Fatalf("expected 1 match")
209+
}
210+
211+
if len(where(containers, "Env.VIRTUAL_HOST", "demo2.localhost")) != 2 {
212+
t.Fatalf("expected 2 matches")
213+
}
214+
215+
if len(where(containers, "Env.VIRTUAL_HOST", "demo3.localhost")) != 1 {
216+
t.Fatalf("expected 1 match")
217+
}
218+
219+
if len(where(containers, "Env.NOEXIST", "demo3.localhost")) != 0 {
220+
t.Fatalf("expected 0 match")
221+
}
222+
}
223+
179224
func TestHasPrefix(t *testing.T) {
180225
const prefix = "tcp://"
181226
const str = "tcp://127.0.0.1:2375"

0 commit comments

Comments
 (0)