Skip to content

Support CelOptions to disable string conversion and list/string concatenation #502

@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 equivalent runtime options that control string conversion and list/string concatenation equivalent to those found in CEL-cpp:

// Enable string() overloads.
bool enable_string_conversion = true;

// Enable string concatenation overload.
bool enable_string_concat = true;

// Enable list concatenation overload.
bool enable_list_concat = true;

Example

CelCompiler CEL_COMPILER = CelCompilerFactory.standardCelCompilerBuilder()
    .setResultType(SimpleType.BOOL)
    .build();

@Test
public void cel_stringConversionDisabled() throws Exception {
  CelRuntime CEL_RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder()
      .setOptions(CelOptions.current().enableStringConversion(false).build())
      .build();

  // TODO: verify conversions to all types.
  String expr = "string(3.14) == '3.14'";
  CelRuntime.Program program = CEL_RUNTIME.createProgram(CEL_COMPILER.compile(expr).getAst());
  CelEvaluationException celErr = assertThrows(CelEvaluationException.class, program::eval);
  assertThat(celErr.getErrorCode()).isEqualTo(CelErrorCode.OVERLOAD_NOT_FOUND);
}

@Test
public void cel_stringConcatDisabled() throws Exception {
  CelRuntime CEL_RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder()
      .setOptions(CelOptions.current().enableStringConcat(false).build())
      .build();

  String expr = "'ab' + 'cd' == 'abcd'";
  CelRuntime.Program program = CEL_RUNTIME.createProgram(CEL_COMPILER.compile(expr).getAst());
  CelEvaluationException celErr = assertThrows(CelEvaluationException.class, program::eval);
  assertThat(celErr.getErrorCode()).isEqualTo(CelErrorCode.OVERLOAD_NOT_FOUND);
}

@Test
public void cel_listConcatDisabled() throws Exception {
  CelRuntime CEL_RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder()
      .setOptions(CelOptions.current().enableListConcat(false).build())
      .build();

  String expr = "size([1, 2] + [3, 4]) == 4";
  CelRuntime.Program program = CEL_RUNTIME.createProgram(CEL_COMPILER.compile(expr).getAst());
  CelEvaluationException celErr = assertThrows(CelEvaluationException.class, program::eval);
  assertThat(celErr.getErrorCode()).isEqualTo(CelErrorCode.OVERLOAD_NOT_FOUND);
}

Related

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