Search in sources :

Example 11 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class AttributesTest method attributesConditionalAttr_ErrorUnknown.

@Test
void attributesConditionalAttr_ErrorUnknown() {
    TypeRegistry reg = newRegistry();
    AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
    // err ? a : b
    NamespacedAttribute tv = attrs.absoluteAttribute(2, "a");
    Attribute fv = attrs.maybeAttribute(3, "b");
    Attribute cond = attrs.conditionalAttribute(1, newConstValue(0, newErr("test error")), tv, fv);
    Object out = cond.resolve(emptyActivation());
    assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
    // unk ? a : b
    Attribute condUnk = attrs.conditionalAttribute(1, newConstValue(0, unknownOf(1)), tv, fv);
    out = condUnk.resolve(emptyActivation());
    assertThat(out).isInstanceOf(UnknownT.class);
    assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
}
Also used : NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) 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) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry 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 13 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry 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 14 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class PbObjectTest method protoObjectIsSet.

@Test
void protoObjectIsSet() {
    TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
    ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
    Val obj = reg.nativeToValue(msg);
    assertThat(obj).isInstanceOf(ObjectT.class);
    ObjectT objVal = (ObjectT) obj;
    assertThat(objVal.isSet(stringOf("source_info"))).isSameAs(True);
    assertThat(objVal.isSet(stringOf("expr"))).isSameAs(False);
    assertThat(objVal.isSet(stringOf("bad_field"))).matches(Err::isError);
    assertThat(objVal.isSet(IntZero)).matches(Err::isError);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Err(org.projectnessie.cel.common.types.Err) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 15 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class ProviderTest method typeRegistryGetters.

@Test
void typeRegistryGetters() {
    TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
    Val sourceInfo = reg.newValue("google.api.expr.v1alpha1.SourceInfo", mapOf("location", stringOf("TestTypeRegistryGetFieldValue"), "line_offsets", newGenericArrayList(reg, new Long[] { 0L, 2L }), "positions", newMaybeWrappedMap(reg, mapOf(1L, 2L, 3L, 4L))));
    assertThat(sourceInfo).matches(v -> !Err.isError(v));
    Indexer si = (Indexer) sourceInfo;
    Val loc = si.get(stringOf("location"));
    assertThat(loc.equal(stringOf("TestTypeRegistryGetFieldValue"))).isSameAs(True);
    Val pos = si.get(stringOf("positions"));
    assertThat(pos.equal(newMaybeWrappedMap(reg, mapOf(1, 2, 3, 4)))).isSameAs(True);
    Val posKeyVal = ((Indexer) pos).get(intOf(1));
    assertThat(posKeyVal.intValue()).isEqualTo(2);
    Val offsets = si.get(stringOf("line_offsets"));
    assertThat(offsets).matches(v -> !Err.isError(v));
    Val offset1 = ((Lister) offsets).get(intOf(1));
    assertThat(offset1).matches(v -> !Err.isError(v)).isEqualTo(intOf(2));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Lister(org.projectnessie.cel.common.types.traits.Lister) Indexer(org.projectnessie.cel.common.types.traits.Indexer) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StringValue(com.google.protobuf.StringValue) ULong(org.projectnessie.cel.common.ULong) Disabled(org.junit.jupiter.api.Disabled) True(org.projectnessie.cel.common.types.BoolT.True) Arrays.asList(java.util.Arrays.asList) Duration(java.time.Duration) ProtoTypeRegistry.newEmptyRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newEmptyRegistry) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Val(org.projectnessie.cel.common.types.ref.Val) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) FloatValue(com.google.protobuf.FloatValue) False(org.projectnessie.cel.common.types.BoolT.False) DoubleT.doubleOf(org.projectnessie.cel.common.types.DoubleT.doubleOf) Expr(com.google.api.expr.v1alpha1.Expr) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Instant(java.time.Instant) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) StandardCharsets(java.nio.charset.StandardCharsets) UintT.uintOf(org.projectnessie.cel.common.types.UintT.uintOf) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) TimestampT.timestampOf(org.projectnessie.cel.common.types.TimestampT.timestampOf) BytesValue(com.google.protobuf.BytesValue) ListT.newGenericArrayList(org.projectnessie.cel.common.types.ListT.newGenericArrayList) NULL_VALUE(com.google.protobuf.NullValue.NULL_VALUE) ZoneIdZ(org.projectnessie.cel.common.types.TimestampT.ZoneIdZ) SourceInfo(com.google.api.expr.v1alpha1.SourceInfo) Int64Value(com.google.protobuf.Int64Value) BoolValue(com.google.protobuf.BoolValue) UInt64Value(com.google.protobuf.UInt64Value) DurationT.durationOf(org.projectnessie.cel.common.types.DurationT.durationOf) Timestamp(com.google.protobuf.Timestamp) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) ProtoTypeRegistry.newRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newRegistry) Calendar(java.util.Calendar) GlobalEnum(com.google.api.expr.test.v1.proto3.TestAllTypesProto.GlobalEnum) Constant(com.google.api.expr.v1alpha1.Constant) DoubleValue(com.google.protobuf.DoubleValue) Int32Value(com.google.protobuf.Int32Value) UInt32Value(com.google.protobuf.UInt32Value) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) BytesT.bytesOf(org.projectnessie.cel.common.types.BytesT.bytesOf) TimeUnit(java.util.concurrent.TimeUnit) IntZero(org.projectnessie.cel.common.types.IntT.IntZero) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) Message(com.google.protobuf.Message) Util.mapOf(org.projectnessie.cel.Util.mapOf) MapT.newMaybeWrappedMap(org.projectnessie.cel.common.types.MapT.newMaybeWrappedMap) NullValue(org.projectnessie.cel.common.types.NullT.NullValue) Indexer(org.projectnessie.cel.common.types.traits.Indexer) Lister(org.projectnessie.cel.common.types.traits.Lister) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)51 Test (org.junit.jupiter.api.Test)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)26 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)25 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)21 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)21 Val (org.projectnessie.cel.common.types.ref.Val)20 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)20 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)19 Qualifier (org.projectnessie.cel.interpreter.AttributeFactory.Qualifier)16 Container (org.projectnessie.cel.common.containers.Container)14 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)12 Attribute (org.projectnessie.cel.interpreter.AttributeFactory.Attribute)12 MethodSource (org.junit.jupiter.params.provider.MethodSource)11 ProtoTypeRegistry (org.projectnessie.cel.common.types.pb.ProtoTypeRegistry)10 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)10 Source (org.projectnessie.cel.common.Source)9 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)9 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)8