MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestNativeStructHiddenField

Function TestNativeStructHiddenField

ext/native_test.go:1122–1163  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1120}
1121
1122func TestNativeStructHiddenField(t *testing.T) {
1123 envOpts := []cel.EnvOption{
1124 NativeTypes(
1125 reflect.TypeFor[*TestEmbeddedTypes](),
1126 ParseStructTag("json"),
1127 ),
1128 cel.Variable("test", cel.ObjectType("ext.TestEmbeddedTypes")),
1129 }
1130
1131 env, err := cel.NewEnv(envOpts...)
1132 if err != nil {
1133 t.Fatalf("cel.NewEnv(NativeTypes()) failed: %v", err)
1134 }
1135
1136 // 1. Static reference compilation failure case
1137 // Attempting to compile `test.Password` should fail static analysis because the field is skipped/hidden.
1138 _, iss := env.Compile("test.Password")
1139 if iss.Err() == nil {
1140 t.Error("env.Compile('test.Password') succeeded, expected a compilation/check error")
1141 }
1142
1143 // 2. Dynamic reference runtime evaluation failure case
1144 // Using dyn(test).Password should compile successfully (since dyn disables static type checks),
1145 // but it must fail at runtime during evaluation because the field is not exposed.
1146 ast, iss := env.Compile("dyn(test).Password")
1147 if iss.Err() != nil {
1148 t.Fatalf("env.Compile('dyn(test).Password') failed: %v", iss.Err())
1149 }
1150 prg, err := env.Program(ast)
1151 if err != nil {
1152 t.Fatalf("env.Program() failed: %v", err)
1153 }
1154 in := map[string]any{
1155 "test": &TestEmbeddedTypes{
1156 Skipped: "sensitive_password",
1157 },
1158 }
1159 out, _, err := prg.Eval(in)
1160 if err == nil {
1161 t.Errorf("prg.Eval() succeeded and returned %v, expected runtime error accessing hidden field", out)
1162 }
1163}
1164
1165type TestNestedStruct struct {
1166 ListVal []*TestNestedType

Callers

nothing calls this directly

Calls 9

CompileMethod · 0.95
ProgramMethod · 0.95
VariableFunction · 0.92
NewEnvFunction · 0.92
NativeTypesFunction · 0.85
ParseStructTagFunction · 0.85
ErrMethod · 0.80
EvalMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected