diff --git a/object.go b/object.go index 7e45498..3a9d8f2 100644 --- a/object.go +++ b/object.go @@ -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 } @@ -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 } } @@ -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 } diff --git a/object_test.go b/object_test.go index 79b471c..f66d42e 100644 --- a/object_test.go +++ b/object_test.go @@ -11,6 +11,18 @@ 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), @@ -18,12 +30,6 @@ func TestObjectGetProperty(t *testing.T) { 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()) @@ -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)