Search in sources :

Example 1 with CheckedExpr

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

the class CELTest method CheckedExprToAst_ConstantExpr.

@Test
void CheckedExprToAst_ConstantExpr() {
    Env stdEnv = newEnv();
    String in = "10";
    AstIssuesTuple astIss = stdEnv.compile(in);
    assertThat(astIss.hasIssues()).isFalse();
    CheckedExpr expr = astToCheckedExpr(astIss.getAst());
    Ast ast2 = checkedExprToAst(expr);
    assertThat(ast2.getExpr()).isEqualTo(astIss.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) CEL.astToString(org.projectnessie.cel.CEL.astToString) 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 CheckedExpr

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

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

the class CELTest method AstIsChecked.

@Test
void AstIsChecked() {
    Env e = newEnv();
    AstIssuesTuple astIss = e.compile("true");
    assertThat(astIss.hasIssues()).isFalse();
    assertThat(astIss.getAst()).extracting(Ast::isChecked).isEqualTo(true);
    CheckedExpr ce = astToCheckedExpr(astIss.getAst());
    Ast ast2 = checkedExprToAst(ce);
    assertThat(ast2).extracting(Ast::isChecked).isEqualTo(true);
    assertThat(astIss.getAst().getExpr()).isEqualTo(ast2.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) 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 4 with CheckedExpr

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

use of com.google.api.expr.v1alpha1.CheckedExpr 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)

Aggregations

CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)9 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)5 Test (org.junit.jupiter.api.Test)5 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)4 CEL.astToCheckedExpr (org.projectnessie.cel.CEL.astToCheckedExpr)3 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)3 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)3 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)3 Env.newEnv (org.projectnessie.cel.Env.newEnv)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 Expr (com.google.api.expr.v1alpha1.Expr)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 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)1