From 02893d30636e5594ce426def1af6eb112ca81bb3 Mon Sep 17 00:00:00 2001 From: yigitkanbalci Date: Tue, 27 May 2025 00:40:08 +0300 Subject: [PATCH] add check for non struct types to fetchField --- checker/checker_test.go | 25 +++++++++++++++++++++++++ checker/nature/utils.go | 5 +++++ 2 files changed, 30 insertions(+) diff --git a/checker/checker_test.go b/checker/checker_test.go index 4950c1a8e..2ec7c7cfc 100644 --- a/checker/checker_test.go +++ b/checker/checker_test.go @@ -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{ diff --git a/checker/nature/utils.go b/checker/nature/utils.go index c242f91a6..179459874 100644 --- a/checker/nature/utils.go +++ b/checker/nature/utils.go @@ -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)