Skip to content

Commit

Permalink
Fix functional exemptions for homogeneous literal checks (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristonianJones committed Aug 31, 2023
1 parent 705546a commit bfccebd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
7 changes: 3 additions & 4 deletions cel/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ func (v homogeneousAggregateLiteralValidator) Validate(_ *Env, c ValidatorConfig
}

func inExemptFunction(e ast.NavigableExpr, exemptFunctions []string) bool {
if parent, found := e.Parent(); found {
parent, found := e.Parent()
for found {
if parent.Kind() == ast.CallKind {
fnName := parent.AsCall().FunctionName()
for _, exempt := range exemptFunctions {
Expand All @@ -313,9 +314,7 @@ func inExemptFunction(e ast.NavigableExpr, exemptFunctions []string) bool {
}
}
}
if parent.Kind() == ast.ListKind || parent.Kind() == ast.MapKind {
return inExemptFunction(parent, exemptFunctions)
}
parent, found = parent.Parent()
}
return false
}
Expand Down
44 changes: 44 additions & 0 deletions ext/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,50 @@ func TestStringFormat(t *testing.T) {
}
}

func TestStringFormatHeterogeneousLiterals(t *testing.T) {
tests := []struct {
expr string
out string
}{
{
expr: `"list: %s".format([[[1, 2, [3.0, 4]]]])`,
out: `list: [[1, 2, [3.000000, 4]]]`,
},
{
expr: `"list size: %d".format([[[1, 2, [3.0, 4]]].size()])`,
out: `list size: 1`,
},
{
expr: `"list element: %s".format([[[1, 2, [3.0, 4]]][0]])`,
out: `list element: [1, 2, [3.000000, 4]]`,
},
}
env, err := cel.NewEnv(Strings(), cel.ASTValidators(cel.ValidateHomogeneousAggregateLiterals()))
if err != nil {
t.Fatalf("cel.NewEnv() failed: %v", err)
}
for _, tst := range tests {
tc := tst
t.Run(tc.expr, func(t *testing.T) {
ast, iss := env.Compile(tc.expr)
if iss.Err() != nil {
t.Fatalf("env.Compile(%q) failed: %v", tc.expr, iss.Err())
}
prg, err := env.Program(ast)
if err != nil {
t.Fatalf("env.Program() failed: %v", err)
}
out, _, err := prg.Eval(cel.NoVars())
if err != nil {
t.Fatalf("Eval() failed: %v", err)
}
if out.Value() != tc.out {
t.Errorf("Eval() got %v, wanted %v", out, tc.out)
}
})
}
}

func TestBadLocale(t *testing.T) {
_, err := cel.NewEnv(Strings(StringsLocale("bad-locale")))
if err != nil {
Expand Down

0 comments on commit bfccebd

Please sign in to comment.