Skip to content

Commit a38bb03

Browse files
Ted Chenjwilder
Ted Chen
authored andcommitted
Added template function intersect.
1 parent c09e451 commit a38bb03

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Within those templates, the object emitted by docker-gen will have [this structu
123123
* *`groupByMulti $containers $fieldPath $sep`*: Like `groupBy`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. A container whose `$fieldPath` value contains a list of strings will show up in the map output under each of those strings.
124124
* *`hasPrefix $prefix $string`*: Returns whether `$prefix` is a prefix of `$string`.
125125
* *`hasSuffix $suffix $string`*: Returns whether `$suffix` is a suffix of `$string`.
126+
* *`intersect $slice1 $slice2`*: Returns the strings that exist in both string slices.
126127
* *`json $value`*: Returns the JSON representation of `$value` as a `string`.
127128
* *`keys $map`*: Returns the keys from `$map`. If `$map` is `nil`, a `nil` is returned. If `$map` is not a `map`, an error will be thrown.
128129
* *`last $array`*: Returns the last value of an array.

template.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ func keys(input interface{}) (interface{}, error) {
9494
return k, nil
9595
}
9696

97+
func intersect(l1, l2 []string) []string {
98+
m := make(map[string]bool)
99+
m2 := make(map[string]bool)
100+
for _, v := range l2 {
101+
m2[v] = true
102+
}
103+
for _, v := range l1 {
104+
if m2[v] {
105+
m[v] = true
106+
}
107+
}
108+
keys := make([]string, 0, len(m))
109+
for k := range m {
110+
keys = append(keys, k)
111+
}
112+
return keys
113+
}
114+
97115
func contains(item map[string]string, key string) bool {
98116
if _, ok := item[key]; ok {
99117
return true
@@ -214,6 +232,7 @@ func generateFile(config Config, containers Context) bool {
214232
"hasPrefix": hasPrefix,
215233
"hasSuffix": hasSuffix,
216234
"json": marshalJson,
235+
"intersect": intersect,
217236
"keys": keys,
218237
"last": arrayLast,
219238
"replace": strings.Replace,

template_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ func TestKeysNil(t *testing.T) {
7474
}
7575
}
7676

77+
func TestIntersect(t *testing.T) {
78+
if len(intersect([]string{"foo.fo.com", "bar.com"}, []string{"foo.bar.com"})) != 0 {
79+
t.Fatal("Expected no match")
80+
}
81+
82+
if len(intersect([]string{"foo.fo.com", "bar.com"}, []string{"bar.com", "foo.com"})) != 1 {
83+
t.Fatal("Expected only one match")
84+
}
85+
86+
if len(intersect([]string{"foo.com"}, []string{"bar.com", "foo.com"})) != 1 {
87+
t.Fatal("Expected only one match")
88+
}
89+
90+
if len(intersect([]string{"foo.fo.com", "foo.com", "bar.com"}, []string{"bar.com", "foo.com"})) != 2 {
91+
t.Fatal("Expected two matches")
92+
}
93+
}
94+
7795
func TestGroupByExistingKey(t *testing.T) {
7896
containers := []*RuntimeContainer{
7997
&RuntimeContainer{

0 commit comments

Comments
 (0)