Skip to content

Commit 43eb300

Browse files
committed
assert.Empty: refactor isEmpty (1)
Refactor isEmpty to extract func isEmptyValue. This allows to avoid unwrapping/wrapping when checking pointer values.
1 parent 0be19fb commit 43eb300

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

assert/assertions.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,11 @@ func isEmpty(object interface{}) bool {
752752
return true
753753
}
754754

755-
objValue := reflect.ValueOf(object)
755+
return isEmptyValue(reflect.ValueOf(object))
756+
}
756757

758+
// isEmptyValue gets whether the specified reflect.Value is considered empty or not.
759+
func isEmptyValue(objValue reflect.Value) bool {
757760
switch objValue.Kind() {
758761
// collection types are empty when they have no element
759762
case reflect.Chan, reflect.Map, reflect.Slice:
@@ -763,13 +766,12 @@ func isEmpty(object interface{}) bool {
763766
if objValue.IsNil() {
764767
return true
765768
}
766-
deref := objValue.Elem().Interface()
767-
return isEmpty(deref)
769+
return isEmptyValue(objValue.Elem())
768770
// for all other types, compare against the zero value
769771
// array types are empty when they match their zero-initialized state
770772
default:
771773
zero := reflect.Zero(objValue.Type())
772-
return reflect.DeepEqual(object, zero.Interface())
774+
return reflect.DeepEqual(objValue.Interface(), zero.Interface())
773775
}
774776
}
775777

0 commit comments

Comments
 (0)