mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Add check for non struct types to fetchField (#794)
Co-authored-by: yigitkanbalci <[email protected]>
This commit is contained in:
committed by
GitHub
co-authored by
yigitkanbalci
parent
c76a6cf3e8
commit
483e066175
@@ -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{
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user