Skip to content

Commit 29b4216

Browse files
committed
Merge pull request #39 from md5/keys-function
Add "keys" function
2 parents f177402 + a29489d commit 29b4216

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Within those templates, the object emitted by docker-gen will have [this structu
122122
* *`hasPrefix $prefix $string`*: Returns whether `$prefix` is a prefix of `$string`.
123123
* *`hasSuffix $suffix $string`*: Returns whether `$suffix` is a suffix of `$string`.
124124
* *`json $value`*: Returns the JSON representation of `$value` as a `string`.
125+
* *`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.
125126
* *`last $array`*: Returns the last value of an array.
126127
* *`replace $string $old $new $count`*: Replaces up to `$count` occurences of `$old` with `$new` in `$string`. Alias for [`strings.Replace`](http://golang.org/pkg/strings/#Replace)
127128
* *`sha1 $string`*: Returns the hexadecimal representation of the SHA1 hash of `$string`.

template.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ func hasSuffix(suffix, s string) bool {
7575
return strings.HasSuffix(s, suffix)
7676
}
7777

78+
func keys(input interface{}) (interface{}, error) {
79+
if input == nil {
80+
return nil, nil
81+
}
82+
83+
val := reflect.ValueOf(input)
84+
if val.Kind() != reflect.Map {
85+
return nil, fmt.Errorf("Cannot call keys on a non-map value: %v", input)
86+
}
87+
88+
vk := val.MapKeys()
89+
k := make([]interface{}, val.Len())
90+
for i, _ := range k {
91+
k[i] = vk[i].Interface()
92+
}
93+
94+
return k, nil
95+
}
96+
7897
func contains(item map[string]string, key string) bool {
7998
if _, ok := item[key]; ok {
8099
return true
@@ -195,6 +214,7 @@ func generateFile(config Config, containers Context) bool {
195214
"hasPrefix": hasPrefix,
196215
"hasSuffix": hasSuffix,
197216
"json": marshalJson,
217+
"keys": keys,
198218
"last": arrayLast,
199219
"replace": strings.Replace,
200220
"sha1": hashSha1,

template_test.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"encoding/json"
6+
"reflect"
67
"testing"
78
)
89

@@ -20,7 +21,60 @@ func TestContains(t *testing.T) {
2021
}
2122
}
2223

23-
func TestGroupByExitingKey(t *testing.T) {
24+
func TestKeys(t *testing.T) {
25+
expected := "VIRTUAL_HOST"
26+
env := map[string]string{
27+
expected: "demo.local",
28+
}
29+
30+
k, err := keys(env)
31+
if err != nil {
32+
t.Fatalf("Error fetching keys: %v", err)
33+
}
34+
vk := reflect.ValueOf(k)
35+
if vk.Kind() == reflect.Invalid {
36+
t.Fatalf("Got invalid kind for keys: %v", vk)
37+
}
38+
39+
if len(env) != vk.Len() {
40+
t.Fatalf("Incorrect key count; expected %s, got %s", len(env), vk.Len())
41+
}
42+
43+
got := vk.Index(0).Interface()
44+
if expected != got {
45+
t.Fatalf("Incorrect key found; expected %s, got %s", expected, got)
46+
}
47+
}
48+
49+
func TestKeysEmpty(t *testing.T) {
50+
input := map[string]int{}
51+
52+
k, err := keys(input)
53+
if err != nil {
54+
t.Fatalf("Error fetching keys: %v", err)
55+
}
56+
vk := reflect.ValueOf(k)
57+
if vk.Kind() == reflect.Invalid {
58+
t.Fatalf("Got invalid kind for keys: %v", vk)
59+
}
60+
61+
if len(input) != vk.Len() {
62+
t.Fatalf("Incorrect key count; expected %s, got %s", len(input), vk.Len())
63+
}
64+
}
65+
66+
func TestKeysNil(t *testing.T) {
67+
k, err := keys(nil)
68+
if err != nil {
69+
t.Fatalf("Error fetching keys: %v", err)
70+
}
71+
vk := reflect.ValueOf(k)
72+
if vk.Kind() != reflect.Invalid {
73+
t.Fatalf("Got invalid kind for keys: %v", vk)
74+
}
75+
}
76+
77+
func TestGroupByExistingKey(t *testing.T) {
2478
containers := []*RuntimeContainer{
2579
&RuntimeContainer{
2680
Env: map[string]string{

0 commit comments

Comments
 (0)