Skip to content

Support CelOptions.enableComprehension(boolean) for feature parity with CEL-Cpp #484

@sergiitk

Description

@sergiitk

Feature request checklist

  • There are no issues that match the desired change
  • The change is large enough it can't be addressed with a simple Pull Request
  • If this is a bug, please file a Bug Report.

Change
To make CEL environment setup consistent across CEL implementations, I propose to add a runtime option equivalent to CEL-cpp InterpreterOptions.enable_comprehension to CEL-Java and CEL-Go (corresponding ticket google/cel-go#1050).

CelOptions.enableComprehension is true by default. Setting it to false will disable comprehension expressions (e.g. exists(), all() -- except has()). Under-the-hood it may simply call comprehensionMaxIterations(0) to disable comprehension expressions, and reset it to the default value to enable them.

Example

private static final CelRuntime CEL_RUNTIME = CelRuntimeFactory
    .standardCelRuntimeBuilder()
    .setOptions(CelOptions.current().enableComprehension(false).build())
    .build();
@Test
public void macros_comprehensionsDisabled() throws Exception {
  CelCompiler CEL_COMPILER = CelCompilerFactory.standardCelCompilerBuilder()
    .addVar("request", SimpleType.ANY)
    .setResultType(SimpleType.BOOL)
    .setStandardMacros(CelStandardMacro.STANDARD_MACROS)
    .build();

  String expr = "size(['foo', 'bar'].map(x, request[x])) == 2";
  CelRuntime.Program program = CEL_RUNTIME.createProgram(CEL_COMPILER.compile(expr).getAst());

  Map<String, ?> input = ImmutableMap.of("request", ImmutableMap.of("foo", "f", "bar", "b"));
  CelEvaluationException celErr = assertThrows(CelEvaluationException.class, () -> program.eval(input));

  assertThat(celErr.getErrorCode()).isEqualTo(CelErrorCode.ITERATION_BUDGET_EXCEEDED);
}

Related
Internal ref: go/grpc-cel-integration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions