Skip to content

Commit 7dbd409

Browse files
authored
Issue 840: Fix get field pointer struct (#843)
* improve issue test * fix #840: pointer to struct env not returning fields
1 parent 69b27af commit 7dbd409

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

checker/nature/nature.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,15 @@ func (n *Nature) getSlow(c *Cache, name string) (Nature, bool) {
404404
if nt, ok := n.MethodByName(c, name); ok {
405405
return nt, true
406406
}
407-
if n.Kind == reflect.Struct {
408-
if sf := n.structField(c, nil, name); sf != nil {
407+
t, k, changed := deref.TypeKind(n.Type, n.Kind)
408+
if k == reflect.Struct {
409+
var sd *structData
410+
if changed {
411+
sd = c.getStruct(t).structData
412+
} else {
413+
sd = n.structData
414+
}
415+
if sf := sd.structField(c, nil, name); sf != nil {
409416
return sf.Nature, true
410417
}
411418
}

test/issues/840/issue_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@ import (
88
)
99

1010
func TestEnvFieldMethods(t *testing.T) {
11-
program, err := expr.Compile(`Func(0)`, expr.Env(&Env{}))
11+
program, err := expr.Compile(`Func() + Int`, expr.Env(&Env{}))
1212
require.NoError(t, err)
1313

1414
env := &Env{}
1515
env.Func = func() int {
16-
return 42
16+
return 40
17+
}
18+
env.EmbeddedEnv = &EmbeddedEnv{
19+
Int: 2,
1720
}
1821

19-
out, err := expr.Run(program, Env{})
22+
out, err := expr.Run(program, env)
2023
require.NoError(t, err)
2124

2225
require.Equal(t, 42, out)
2326
}
2427

2528
type Env struct {
26-
EmbeddedEnv
29+
*EmbeddedEnv
2730
Func func() int
2831
}
2932

30-
type EmbeddedEnv struct{}
33+
type EmbeddedEnv struct {
34+
Int int
35+
}

0 commit comments

Comments
 (0)