Skip to content

Commit bfccebd

Browse files
Fix functional exemptions for homogeneous literal checks (#832)
1 parent 705546a commit bfccebd

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

cel/validator.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ func (v homogeneousAggregateLiteralValidator) Validate(_ *Env, c ValidatorConfig
304304
}
305305

306306
func inExemptFunction(e ast.NavigableExpr, exemptFunctions []string) bool {
307-
if parent, found := e.Parent(); found {
307+
parent, found := e.Parent()
308+
for found {
308309
if parent.Kind() == ast.CallKind {
309310
fnName := parent.AsCall().FunctionName()
310311
for _, exempt := range exemptFunctions {
@@ -313,9 +314,7 @@ func inExemptFunction(e ast.NavigableExpr, exemptFunctions []string) bool {
313314
}
314315
}
315316
}
316-
if parent.Kind() == ast.ListKind || parent.Kind() == ast.MapKind {
317-
return inExemptFunction(parent, exemptFunctions)
318-
}
317+
parent, found = parent.Parent()
319318
}
320319
return false
321320
}

ext/strings_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,50 @@ func TestStringFormat(t *testing.T) {
13181318
}
13191319
}
13201320

1321+
func TestStringFormatHeterogeneousLiterals(t *testing.T) {
1322+
tests := []struct {
1323+
expr string
1324+
out string
1325+
}{
1326+
{
1327+
expr: `"list: %s".format([[[1, 2, [3.0, 4]]]])`,
1328+
out: `list: [[1, 2, [3.000000, 4]]]`,
1329+
},
1330+
{
1331+
expr: `"list size: %d".format([[[1, 2, [3.0, 4]]].size()])`,
1332+
out: `list size: 1`,
1333+
},
1334+
{
1335+
expr: `"list element: %s".format([[[1, 2, [3.0, 4]]][0]])`,
1336+
out: `list element: [1, 2, [3.000000, 4]]`,
1337+
},
1338+
}
1339+
env, err := cel.NewEnv(Strings(), cel.ASTValidators(cel.ValidateHomogeneousAggregateLiterals()))
1340+
if err != nil {
1341+
t.Fatalf("cel.NewEnv() failed: %v", err)
1342+
}
1343+
for _, tst := range tests {
1344+
tc := tst
1345+
t.Run(tc.expr, func(t *testing.T) {
1346+
ast, iss := env.Compile(tc.expr)
1347+
if iss.Err() != nil {
1348+
t.Fatalf("env.Compile(%q) failed: %v", tc.expr, iss.Err())
1349+
}
1350+
prg, err := env.Program(ast)
1351+
if err != nil {
1352+
t.Fatalf("env.Program() failed: %v", err)
1353+
}
1354+
out, _, err := prg.Eval(cel.NoVars())
1355+
if err != nil {
1356+
t.Fatalf("Eval() failed: %v", err)
1357+
}
1358+
if out.Value() != tc.out {
1359+
t.Errorf("Eval() got %v, wanted %v", out, tc.out)
1360+
}
1361+
})
1362+
}
1363+
}
1364+
13211365
func TestBadLocale(t *testing.T) {
13221366
_, err := cel.NewEnv(Strings(StringsLocale("bad-locale")))
13231367
if err != nil {

0 commit comments

Comments
 (0)