Skip to content

Commit

Permalink
template: add filter to get map value by key
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Jun 29, 2020
1 parent 04c1d6c commit e36da5f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/content/template/template-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ weight: 20
```
</details>

<details>
<summary> **mapValue** -- Returns an map element by key </summary>
```
{{ getv("/some_yaml_config") | parseYAML | mapValue:"key" }}
```
</details>

<details>
<summary> **index** -- Returns an array element by index </summary>
```
Expand Down
26 changes: 26 additions & 0 deletions pkg/template/template_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io/ioutil"
"path"
"path/filepath"
"reflect"
"sort"
"strings"

Expand All @@ -36,6 +37,7 @@ func init() {
pongo2.RegisterFilter("base", filterBase)
pongo2.RegisterFilter("base64", filterBase64)
pongo2.RegisterFilter("index", filterIndex)
pongo2.RegisterFilter("mapValue", filterMapValue)
}

// RegisterCustomJsFilters loads all filters from the given directory.
Expand Down Expand Up @@ -167,6 +169,30 @@ func filterIndex(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.
return pongo2.AsValue(in.Index(index)), nil
}

func filterMapValue(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if in == nil || in.IsNil() {
return pongo2.AsValue(nil), nil
}

val := reflect.ValueOf(in.Interface())
if val.Kind() == reflect.Map {
valueType := val.Type().Key().Kind()
paramValue := reflect.ValueOf(param.Interface())

if paramValue.Kind() != valueType {
return pongo2.AsValue(nil), nil
}

mv := val.MapIndex(paramValue)
if !mv.IsValid() {
return pongo2.AsValue(nil), nil
}

return pongo2.AsValue(mv.Interface()), nil
}
return pongo2.AsValue(nil), nil
}

func filterSortByLength(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.CanSlice() {
return in, nil
Expand Down
42 changes: 42 additions & 0 deletions pkg/template/template_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,48 @@ func (s *FilterSuite) TestFilterSortByLengthKVPair(t *C) {
t.Check(m1, DeepEquals, expected)
}

func (s *FilterSuite) TestFilterMapValue(t *C) {
a := map[string]int{
"hallo": 1,
"moin": 2,
}
fm, err := filterMapValue(pongo2.AsValue(a), pongo2.AsValue("moin"))
if err != nil {
t.Error(err.OrigError)
}
t.Check(fm.Interface(), DeepEquals, 2)

b := map[string]string{
"servus": "one",
"hi": "oneone",
}
fm, err = filterMapValue(pongo2.AsValue(b), pongo2.AsValue("servus"))
if err != nil {
t.Error(err.OrigError)
}
t.Check(fm.Interface(), DeepEquals, "one")

c := map[string]interface{}{
"servus": "one",
"hi": 100,
}
fm, err = filterMapValue(pongo2.AsValue(c), pongo2.AsValue("hi"))
if err != nil {
t.Error(err.OrigError)
}
t.Check(fm.Interface(), DeepEquals, 100)

d := map[int]string{
1: "one",
2: "oneone",
}
fm, err = filterMapValue(pongo2.AsValue(d), pongo2.AsValue(2))
if err != nil {
t.Error(err.OrigError)
}
t.Check(fm.Interface(), DeepEquals, "oneone")
}

func (s *FilterSuite) TestFilterIndex(t *C) {
in := pongo2.AsValue([]string{"Hallo", "Test", "123", "Moin"})
expected := "123"
Expand Down

0 comments on commit e36da5f

Please sign in to comment.