Merge pull request #3980 from aledbf/refactor-isiterable
Refactor isIterable
This commit is contained in:
commit
10c0df55c8
1 changed files with 7 additions and 6 deletions
|
@ -24,18 +24,19 @@ type equalFunction func(e1, e2 interface{}) bool
|
||||||
|
|
||||||
// Compare checks if the parameters are iterable and contains the same elements
|
// Compare checks if the parameters are iterable and contains the same elements
|
||||||
func Compare(listA, listB interface{}, eq equalFunction) bool {
|
func Compare(listA, listB interface{}, eq equalFunction) bool {
|
||||||
a := reflect.ValueOf(listA)
|
ok := isIterable(listA)
|
||||||
ok := isIterable(a)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
b := reflect.ValueOf(listB)
|
ok = isIterable(listB)
|
||||||
ok = isIterable(b)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a := reflect.ValueOf(listA)
|
||||||
|
b := reflect.ValueOf(listB)
|
||||||
|
|
||||||
if a.IsNil() && b.IsNil() {
|
if a.IsNil() && b.IsNil() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -91,9 +92,9 @@ func StringElementsMatch(a, b []string) bool {
|
||||||
return Compare(a, b, compareStrings)
|
return Compare(a, b, compareStrings)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isIterable(obj reflect.Value) bool {
|
func isIterable(obj interface{}) bool {
|
||||||
switch reflect.TypeOf(obj).Kind() {
|
switch reflect.TypeOf(obj).Kind() {
|
||||||
case reflect.Struct, reflect.Slice, reflect.Array:
|
case reflect.Slice, reflect.Array:
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Reference in a new issue