Fix Fetch and OpDeref (#467)

* Fix OpDeref allowing to deref pointer of interfaces

Loop over `Ptr` and `Interface` kind and get `Elem`. Also return
directly the `nil` otherwise we might are returning the pointer nil
value and not the base nil.

* Fix deref interface in Fetch

We were only de-referencing pointers, but we can have pointer of
interface or interface obfuscating pointers...
So before trying to fetch anything, just deference fully the
variable.
This commit is contained in:
Augustin Peyrard
2023-11-16 12:40:28 +01:00
committed by GitHub
parent 5ba4704bf8
commit fe7f698b81
3 changed files with 90 additions and 20 deletions
+15
View File
@@ -1204,6 +1204,21 @@ func TestExpr_fetch_from_func(t *testing.T) {
assert.Contains(t, err.Error(), "cannot fetch Value from func()")
}
func TestExpr_fetch_from_interface(t *testing.T) {
type FooBar struct {
Value string
}
foobar := &FooBar{"waldo"}
var foobarAny any = foobar
var foobarPtrAny any = &foobarAny
res, err := expr.Eval("foo.Value", map[string]any{
"foo": foobarPtrAny,
})
assert.NoError(t, err)
assert.Equal(t, "waldo", res)
}
func TestExpr_map_default_values(t *testing.T) {
env := map[string]any{
"foo": map[string]string{},
+63
View File
@@ -137,3 +137,66 @@ func TestDeref_multiple_pointers(t *testing.T) {
require.Equal(t, 44, output)
})
}
func TestDeref_pointer_of_interface(t *testing.T) {
v := 42
a := &v
b := any(a)
c := any(&b)
t.Run("returned as is", func(t *testing.T) {
output, err := expr.Eval(`c`, map[string]any{
"c": c,
})
require.NoError(t, err)
require.Equal(t, c, output)
require.IsType(t, (*interface{})(nil), output)
})
t.Run("+ works", func(t *testing.T) {
output, err := expr.Eval(`c+2`, map[string]any{
"c": c,
})
require.NoError(t, err)
require.Equal(t, 44, output)
})
}
func TestDeref_nil(t *testing.T) {
var b *int = nil
c := &b
t.Run("returned as is", func(t *testing.T) {
output, err := expr.Eval(`c`, map[string]any{
"c": c,
})
require.NoError(t, err)
require.Equal(t, c, output)
require.IsType(t, (**int)(nil), output)
})
t.Run("== nil works", func(t *testing.T) {
output, err := expr.Eval(`c == nil`, map[string]any{
"c": c,
})
require.NoError(t, err)
require.Equal(t, true, output)
})
}
func TestDeref_nil_in_pointer_of_interface(t *testing.T) {
var a *int32 = nil
b := any(a)
c := any(&b)
t.Run("returned as is", func(t *testing.T) {
output, err := expr.Eval(`c`, map[string]any{
"c": c,
})
require.NoError(t, err)
require.Equal(t, c, output)
require.IsType(t, (*interface{})(nil), output)
})
t.Run("== nil works", func(t *testing.T) {
output, err := expr.Eval(`c == nil`, map[string]any{
"c": c,
})
require.NoError(t, err)
require.Equal(t, true, output)
})
}
+12 -20
View File
@@ -8,6 +8,14 @@ import (
"reflect"
)
func deref(kind reflect.Kind, value reflect.Value) (reflect.Kind, reflect.Value) {
for kind == reflect.Ptr || kind == reflect.Interface {
value = value.Elem()
kind = value.Kind()
}
return kind, value
}
func Fetch(from, i any) any {
v := reflect.ValueOf(from)
kind := v.Kind()
@@ -28,10 +36,8 @@ func Fetch(from, i any) any {
// Structs, maps, and slices can be access through a pointer or through
// a value, when they are accessed through a pointer we don't want to
// copy them to a value.
if kind == reflect.Ptr {
v = reflect.Indirect(v)
kind = v.Kind()
}
// De-reference everything if necessary (interface and pointers)
kind, v = deref(kind, v)
// TODO: We can create separate opcodes for each of the cases below to make
// the little bit faster.
@@ -145,27 +151,13 @@ func Deref(i any) any {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Interface {
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
if v.IsNil() {
return i
return nil
}
v = v.Elem()
}
loop:
for v.Kind() == reflect.Ptr {
if v.IsNil() {
return i
}
indirect := reflect.Indirect(v)
switch indirect.Kind() {
case reflect.Struct, reflect.Map, reflect.Array, reflect.Slice:
break loop
default:
v = v.Elem()
}
}
if v.IsValid() {
return v.Interface()
}