From 4d1de3d50f98a76e449f7bda0c813d1d97c7d0a3 Mon Sep 17 00:00:00 2001 From: Olivier Favre Date: Fri, 16 Sep 2022 15:34:28 +0200 Subject: [PATCH] Fix map filter with slice of structs 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. --- filters/standard_filters.go | 6 ++++-- filters/standard_filters_test.go | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/filters/standard_filters.go b/filters/standard_filters.go index 181125b..8cb2955 100644 --- a/filters/standard_filters.go +++ b/filters/standard_filters.go @@ -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 }) diff --git a/filters/standard_filters_test.go b/filters/standard_filters_test.go index 7d7cb31..6aba9be 100644 --- a/filters/standard_filters_test.go +++ b/filters/standard_filters_test.go @@ -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"}, @@ -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) {