Skip to content

Commit

Permalink
Fix map filter with slice of structs
Browse files Browse the repository at this point in the history
The previous map implementation worked only for slices of maps.
By using the library's `values` package this implementation should work
with all compatible types.
  • Loading branch information
ofavre committed Sep 16, 2022
1 parent f491f5e commit 4d1de3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions filters/standard_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ func AddStandardFilters(fd FilterDictionary) { // nolint: gocyclo
return append(append(result, a...), b...)
})
fd.AddFilter("join", joinFilter)
fd.AddFilter("map", func(a []map[string]interface{}, key string) (result []interface{}) {
fd.AddFilter("map", func(a []interface{}, key string) (result []interface{}) {
keyValue := values.ValueOf(key)
for _, obj := range a {
result = append(result, obj[key])
value := values.ValueOf(obj)
result = append(result, value.PropertyValue(keyValue).Interface())
}
return result
})
Expand Down
9 changes: 9 additions & 0 deletions filters/standard_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ var filterTests = []struct {
{`map_slice_dup | join`, `a a b`},
{`map_slice_dup | uniq | join`, `a b`},

{`struct_slice | map: "str" | join`, `a b c`},

// date filters
{`article.published_at | date`, "Fri, Jul 17, 15"},
{`article.published_at | date: "%a, %b %d, %y"`, "Fri, Jul 17, 15"},
Expand Down Expand Up @@ -237,6 +239,13 @@ var filterTestBindings = map[string]interface{}{
{"name": "page 6"},
{"name": "page 7", "category": "technology"},
},
"struct_slice": []struct {
Str string `liquid:"str"`
}{
{Str: "a"},
{Str: "b"},
{Str: "c"},
},
}

func TestFilters(t *testing.T) {
Expand Down

0 comments on commit 4d1de3d

Please sign in to comment.