Search in sources :

Example 6 with ParsedExpr

use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.

the class Env method check.

/**
 * Check performs type-checking on the input Ast and yields a checked Ast and/or set of Issues.
 *
 * <p>Checking has failed if the returned Issues value and its Issues.Err() value are non-nil.
 * Issues should be inspected if they are non-nil, but may not represent a fatal error.
 *
 * <p>It is possible to have both non-nil Ast and Issues values returned from this call: however,
 * the mere presence of an Ast does not imply that it is valid for use.
 */
public AstIssuesTuple check(Ast ast) {
    // Note, errors aren't currently possible on the Ast to ParsedExpr conversion.
    ParsedExpr pe = astToParsedExpr(ast);
    // Construct the internal checker env, erroring if there is an issue adding the declarations.
    synchronized (once) {
        if (chk == null && chkErr == null) {
            CheckerEnv ce = CheckerEnv.newCheckerEnv(container, provider);
            ce.enableDynamicAggregateLiterals(true);
            if (hasFeature(FeatureDisableDynamicAggregateLiterals)) {
                ce.enableDynamicAggregateLiterals(false);
            }
            try {
                ce.add(declarations);
                chk = ce;
            } catch (RuntimeException e) {
                chkErr = e;
            } catch (Exception e) {
                chkErr = new RuntimeException(e);
            }
        }
    }
    // The once call will ensure that this value is set or nil for all invocations.
    if (chkErr != null) {
        Errors errs = new Errors(ast.getSource());
        errs.reportError(NoLocation, "%s", chkErr.toString());
        return new AstIssuesTuple(null, newIssues(errs));
    }
    ParseResult pr = new ParseResult(pe.getExpr(), new Errors(ast.getSource()), pe.getSourceInfo());
    CheckResult checkRes = Checker.Check(pr, ast.getSource(), chk);
    if (checkRes.hasErrors()) {
        return new AstIssuesTuple(null, newIssues(checkRes.getErrors()));
    }
    // Manually create the Ast to ensure that the Ast source information (which may be more
    // detailed than the information provided by Check), is returned to the caller.
    CheckedExpr ce = checkRes.getCheckedExpr();
    ast = new Ast(ce.getExpr(), ce.getSourceInfo(), ast.getSource(), ce.getReferenceMapMap(), ce.getTypeMapMap());
    return new AstIssuesTuple(ast, Issues.noIssues(ast.getSource()));
}
Also used : Errors(org.projectnessie.cel.common.Errors) AstPruner.pruneAst(org.projectnessie.cel.interpreter.AstPruner.pruneAst) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr)

Example 7 with ParsedExpr

use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.

the class ProviderTest method convertToNative.

@Test
void convertToNative() {
    TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
    // Core type conversion tests.
    expectValueToNative(True, true);
    expectValueToNative(True, True);
    expectValueToNative(newGenericArrayList(reg, new Val[] { True, False }), new Object[] { true, false });
    expectValueToNative(newGenericArrayList(reg, new Val[] { True, False }), new Val[] { True, False });
    expectValueToNative(intOf(-1), -1);
    expectValueToNative(intOf(2), 2L);
    expectValueToNative(intOf(-1), -1);
    expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(4) }), new Object[] { 4L });
    expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(5) }), new Val[] { intOf(5) });
    expectValueToNative(uintOf(3), ULong.valueOf(3));
    expectValueToNative(uintOf(4), ULong.valueOf(4));
    expectValueToNative(uintOf(5), 5);
    expectValueToNative(newGenericArrayList(reg, new Val[] { uintOf(4) }), // loses "ULong" here
    new Object[] { 4L });
    expectValueToNative(newGenericArrayList(reg, new Val[] { uintOf(5) }), new Val[] { uintOf(5) });
    expectValueToNative(doubleOf(5.5d), 5.5f);
    expectValueToNative(doubleOf(-5.5d), -5.5d);
    expectValueToNative(newGenericArrayList(reg, new Val[] { doubleOf(-5.5) }), new Object[] { -5.5 });
    expectValueToNative(newGenericArrayList(reg, new Val[] { doubleOf(-5.5) }), new Val[] { doubleOf(-5.5) });
    expectValueToNative(doubleOf(-5.5), doubleOf(-5.5));
    expectValueToNative(stringOf("hello"), "hello");
    expectValueToNative(stringOf("hello"), stringOf("hello"));
    expectValueToNative(NullValue, NULL_VALUE);
    expectValueToNative(NullValue, NullValue);
    expectValueToNative(newGenericArrayList(reg, new Val[] { NullValue }), new Object[] { null });
    expectValueToNative(newGenericArrayList(reg, new Val[] { NullValue }), new Val[] { NullValue });
    expectValueToNative(bytesOf("world"), "world".getBytes(StandardCharsets.UTF_8));
    expectValueToNative(bytesOf("world"), "world".getBytes(StandardCharsets.UTF_8));
    expectValueToNative(newGenericArrayList(reg, new Val[] { bytesOf("hello") }), new Object[] { ByteString.copyFromUtf8("hello") });
    expectValueToNative(newGenericArrayList(reg, new Val[] { bytesOf("hello") }), new Val[] { bytesOf("hello") });
    expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(1), intOf(2), intOf(3) }), new Object[] { 1L, 2L, 3L });
    expectValueToNative(durationOf(Duration.ofSeconds(500)), Duration.ofSeconds(500));
    expectValueToNative(durationOf(Duration.ofSeconds(500)), com.google.protobuf.Duration.newBuilder().setSeconds(500).build());
    expectValueToNative(durationOf(Duration.ofSeconds(500)), durationOf(Duration.ofSeconds(500)));
    expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), Instant.ofEpochSecond(12345, 0).atZone(ZoneIdZ));
    expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), timestampOf(Timestamp.newBuilder().setSeconds(12345).build()));
    expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), Timestamp.newBuilder().setSeconds(12345).build());
    expectValueToNative(newMaybeWrappedMap(reg, mapOf(1L, 1L, 2L, 1L, 3L, 1L)), mapOf(1L, 1L, 2L, 1L, 3L, 1L));
    // Null conversion tests.
    expectValueToNative(NullValue, NULL_VALUE);
    // Proto conversion tests.
    ParsedExpr parsedExpr = ParsedExpr.getDefaultInstance();
    expectValueToNative(reg.nativeToValue(parsedExpr), parsedExpr);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 8 with ParsedExpr

