Issue 840: Fix get field pointer struct (#843)

* improve issue test

* fix #840: pointer to struct env not returning fields
This commit is contained in:
Diego Augusto Molina
2025-09-21 20:42:57 +02:00
committed by GitHub
parent 0bd991a6bf
commit faf4cc4359
2 changed files with 19 additions and 7 deletions
+9 -2
View File
@@ -404,8 +404,15 @@ func (n *Nature) getSlow(c *Cache, name string) (Nature, bool) {
if nt, ok := n.MethodByName(c, name); ok {
return nt, true
}
if n.Kind == reflect.Struct {
if sf := n.structField(c, nil, name); sf != nil {
t, k, changed := deref.TypeKind(n.Type, n.Kind)
if k == reflect.Struct {
var sd *structData
if changed {
sd = c.getStruct(t).structData
} else {
sd = n.structData
}
if sf := sd.structField(c, nil, name); sf != nil {
return sf.Nature, true
}
}
+10 -5
View File
@@ -8,23 +8,28 @@ import (
)
func TestEnvFieldMethods(t *testing.T) {
program, err := expr.Compile(`Func(0)`, expr.Env(&Env{}))
program, err := expr.Compile(`Func() + Int`, expr.Env(&Env{}))
require.NoError(t, err)
env := &Env{}
env.Func = func() int {
return 42
return 40
}
env.EmbeddedEnv = &EmbeddedEnv{
Int: 2,
}
out, err := expr.Run(program, Env{})
out, err := expr.Run(program, env)
require.NoError(t, err)
require.Equal(t, 42, out)
}
type Env struct {
EmbeddedEnv
*EmbeddedEnv
Func func() int
}
type EmbeddedEnv struct{}
type EmbeddedEnv struct {
Int int
}