Skip to content

Commit

Permalink
Merge pull request #71 from wonderpush/fix-map-struct
Browse files Browse the repository at this point in the history
Fix map filter with slice of structs
  • Loading branch information
danog authored Aug 23, 2023
2 parents 919a6d6 + 4d1de3d commit ddc76a5
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 @@ -50,9 +50,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 @@ -63,6 +63,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 @@ -244,6 +246,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 ddc76a5

Please sign in to comment.