use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.

the class PbObjectTest method protoObjectConvertToType.

@Test
void protoObjectConvertToType() {
    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;
    Type tv = objVal.type();
    assertThat(objVal.convertToType(TypeType).equal(tv)).isSameAs(True);
    assertThat(objVal.convertToType(objVal.type())).isSameAs(objVal);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TypeType(org.projectnessie.cel.common.types.TypeT.TypeType) Type(org.projectnessie.cel.common.types.ref.Type) 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 9 with ParsedExpr

use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.

the class PbObjectTest method protoObjectConvertToNative.

@Test
void protoObjectConvertToNative() throws Exception {
    TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
    ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
    Val objVal = reg.nativeToValue(msg);
    // Proto Message
    ParsedExpr val = objVal.convertToNative(ParsedExpr.class);
    assertThat(val).isEqualTo(msg);
    // Dynamic protobuf
    Val dynPB = reg.newValue(msg.getDescriptorForType().getFullName(), Collections.singletonMap("source_info", reg.nativeToValue(msg.getSourceInfo())));
    Val dynVal = reg.nativeToValue(dynPB);
    val = dynVal.convertToNative(msg.getClass());
    assertThat(val).isEqualTo(msg);
    // google.protobuf.Any
    Any anyVal = objVal.convertToNative(Any.class);
    Message unpackedAny = anyVal.unpack(ParsedExpr.class);
    assertThat(unpackedAny).isEqualTo(objVal.value());
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Message(com.google.protobuf.Message) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Any(com.google.protobuf.Any) Test(org.junit.jupiter.api.Test)

Example 10 with ParsedExpr

use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.

the class PbObjectTest method protoObjectGet.

@Test
void protoObjectGet() {
    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.get(stringOf("source_info")).equal(reg.nativeToValue(msg.getSourceInfo()))).isSameAs(True);
    assertThat(objVal.get(stringOf("expr")).equal(reg.nativeToValue(Expr.getDefaultInstance()))).isSameAs(True);
    assertThat(objVal.get(stringOf("bad_field"))).matches(Err::isError);
    assertThat(objVal.get(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

ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)10 Test (org.junit.jupiter.api.Test)8 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)5 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)5 Val (org.projectnessie.cel.common.types.ref.Val)5 ObjectT (org.projectnessie.cel.common.types.ObjectT)3 CheckRequest (com.google.api.expr.v1alpha1.CheckRequest)2 CheckResponse (com.google.api.expr.v1alpha1.CheckResponse)2 EvalRequest (com.google.api.expr.v1alpha1.EvalRequest)2 EvalResponse (com.google.api.expr.v1alpha1.EvalResponse)2 ParseRequest (com.google.api.expr.v1alpha1.ParseRequest)2 ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)2 Type (com.google.api.expr.v1alpha1.Type)2 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)2 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)2 Err (org.projectnessie.cel.common.types.Err)2 Expr (com.google.api.expr.v1alpha1.Expr)1 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)1 Any (com.google.protobuf.Any)1