From eabcb1dc262eeaeb3e35fb805d8b25a93b2d3792 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Tue, 11 Jul 2017 12:13:57 -0400 Subject: [PATCH] group_by was returning reflection values --- filters/filters.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/filters/filters.go b/filters/filters.go index f7ab42b..ae5e04c 100644 --- a/filters/filters.go +++ b/filters/filters.go @@ -181,25 +181,21 @@ func arrayToSentenceStringFilter(array []string, conjunction func(string) string func groupByFilter(array []map[string]interface{}, property string) []map[string]interface{} { rt := reflect.ValueOf(array) - if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice { + if !(rt.Kind() != reflect.Array || rt.Kind() == reflect.Slice) { return nil } groups := map[interface{}][]interface{}{} for i := 0; i < rt.Len(); i++ { - item := rt.Index(i) - if item.Kind() == reflect.Map && item.Type().Key().Kind() == reflect.String { - attr := item.MapIndex(reflect.ValueOf(property)) - // fmt.Println("invalid", item) - if attr.IsValid() { - key := attr.Interface() - group, found := groups[key] - // fmt.Println("found", attr) - if found { - group = append(group, groups[key]) + irt := rt.Index(i) + if irt.Kind() == reflect.Map && irt.Type().Key().Kind() == reflect.String { + krt := irt.MapIndex(reflect.ValueOf(property)) + if krt.IsValid() && krt.CanInterface() { + key := krt.Interface() + if group, found := groups[key]; found { + groups[key] = append(group, irt.Interface()) } else { - group = []interface{}{item} + groups[key] = []interface{}{irt.Interface()} } - groups[key] = group } } }