Skip to content

@in operator is unable to be applied for (int, list(uint)) #416

@buildbreaker

Description

@buildbreaker

I am looking to use cel-java to evaluate if a value is in a list. The problem that I'm running into is the different conversions between the different types. This fails on the Script build line:

check failed: ERROR: <input>:1:5: found no matching overload for '@in' applied to '(int, list(uint))'
 | !(3 in rules.repeated_fixed32) ? 'value must be in list' : ''
 | ....^
org.projectnessie.cel.tools.ScriptCreateException: check failed: ERROR: <input>:1:5: found no matching overload for '@in' applied to '(int, list(uint))'
 | !(3 in rules.repeated_fixed32) ? 'value must be in list' : ''
 | ....^

What I am also noticing is that if I change the script to be a unit (intList() test), it still fails due to cel-java interpreting the list type to be int rather than uint. Instead of failing on build time, it incorrectly materializes the uint list as a list of integers, then it cannot find the unit in the list because of the type difference.. From my investigation it seems like this is intentional behavior as we do end up hitting this code path for equality between UintT and IntT.

Here are tests that I am using to demonstrate this behavior:

  @Test
  public void uintList() throws ScriptException {
    TestAllTypesProto.TestAllTypes rule = TestAllTypesProto.TestAllTypes.newBuilder()
            .addRepeatedFixed32(2)
            .addRepeatedFixed32(3)
            .build();
    ScriptHost scriptHost = ScriptHost.newBuilder().build();
    Script script =
        scriptHost
            .buildScript("!(3 in rules.repeated_fixed32) ? 'value must be in list' : ''")
            .withTypes(rule.getDefaultInstanceForType())
            .withDeclarations(
                Decls.newVar(
                    "rules", Decls.newObjectType(rule.getDescriptorForType().getFullName())))
            .build();
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("rules", rule);

    String result = script.execute(String.class, arguments);
    assertThat(result).isEqualTo("");
  }

  @Test
  public void intList() throws ScriptException {
    TestAllTypesProto.TestAllTypes rule = TestAllTypesProto.TestAllTypes.newBuilder()
            .addRepeatedFixed32(2)
            .addRepeatedFixed32(3)
            .build();
    ScriptHost scriptHost = ScriptHost.newBuilder().build();
    Script script =
            scriptHost
                    .buildScript("!(3u in rules.repeated_fixed32) ? 'value must be in list' : ''")
                    .withTypes(rule.getDefaultInstanceForType())
                    .withDeclarations(
                            Decls.newVar(
                                    "rules", Decls.newObjectType(rule.getDescriptorForType().getFullName())))
                    .build();
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("rules", rule);

    String result = script.execute(String.class, arguments);
    assertThat(result).isEqualTo("");
  }

Thank you for your quick responses!

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