Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 of 3 tasks
sergiitk opened this issue Nov 25, 2024 · 1 comment · Fixed by #506
Closed
2 of 3 tasks

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

sergiitk opened this issue Nov 25, 2024 · 1 comment · Fixed by #506

Comments

@sergiitk
Copy link
Member

sergiitk commented Nov 25, 2024

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

@sergiitk
Copy link
Member Author

FYI @TristonianJones @l46kok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant