From a6ec181d21879b51ea4fefea931225eefe1dae8c Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Mon, 18 Dec 2023 17:04:13 +0100 Subject: [PATCH] fix: backport comparable check to <1.20 Signed-off-by: Mark Sagi-Kazar --- mapstructure.go | 2 +- reflect_go1_19.go | 9 +++++++++ reflect_go1_20.go | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 reflect_go1_19.go create mode 100644 reflect_go1_20.go diff --git a/mapstructure.go b/mapstructure.go index 1f646317..d7ed4473 100644 --- a/mapstructure.go +++ b/mapstructure.go @@ -1163,7 +1163,7 @@ func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value) valArray := val - if valArray.Comparable() && valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields { + if isComparable(valArray) && valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields { // Check input type if dataValKind != reflect.Array && dataValKind != reflect.Slice { if d.config.WeaklyTypedInput { diff --git a/reflect_go1_19.go b/reflect_go1_19.go new file mode 100644 index 00000000..6b8c71ae --- /dev/null +++ b/reflect_go1_19.go @@ -0,0 +1,9 @@ +//go:build !go1.20 + +package mapstructure + +import "reflect" + +func isComparable(v reflect.Value) bool { + return true +} diff --git a/reflect_go1_20.go b/reflect_go1_20.go new file mode 100644 index 00000000..f8255a1b --- /dev/null +++ b/reflect_go1_20.go @@ -0,0 +1,10 @@ +//go:build go1.20 + +package mapstructure + +import "reflect" + +// TODO: remove once we drop support for Go <1.20 +func isComparable(v reflect.Value) bool { + return v.Comparable() +}