From 2d43983b4ac6d78ea2e64f2fea5342224d3ca397 Mon Sep 17 00:00:00 2001 From: Ted Chen Date: Tue, 27 Jan 2015 21:05:21 -0400 Subject: [PATCH] Added template function where --- README.md | 1 + template.go | 13 +++++++++++++ template_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/README.md b/README.md index 204b4b34..c0209be5 100644 --- a/README.md +++ b/README.md @@ -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. === diff --git a/template.go b/template.go index f7aa0133..a5126258 100644 --- a/template.go +++ b/template.go @@ -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) @@ -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) diff --git a/template_test.go b/template_test.go index 534ee944..f901687a 100644 --- a/template_test.go +++ b/template_test.go @@ -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"