Skip to content

Commit 4401220

Browse files
authored
Merge pull request #343 from AlexanderLieret/master
Lift restriction on the function contains
2 parents de4d963 + 69adf5b commit 4401220

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct:
353353

354354
* *`closest $array $value`*: Returns the longest matching substring in `$array` that matches `$value`
355355
* *`coalesce ...`*: Returns the first non-nil argument.
356-
* *`contains $map $key`*: Returns `true` if `$map` contains `$key`. Takes maps from `string` to `string`.
356+
* *`contains $map $key`*: Returns `true` if `$map` contains `$key`. Takes maps from `string` to any type.
357357
* *`dict $key $value ...`*: Creates a map from a list of pairs. Each `$key` value must be a `string`, but the `$value` can be any type (or `nil`). Useful for passing more than one value as a pipeline context to subtemplates.
358358
* *`dir $path`*: Returns an array of filenames in the specified `$path`.
359359
* *`exists $path`*: Returns `true` if `$path` refers to an existing file or directory. Takes a string.

template.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,20 @@ func intersect(l1, l2 []string) []string {
291291
return keys
292292
}
293293

294-
func contains(item map[string]string, key string) bool {
295-
if _, ok := item[key]; ok {
296-
return true
294+
func contains(input interface{}, key interface{}) bool {
295+
if input == nil {
296+
return false
297+
}
298+
299+
val := reflect.ValueOf(input)
300+
if val.Kind() == reflect.Map {
301+
for _, k := range val.MapKeys() {
302+
if k.Interface() == key {
303+
return true
304+
}
305+
}
297306
}
307+
298308
return false
299309
}
300310

template_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (tests templateTestList) run(t *testing.T, prefix string) {
3333
}
3434
}
3535

36-
func TestContains(t *testing.T) {
36+
func TestContainsString(t *testing.T) {
3737
env := map[string]string{
3838
"PORT": "1234",
3939
}
@@ -47,6 +47,24 @@ func TestContains(t *testing.T) {
4747
}
4848
}
4949

50+
func TestContainsInteger(t *testing.T) {
51+
env := map[int]int{
52+
42: 1234,
53+
}
54+
55+
if !contains(env, 42) {
56+
t.Fail()
57+
}
58+
59+
if contains(env, "WRONG TYPE") {
60+
t.Fail()
61+
}
62+
63+
if contains(env, 24) {
64+
t.Fail()
65+
}
66+
}
67+
5068
func TestKeys(t *testing.T) {
5169
env := map[string]string{
5270
"VIRTUAL_HOST": "demo.local",

0 commit comments

Comments
 (0)