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 0c994e7
Show file tree
Hide file tree
Showing 3 changed files with 57 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
46 changes: 46 additions & 0 deletions reflect_go1_19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//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++ {
return false

if !v.Index(i).Comparable() {

Check failure on line 19 in reflect_go1_19.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

v.Index(i).Comparable undefined (type reflect.Value has no field or method Comparable)

Check failure on line 19 in reflect_go1_19.go

View workflow job for this annotation

GitHub Actions / Test (1.18)

v.Index(i).Comparable undefined (type reflect.Value has no field or method Comparable)

Check failure on line 19 in reflect_go1_19.go

View workflow job for this annotation

GitHub Actions / Test (1.19)

v.Index(i).Comparable undefined (type reflect.Value has no field or method Comparable)
return false
}
}
return true
}
// return v.Type().Comparable()
return false

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

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

if !v.Field(i).Comparable() {

Check failure on line 36 in reflect_go1_19.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

v.Field(i).Comparable undefined (type reflect.Value has no field or method Comparable)

Check failure on line 36 in reflect_go1_19.go

View workflow job for this annotation

GitHub Actions / Test (1.18)

v.Field(i).Comparable undefined (type reflect.Value has no field or method Comparable)

Check failure on line 36 in reflect_go1_19.go

View workflow job for this annotation

GitHub Actions / Test (1.19)

v.Field(i).Comparable undefined (type reflect.Value has no field or method Comparable)
return false
}
}
return true

default:
// return v.Type().Comparable()
return false
}
}
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 0c994e7

Please sign in to comment.