Add check for non struct types to fetchField (#794)

Co-authored-by: yigitkanbalci <[email protected]>
This commit is contained in:
Yigitkan Balci
2025-05-27 12:13:35 +02:00
committed by GitHub
co-authored by yigitkanbalci
parent c76a6cf3e8
commit 483e066175
2 changed files with 30 additions and 0 deletions
+25
View File
@@ -1,6 +1,7 @@
package checker_test
import (
"context"
"fmt"
"reflect"
"regexp"
@@ -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{
+5
View File
@@ -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)