Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: checker panic on embedded pointer to struct field #500

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func TestCheck(t *testing.T) {
{"(Any.Bool ?? Bool) > 0"},
{"Bool ?? Bool"},
{"let foo = 1; foo == 1"},
{"(Embed).EmbedPointerEmbedInt > 0"},
}

for _, tt := range tests {
Expand Down
6 changes: 5 additions & 1 deletion checker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ func fetchField(t reflect.Type, name string) (reflect.StructField, bool) {
for i := 0; i < t.NumField(); i++ {
anon := t.Field(i)
if anon.Anonymous {
if field, ok := fetchField(anon.Type, name); ok {
anonType := anon.Type
for anonType.Kind() == reflect.Pointer {
anonType = anonType.Elem()
}
if field, ok := fetchField(anonType, name); ok {
field.Index = append(anon.Index, field.Index...)
return field, true
}
Expand Down
62 changes: 62 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,68 @@ func TestIssue462(t *testing.T) {
require.Error(t, err)
}

func TestIssue_embedded_pointer_struct(t *testing.T) {
var tests = []struct {
input string
env mock.Env
want any
}{
{
input: "(Embed).EmbedPointerEmbedInt > 0",
env: mock.Env{
Embed: mock.Embed{
EmbedPointerEmbed: &mock.EmbedPointerEmbed{
EmbedPointerEmbedInt: 123,
},
},
},
want: true,
},
{
input: "(Embed).EmbedPointerEmbedInt > 0",
env: mock.Env{
Embed: mock.Embed{
EmbedPointerEmbed: &mock.EmbedPointerEmbed{
EmbedPointerEmbedInt: 0,
},
},
},
want: false,
},
{
input: "(Embed).EmbedPointerEmbedMethod(0)",
env: mock.Env{
Embed: mock.Embed{
EmbedPointerEmbed: &mock.EmbedPointerEmbed{
EmbedPointerEmbedInt: 0,
},
},
},
want: "",
},
{
input: "(Embed).EmbedPointerEmbedPointerReceiverMethod(0)",
env: mock.Env{
Embed: mock.Embed{
EmbedPointerEmbed: nil,
},
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
program, err := expr.Compile(tt.input, expr.Env(tt.env))
require.NoError(t, err)

out, err := expr.Run(program, tt.env)
require.NoError(t, err)

require.Equal(t, tt.want, out)
})
}
}

func TestIssue(t *testing.T) {
testCases := []struct {
code string
Expand Down
13 changes: 13 additions & 0 deletions test/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,26 @@ func (Env) NotStringerStringerEqual(f fmt.Stringer, g fmt.Stringer) bool {

type Embed struct {
EmbedEmbed
*EmbedPointerEmbed
EmbedString string
}

func (p Embed) EmbedMethod(_ int) string {
return ""
}

type EmbedPointerEmbed struct {
EmbedPointerEmbedInt int
}

func (p EmbedPointerEmbed) EmbedPointerEmbedMethod(_ int) string {
return ""
}

func (p *EmbedPointerEmbed) EmbedPointerEmbedPointerReceiverMethod(_ int) string {
return ""
}

type EmbedEmbed struct {
EmbedEmbedString string
}
Expand Down
Loading