Skip to content

Commit

Permalink
bug fix: IsZero incorrectly used on object reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed May 11, 2024
1 parent 26032c4 commit 56fb11f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
10 changes: 5 additions & 5 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ObjectGetProperty(obj Object, name string) (Value, bool) { //nolint: gocogn

// Use the reflect.ValueOf function to get the value of the struct
v := reflect.ValueOf(obj)
if v.IsZero() || !v.IsValid() {
if !v.IsValid() {
return NewUndefinedWithReasonf("object is nil, not a Go value or invalid"), false
}

Expand All @@ -26,12 +26,12 @@ func ObjectGetProperty(obj Object, name string) (Value, bool) { //nolint: gocogn

if v.Kind() == reflect.Ptr {
v = v.Elem()
if v.IsZero() || !v.IsValid() {
if !v.IsValid() {
return NewUndefinedWithReasonf("object interface is nil, not a Go value or invalid"), false
}

t = t.Elem()
if v.IsZero() || !v.IsValid() {
if !v.IsValid() {
return NewUndefinedWithReasonf("object interface is nil, not a Go value or invalid"), false
}
}
Expand Down Expand Up @@ -61,9 +61,9 @@ func ObjectGetMethod(obj Object, name string) (FunctionalValue, bool) { //nolint
}

value := reflect.ValueOf(obj)
if value.IsZero() || !value.IsValid() {
if !value.IsValid() {
return func(...Value) Value {
return NewUndefinedWithReasonf("object is nil for type '%T' or does not have a method '%s' (check if it has a pointer receiver)", obj, name)
return NewUndefinedWithReasonf("object type '%T' is not valid", obj)
}, false
}

Expand Down
22 changes: 14 additions & 8 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ import (
)

func TestObjectGetProperty(t *testing.T) {
var nilCar *Car

val, ok := gal.ObjectGetProperty(nilCar, "Mileage")
require.False(t, ok)
assert.Equal(t, "undefined: object interface is nil, not a Go value or invalid", val.String())

zeroCar := &Car{}

val, ok = gal.ObjectGetProperty(zeroCar, "Mileage")
require.True(t, ok)
assert.Equal(t, "0", val.String())

myCar := &Car{
Make: "Lotus",
Mileage: gal.NewNumberFromInt(100),
Speed: 50.345,
MaxSpeed: 250,
}

var nilCar *Car

val, ok := gal.ObjectGetProperty(nilCar, "Mileage")
require.False(t, ok)
assert.Equal(t, "undefined: object is nil, not a Go value or invalid", val.String())

val, ok = gal.ObjectGetProperty(myCar, "Make")
require.True(t, ok)
assert.Equal(t, "Lotus", val.(gal.String).RawString())
Expand Down Expand Up @@ -61,8 +67,8 @@ func TestObjectGetMethod(t *testing.T) {
var nilCar *Car

val, ok := gal.ObjectGetMethod(nilCar, "Ignite")
require.False(t, ok)
assert.Equal(t, "undefined: object is nil for type '*gal_test.Car' or does not have a method 'Ignite' (check if it has a pointer receiver)", val().String())
require.True(t, ok)
assert.Equal(t, gal.True, val())

val, ok = gal.ObjectGetMethod(myCar, "DoesNotExist!")
require.False(t, ok)
Expand Down

0 comments on commit 56fb11f

Please sign in to comment.