Skip to content

Commit

Permalink
fix: backport comparable check to <1.20
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
  • Loading branch information
sagikazarmark committed Dec 18, 2023
1 parent 58a4c3b commit a93507f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mapstructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions reflect_go1_19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build !go1.20

package mapstructure

import "reflect"

func isComparable(v reflect.Value) bool {
k := v.Kind()
switch k {
case reflect.Invalid:
return false

case reflect.Array:
switch v.Type().Elem().Kind() {
case reflect.Interface, reflect.Array, reflect.Struct:
for i := 0; i < v.Type().Len(); i++ {
// if !v.Index(i).Comparable() {
if !isComparable(v.Index(i)) {
return false
}
}
return true
}
return v.Type().Comparable()

case reflect.Interface:
// return v.Elem().Comparable()
return isComparable(v.Elem())

case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
return false

// if !v.Field(i).Comparable() {
if !isComparable(v.Field(i)) {
return false
}
}
return true

default:
return v.Type().Comparable()
}
}
10 changes: 10 additions & 0 deletions reflect_go1_20.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit a93507f

Please sign in to comment.