Search in sources :

Example 1 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class AttributesTest method attributesRelativeAttr_OneOf.

@Test
void attributesRelativeAttr_OneOf() {
    TypeRegistry reg = newRegistry();
    Container cont = newContainer(Container.name("acme.ns"));
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "acme.b", 1);
    Activation vars = newActivation(data);
    // The relative attribute under test is applied to a map literal:
    // {
    // a: {-1: [2, 42], b: 1}
    // b: 1
    // }
    // 
    // The expression being evaluated is: <map-literal>.a[-1][b] -> 42
    // 
    // However, since the test is validating what happens with maybe attributes
    // the attribute resolution must also consider the following variations:
    // - <map-literal>.a[-1][acme.ns.b]
    // - <map-literal>.a[-1][acme.b]
    // 
    // The correct behavior should yield the value of the last alternative.
    InterpretableConst op = newConstValue(1, reg.nativeToValue(data));
    Attribute attr = attrs.relativeAttribute(1, op);
    Qualifier qualA = attrs.newQualifier(null, 2, "a");
    Qualifier qualNeg1 = attrs.newQualifier(null, 3, intOf(-1));
    attr.addQualifier(qualA);
    attr.addQualifier(qualNeg1);
    attr.addQualifier(attrs.maybeAttribute(4, "b"));
    Object out = attr.resolve(vars);
    assertThat(out).isEqualTo(intOf(42));
    assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class AttributesTest method attributesOneofAttr.

@Test
void attributesOneofAttr() {
    TypeRegistry reg = newRegistry();
    Container cont = newContainer(Container.name("acme.ns"));
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Map<Object, Object> data = mapOf("a", mapOf("b", new int[] { 2, 42 }), "acme.a.b", 1, "acme.ns.a.b", "found");
    Activation vars = newActivation(data);
    // a.b -> should resolve to acme.ns.a.b per namespace resolution rules.
    Attribute attr = attrs.maybeAttribute(1, "a");
    Qualifier qualB = attrs.newQualifier(null, 2, "b");
    attr.addQualifier(qualB);
    Object out = attr.resolve(vars);
    assertThat(out).isEqualTo("found");
    assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) Attribute(org.projectnessie.cel.interpreter.AttributeFactory.Attribute) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class InterpreterTest method setProto2PrimitiveFields.

@Test
void setProto2PrimitiveFields() {
    // Test the use of proto2 primitives within object construction.
    Source src = newTextSource("input == TestAllTypes{\n" + "  single_int32: 1,\n" + "  single_int64: 2,\n" + "  single_uint32: 3u,\n" + "  single_uint64: 4u,\n" + "  single_float: -3.3,\n" + "  single_double: -2.2,\n" + "  single_string: \"hello world\",\n" + "  single_bool: true\n" + "}");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = testContainer("google.api.expr.test.v1.proto2");
    TypeRegistry reg = newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance());
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    env.add(singletonList(Decls.newVar("input", Decls.newObjectType("google.api.expr.test.v1.proto2.TestAllTypes"))));
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter i = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable eval = i.newInterpretable(checkResult.getCheckedExpr());
    int one = 1;
    long two = 2L;
    int three = 3;
    long four = 4L;
    float five = -3.3f;
    double six = -2.2d;
    String str = "hello world";
    boolean truth = true;
    com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes input = com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.newBuilder().setSingleInt32(one).setSingleInt64(two).setSingleUint32(three).setSingleUint64(four).setSingleFloat(five).setSingleDouble(six).setSingleString(str).setSingleBool(truth).build();
    Activation vars = newActivation(mapOf("input", reg.nativeToValue(input)));
    Val result = eval.eval(vars);
    assertThat(result.value()).isInstanceOf(Boolean.class);
    boolean got = (Boolean) result.value();
    assertThat(got).isTrue();
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TestAllTypes(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) ByteString(com.google.protobuf.ByteString) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class InterpreterTest method missingIdentInSelect.

@Test
void missingIdentInSelect() {
    Source src = newTextSource("a.b.c");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = testContainer("test");
    TypeRegistry reg = newRegistry();
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    env.add(Decls.newVar("a.b", Decls.Dyn));
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newPartialAttributeFactory(cont, reg, reg);
    Interpreter interp = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable i = interp.newInterpretable(checkResult.getCheckedExpr());
    Activation vars = newPartialActivation(mapOf("a.b", mapOf("d", "hello")), newAttributePattern("a.b").qualString("c"));
    Val result = i.eval(vars);
    assertThat(result).isInstanceOf(UnknownT.class);
    result = i.eval(emptyActivation());
    assertThat(result).isInstanceOf(Err.class);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 5 with Container

use of org.projectnessie.cel.common.containers.Container in project cel-java by projectnessie.

the class AttributesTest method attributesAbsoluteAttr.

@Test
void attributesAbsoluteAttr() {
    TypeRegistry reg = newRegistry();
    Container cont = newContainer(Container.name("acme.ns"));
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Activation vars = newActivation(mapOf("acme.a", mapOf("b", mapOf(4L, mapOf(false, "success")))));
    // acme.a.b[4][false]
    NamespacedAttribute attr = attrs.absoluteAttribute(1, "acme.a");
    Qualifier qualB = attrs.newQualifier(null, 2, "b");
    Qualifier qual4 = attrs.newQualifier(null, 3, 4L);
    Qualifier qualFalse = attrs.newQualifier(null, 4, false);
    attr.addQualifier(qualB);
    attr.addQualifier(qual4);
    attr.addQualifier(qualFalse);
    Object out = attr.resolve(vars);
    assertThat(out).isEqualTo("success");
    assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
Also used : Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) Qualifier(org.projectnessie.cel.interpreter.AttributeFactory.Qualifier) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Container (org.projectnessie.cel.common.containers.Container)14 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)13 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)13 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)12 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)12 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)11 Test (org.junit.jupiter.api.Test)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)10 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)10 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)10 Source (org.projectnessie.cel.common.Source)9 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)9 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)8 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)8 Interpreter.newInterpreter (org.projectnessie.cel.interpreter.Interpreter.newInterpreter)7 CheckResult (org.projectnessie.cel.checker.Checker.CheckResult)6 CheckerEnv.newStandardCheckerEnv (org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv)6 Val (org.projectnessie.cel.common.types.ref.Val)6 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)6