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());
}
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());
}
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());
}
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);
}
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);
}
Aggregations