Skip to content

Commit

Permalink
Added template function where
Browse files Browse the repository at this point in the history
  • Loading branch information
datacoda authored and jwilder committed Jan 28, 2015
1 parent a38bb03 commit 2d43983
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Within those templates, the object emitted by docker-gen will have [this structu
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
* *`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.

===

Expand Down
13 changes: 13 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ func groupByKeys(entries []*RuntimeContainer, key string) []string {
return ret
}

// selects entries based on key
func where(entries []*RuntimeContainer, key string, cmp string) []*RuntimeContainer {
selection := []*RuntimeContainer{}
for _, v := range entries {
value := deepGet(*v, key)
if value == cmp {
selection = append(selection, v)
}
}
return selection
}

// hasPrefix returns whether a given string is a prefix of another string
func hasPrefix(prefix, s string) bool {
return strings.HasPrefix(s, prefix)
Expand Down Expand Up @@ -240,6 +252,7 @@ func generateFile(config Config, containers Context) bool {
"split": strings.Split,
"trimPrefix": trimPrefix,
"trimSuffix": trimSuffix,
"where": where,
}).ParseFiles(templatePath)
if err != nil {
log.Fatalf("unable to parse template: %s", err)
Expand Down
45 changes: 45 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,51 @@ func TestGroupByMulti(t *testing.T) {
}
}

func TestWhere(t *testing.T) {
containers := []*RuntimeContainer{
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo1.localhost",
},
ID: "1",
},
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo2.localhost",
},
ID: "2",
},
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo3.localhost",
},
ID: "3",
},
&RuntimeContainer{
Env: map[string]string{
"VIRTUAL_HOST": "demo2.localhost",
},
ID: "4",
},
}

if len(where(containers, "Env.VIRTUAL_HOST", "demo1.localhost")) != 1 {
t.Fatalf("expected 1 match")
}

if len(where(containers, "Env.VIRTUAL_HOST", "demo2.localhost")) != 2 {
t.Fatalf("expected 2 matches")
}

if len(where(containers, "Env.VIRTUAL_HOST", "demo3.localhost")) != 1 {
t.Fatalf("expected 1 match")
}

if len(where(containers, "Env.NOEXIST", "demo3.localhost")) != 0 {
t.Fatalf("expected 0 match")
}
}

func TestHasPrefix(t *testing.T) {
const prefix = "tcp://"
const str = "tcp://127.0.0.1:2375"
Expand Down

0 comments on commit 2d43983

Please sign in to comment.