Skip to content

Commit 941e74b

Browse files
committed
Add support for nested element type
Signed-off-by: MisLink <[email protected]>
1 parent 050981e commit 941e74b

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

ext/native.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ func newNativeTypes(fieldNameHandler NativeTypesFieldNameHandler, rawType reflec
609609
var iterateStructMembers func(reflect.Type)
610610
iterateStructMembers = func(t reflect.Type) {
611611
if k := t.Kind(); k == reflect.Pointer || k == reflect.Slice || k == reflect.Array || k == reflect.Map {
612-
t = t.Elem()
612+
iterateStructMembers(t.Elem())
613613
}
614614
if t.Kind() != reflect.Struct {
615615
return

ext/native_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,67 @@ func TestNativeStructEmbedded(t *testing.T) {
941941
}
942942
}
943943

944+
type TestNestedStruct struct {
945+
ListVal []*TestNestedType
946+
}
947+
948+
func TestNativeNestedStruct(t *testing.T) {
949+
var nativeTests = []struct {
950+
expr string
951+
in any
952+
}{
953+
{
954+
expr: `test.ListVal.exists(x, x.custom_name == "name")`,
955+
in: map[string]any{
956+
"test": &TestNestedStruct{ListVal: []*TestNestedType{{NestedCustomName: "name"}}},
957+
},
958+
},
959+
}
960+
961+
envOpts := []cel.EnvOption{
962+
NativeTypes(
963+
reflect.ValueOf(&TestNestedStruct{}),
964+
ParseStructTag("json"),
965+
),
966+
cel.Variable("test", cel.ObjectType("ext.TestNestedStruct")),
967+
}
968+
969+
env, err := cel.NewEnv(envOpts...)
970+
if err != nil {
971+
t.Fatalf("cel.NewEnv(NativeTypes()) failed: %v", err)
972+
}
973+
974+
for i, tst := range nativeTests {
975+
tc := tst
976+
t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
977+
var asts []*cel.Ast
978+
pAst, iss := env.Parse(tc.expr)
979+
if iss.Err() != nil {
980+
t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err())
981+
}
982+
asts = append(asts, pAst)
983+
cAst, iss := env.Check(pAst)
984+
if iss.Err() != nil {
985+
t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err())
986+
}
987+
asts = append(asts, cAst)
988+
for _, ast := range asts {
989+
prg, err := env.Program(ast)
990+
if err != nil {
991+
t.Fatal(err)
992+
}
993+
out, _, err := prg.Eval(tc.in)
994+
if err != nil {
995+
t.Fatal(err)
996+
}
997+
if !reflect.DeepEqual(out.Value(), true) {
998+
t.Errorf("got %v, wanted true for expr: %s", out.Value(), tc.expr)
999+
}
1000+
}
1001+
})
1002+
}
1003+
}
1004+
9441005
func TestNativeTypesVersion(t *testing.T) {
9451006
_, err := cel.NewEnv(NativeTypes(NativeTypesVersion(0)))
9461007
if err != nil {

0 commit comments

Comments
 (0)