diff options
author | satotake <doublequotation@gmail.com> | 2020-03-09 21:32:38 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-09 13:32:38 +0100 |
commit | 8279d2e2271ee64725133d36a12d1d7e2158bffd (patch) | |
tree | 7ca32e23e380b73538bd08f7269384d8d9a97142 /tpl/collections/reflect_helpers.go | |
parent | c4fa2f07996c7f1f4e257089a3c3c5b4c1339722 (diff) |
Support unComparable args of uniq/complement/in
Fixes #6105
Diffstat (limited to 'tpl/collections/reflect_helpers.go')
-rw-r--r-- | tpl/collections/reflect_helpers.go | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/tpl/collections/reflect_helpers.go b/tpl/collections/reflect_helpers.go index 69425fcb0..3d73b70e1 100644 --- a/tpl/collections/reflect_helpers.go +++ b/tpl/collections/reflect_helpers.go @@ -18,6 +18,7 @@ import ( "reflect" "time" + "github.com/mitchellh/hashstructure" "github.com/pkg/errors" ) @@ -42,18 +43,25 @@ func numberToFloat(v reflect.Value) (float64, error) { } } -// normalizes different numeric types to make them comparable. +// normalizes different numeric types if isNumber +// or get the hash values if not Comparable (such as map or struct) +// to make them comparable func normalize(v reflect.Value) interface{} { k := v.Kind() switch { + case !v.Type().Comparable(): + h, err := hashstructure.Hash(v.Interface(), nil) + if err != nil { + panic(err) + } + return h case isNumber(k): f, err := numberToFloat(v) if err == nil { return f } } - return v.Interface() } |