Search in sources :

Example 1 with ParsedExpr

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

the class CELTest method AstToProto.

@Test
void AstToProto() {
    Env stdEnv = newEnv(declarations(Decls.newVar("a", Decls.Dyn), Decls.newVar("b", Decls.Dyn)));
    AstIssuesTuple astIss = stdEnv.parse("a + b");
    assertThat(astIss.hasIssues()).isFalse();
    ParsedExpr parsed = astToParsedExpr(astIss.getAst());
    Ast ast2 = parsedExprToAst(parsed);
    assertThat(ast2.getExpr()).isEqualTo(astIss.getAst().getExpr());
    assertThatThrownBy(() -> astToCheckedExpr(astIss.getAst())).isInstanceOf(IllegalArgumentException.class).hasMessage("cannot convert unchecked ast");
    AstIssuesTuple astIss2 = stdEnv.check(astIss.getAst());
    assertThat(astIss2.hasIssues()).isFalse();
    // assertThat(astIss.hasIssues()).isFalse();
    CheckedExpr checked = astToCheckedExpr(astIss2.getAst());
    Ast ast3 = checkedExprToAst(checked);
    assertThat(ast3.getExpr()).isEqualTo(astIss2.getAst().getExpr());
}
Also used : CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 2 with ParsedExpr

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

the class ConformanceServerTest method fullPipeline.

/**
 * fullPipeline parses, checks, and evaluates the CEL expression in source and returns the result
 * from the Eval call.
 */
FullPipelineResult fullPipeline(String source) {
    // Parse
    ParseRequest preq = ParseRequest.newBuilder().setCelSource(source).build();
    ParseResponse pres = stub.parse(preq);
    assertThat(pres.isInitialized()).isTrue();
    ParsedExpr parsedExpr = pres.getParsedExpr();
    assertThat(parsedExpr.isInitialized()).isTrue();
    assertThat(parsedExpr.getExpr().isInitialized()).isTrue();
    // Check
    CheckRequest creq = CheckRequest.newBuilder().setParsedExpr(parsedExpr).build();
    CheckResponse cres = stub.check(creq);
    assertThat(cres.isInitialized()).isTrue();
    CheckedExpr checkedExpr = cres.getCheckedExpr();
    assertThat(checkedExpr.isInitialized()).isTrue();
    // Eval
    EvalRequest ereq = EvalRequest.newBuilder().setCheckedExpr(checkedExpr).build();
    EvalResponse eres = stub.eval(ereq);
    assertThat(eres.isInitialized()).isTrue();
    assertThat(eres.getResult().isInitialized()).isTrue();
    return new FullPipelineResult(pres, cres, eres);
}
Also used : EvalRequest(com.google.api.expr.v1alpha1.EvalRequest) ParseRequest(com.google.api.expr.v1alpha1.ParseRequest) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) CheckRequest(com.google.api.expr.v1alpha1.CheckRequest) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse) EvalResponse(com.google.api.expr.v1alpha1.EvalResponse)

Example 3 with ParsedExpr

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

the class Checker method Check.

/**
 * Check performs type checking, giving a typed AST. The input is a ParsedExpr proto and an env
 * which encapsulates type binding of variables, declarations of built-in functions, descriptions
 * of protocol buffers, and a registry for errors. Returns a CheckedExpr proto, which might not be
 * usable if there are errors in the error registry.
 */
public static CheckResult Check(ParseResult parsedExpr, Source source, CheckerEnv env) {
    TypeErrors errors = new TypeErrors(source);
    Checker c = new Checker(env, errors, newMapping(), 0, parsedExpr.getSourceInfo());
    Expr.Builder b = parsedExpr.getExpr().toBuilder();
    c.check(b);
    Expr e = b.build();
    // Walk over the final type map substituting any type parameters either by their bound value or
    // by DYN.
    Map<Long, Type> m = new HashMap<>();
    c.types.forEach((k, v) -> m.put(k, substitute(c.mappings, v, true)));
    CheckedExpr checkedExpr = CheckedExpr.newBuilder().setExpr(e).setSourceInfo(parsedExpr.getSourceInfo()).putAllTypeMap(m).putAllReferenceMap(c.references).build();
    return new CheckResult(checkedExpr, errors);
}
Also used : CheckerEnv.getObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType) CheckerEnv.isObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType) CheckerEnv.dynElementType(org.projectnessie.cel.checker.CheckerEnv.dynElementType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) HashMap(java.util.HashMap) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr)

Example 4 with ParsedExpr

use of com.google.api.expr.v1alpha1.ParsedExpr 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 5 with ParsedExpr

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

the class ConformanceServerTest method FullUp.

/**
 * TestFullUp tests Parse, Check, and Eval back-to-back.
 */
@Test
void FullUp() {
    ParseRequest preq = ParseRequest.newBuilder().setCelSource("x + y").build();
    ParseResponse pres = stub.parse(preq);
    assertThat(pres.isInitialized()).isTrue();
    ParsedExpr parsedExpr = pres.getParsedExpr();
    assertThat(parsedExpr.isInitialized()).isTrue();
    CheckRequest creq = CheckRequest.newBuilder().setParsedExpr(parsedExpr).addTypeEnv(Decls.newVar("x", Decls.Int)).addTypeEnv(Decls.newVar("y", Decls.Int)).build();
    CheckResponse cres = stub.check(creq);
    assertThat(cres.isInitialized()).isTrue();
    CheckedExpr checkedExpr = cres.getCheckedExpr();
    assertThat(checkedExpr.isInitialized()).isTrue();
    Type tp = checkedExpr.getTypeMapMap().get(1L);
    assertThat(tp).isNotNull();
    assertThat(tp.getTypeKindCase()).isSameAs(TypeKindCase.PRIMITIVE);
    assertThat(tp.getPrimitive()).isSameAs(PrimitiveType.INT64);
    EvalRequest ereq = EvalRequest.newBuilder().setCheckedExpr(checkedExpr).putBindings("x", exprValueInt64(1)).putBindings("y", exprValueInt64(2)).build();
    EvalResponse eres = stub.eval(ereq);
    assertThat(eres.isInitialized()).isTrue();
    assertThat(eres.getResult().isInitialized()).isTrue();
    assertThat(eres.getResult().getKindCase()).isSameAs(KindCase.VALUE);
    assertThat(eres.getResult().getValue().getKindCase()).isSameAs(Value.KindCase.INT64_VALUE);
    assertThat(eres.getResult().getValue().getInt64Value()).isEqualTo(3L);
}
Also used : EvalRequest(com.google.api.expr.v1alpha1.EvalRequest) PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) ParseRequest(com.google.api.expr.v1alpha1.ParseRequest) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) CheckRequest(com.google.api.expr.v1alpha1.CheckRequest) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse) EvalResponse(com.google.api.expr.v1alpha1.EvalResponse) 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