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