Skip to content

add check for non struct types to fetchField #794

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

Merged
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
25 changes: 25 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package checker_test

import (
"context"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -1078,6 +1079,30 @@ func TestCheck_builtin_without_call(t *testing.T) {
}
}

func TestCheck_EmbeddedInterface(t *testing.T) {
t.Run("embedded interface lookup returns compile-error not panic", func(t *testing.T) {
type Env struct {
context.Context
Country string
}
type Wrapper struct {
Ctx Env
}

config := conf.New(Wrapper{
Ctx: Env{
Context: context.Background(),
Country: "TR",
},
})
expr.WithContext("Ctx")(config)

_, err := checker.ParseCheck("Ctx.C", config)
require.Error(t, err)
require.Contains(t, err.Error(), "has no field C")
})
}

func TestCheck_types(t *testing.T) {
env := types.Map{
"foo": types.Map{
Expand Down
5 changes: 5 additions & 0 deletions checker/nature/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func fieldName(field reflect.StructField) string {
}

func fetchField(t reflect.Type, name string) (reflect.StructField, bool) {
// If t is not a struct, early return.
if t.Kind() != reflect.Struct {
return reflect.StructField{}, false
}

// First check all structs fields.
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
Expand Down
Loading