Search in sources :

Example 16 with Val

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

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

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

the class TimestampTest method timestampGetDayOfYear.

@Test
void timestampGetDayOfYear() {
    // 1970-01-01T02:05:06Z
    TimestampT ts = timestampOf(Instant.ofEpochSecond(7506).atZone(ZoneIdZ));
    Val hr = ts.receive(Overloads.TimeGetDayOfYear, Overloads.TimestampToDayOfYear);
    assertThat(hr).isSameAs(IntZero);
    // 1969-12-31T19:05:06Z
    Val hrTz = ts.receive(Overloads.TimeGetDayOfYear, Overloads.TimestampToDayOfYearWithTz, stringOf("America/Phoenix"));
    assertThat(hrTz.equal(intOf(364))).isSameAs(True);
    hrTz = ts.receive(Overloads.TimeGetDayOfYear, Overloads.TimestampToDayOfYearWithTz, stringOf("-07:00"));
    assertThat(hrTz.equal(intOf(364))).isSameAs(True);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with Val

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

the class TypeTest method typeConvertToType.

@Test
void typeConvertToType() {
    Type[] stdTypes = new Type[] { BoolType, BytesType, DoubleType, DurationType, IntType, ListType, MapType, NullType, StringType, TimestampType, TypeType, UintType };
    for (Type stdType : stdTypes) {
        Val cnv = stdType.convertToType(TypeType);
        assertThat(cnv).isEqualTo(TypeType);
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TypeType(org.projectnessie.cel.common.types.TypeT.TypeType) BytesType(org.projectnessie.cel.common.types.BytesT.BytesType) DoubleType(org.projectnessie.cel.common.types.DoubleT.DoubleType) DurationType(org.projectnessie.cel.common.types.DurationT.DurationType) IntType(org.projectnessie.cel.common.types.IntT.IntType) ListType(org.projectnessie.cel.common.types.ListT.ListType) StringType(org.projectnessie.cel.common.types.StringT.StringType) MapType(org.projectnessie.cel.common.types.MapT.MapType) NullType(org.projectnessie.cel.common.types.NullT.NullType) BoolType(org.projectnessie.cel.common.types.BoolT.BoolType) Type(org.projectnessie.cel.common.types.ref.Type) TimestampType(org.projectnessie.cel.common.types.TimestampT.TimestampType) UintType(org.projectnessie.cel.common.types.UintT.UintType) Test(org.junit.jupiter.api.Test)

Example 20 with Val

use of org.projectnessie.cel.common.types.ref.Val 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)

Aggregations

Val (org.projectnessie.cel.common.types.ref.Val)67 Test (org.junit.jupiter.api.Test)42 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)16 ByteString (com.google.protobuf.ByteString)10 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 Message (com.google.protobuf.Message)8 Err (org.projectnessie.cel.common.types.Err)8 ObjectT (org.projectnessie.cel.common.types.ObjectT)7 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)7 Expr (com.google.api.expr.v1alpha1.Expr)6 Source (org.projectnessie.cel.common.Source)6 Mapper (org.projectnessie.cel.common.types.traits.Mapper)6 HashMap (java.util.HashMap)5 EvalResult (org.projectnessie.cel.Program.EvalResult)5 True (org.projectnessie.cel.common.types.BoolT.True)5 StringT.stringOf (org.projectnessie.cel.common.types.StringT.stringOf)5 ProtoTypeRegistry (org.projectnessie.cel.common.types.pb.ProtoTypeRegistry)5 Call (com.google.api.expr.v1alpha1.Expr.Call)4