Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions dict.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sprig

import (
"fmt"
"reflect"

"dario.cat/mergo"
"github.com/mitchellh/copystructure"
)
Expand Down Expand Up @@ -37,11 +40,15 @@ func pluck(key string, d ...map[string]interface{}) []interface{} {
return res
}

func keys(dicts ...map[string]interface{}) []string {
func keys(dicts ...interface{}) []string {
k := []string{}
for _, dict := range dicts {
for key := range dict {
k = append(k, key)
m := reflect.ValueOf(dict)
if m.Kind() != reflect.Map {
continue
}
for _, key := range m.MapKeys() {
k = append(k, fmt.Sprint(key.Interface()))
}
}
return k
Expand Down
18 changes: 18 additions & 0 deletions dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ func TestKeys(t *testing.T) {
}
}

func TestKeysWithTypedMaps(t *testing.T) {
// keys should work with maps whose value type is not interface{} (issue #455).
type item struct{ N int }
vars := map[string]interface{}{
"structMap": map[string]item{"foo": {1}, "bar": {2}},
"intMap": map[string]int{"a": 1, "b": 2},
}
tests := map[string]string{
`{{ keys .structMap | sortAlpha }}`: "[bar foo]",
`{{ keys .intMap | sortAlpha }}`: "[a b]",
}
for tpl, expect := range tests {
if err := runtv(tpl, expect, vars); err != nil {
t.Error(err)
}
}
}

func TestPick(t *testing.T) {
tests := map[string]string{
`{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "two" | len -}}`: "1",
Expand Down
3 changes: 2 additions & 1 deletion docs/dicts.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ deepCopy $source | mergeOverwrite $dest
## keys

The `keys` function will return a `list` of all of the keys in one or more `dict`
types. Since a dictionary is _unordered_, the keys will not be in a predictable order.
types. Any map type is accepted, not only `dict` (`map[string]interface{}`).
Since a dictionary is _unordered_, the keys will not be in a predictable order.
They can be sorted with `sortAlpha`.

```
Expand Down