use of com.google.api.expr.v1alpha1.ParseRequest 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.ParseRequest in project cel-java by projectnessie.
the class ConformanceServiceImpl method parse.
@Override
public void parse(ParseRequest request, io.grpc.stub.StreamObserver<ParseResponse> responseObserver) {
try {
String sourceText = request.getCelSource();
if (sourceText.trim().isEmpty()) {
throw new IllegalArgumentException("No source code.");
}
// NOTE: syntax_version isn't currently used
List<EnvOption> parseOptions = new ArrayList<>();
if (request.getDisableMacros()) {
parseOptions.add(clearMacros());
}
Env env = newEnv(parseOptions.toArray(new EnvOption[0]));
AstIssuesTuple astIss = env.parse(sourceText);
ParseResponse.Builder response = ParseResponse.newBuilder();
if (!astIss.hasIssues()) {
// Success
response.setParsedExpr(astToParsedExpr(astIss.getAst()));
} else {
// Failure
appendErrors(astIss.getIssues().getErrors(), response::addIssuesBuilder);
}
responseObserver.onNext(response.build());
responseObserver.onCompleted();
} catch (Exception e) {
responseObserver.onError(io.grpc.Status.fromCode(io.grpc.Status.Code.UNKNOWN).withDescription(stacktrace(e)).asException());
}
}
use of com.google.api.expr.v1alpha1.ParseRequest in project cel-java by projectnessie.
the class ConformanceServerTest method Parse.
/**
* TestParse tests the Parse method.
*/
@Test
void Parse() {
ParseRequest req = ParseRequest.newBuilder().setCelSource("1 + 1").build();
ParseResponse res = stub.parse(req);
assertThat(res.isInitialized()).isTrue();
assertThat(res.getParsedExpr().isInitialized()).isTrue();
// Could check against 'parsed' above,
// but the expression ids are arbitrary,
// and explicit comparison logic is about as
// much work as normalization would be.
assertThat(res.getParsedExpr().getExpr().isInitialized()).isTrue();
assertThat(res.getParsedExpr().getExpr().getExprKindCase()).isSameAs(ExprKindCase.CALL_EXPR);
Call c = res.getParsedExpr().getExpr().getCallExpr();
assertThat(c.getTarget().isInitialized()).isTrue();
assertThat(c.getFunction()).isEqualTo("_+_");
assertThat(c.getArgsCount()).isEqualTo(2);
for (Expr a : c.getArgsList()) {
assertThat(a.getExprKindCase()).isSameAs(ExprKindCase.CONST_EXPR);
Constant l = a.getConstExpr();
assertThat(l.getConstantKindCase()).isSameAs(ConstantKindCase.INT64_VALUE);
assertThat(l.getInt64Value()).isEqualTo(1);
}
}
use of com.google.api.expr.v1alpha1.ParseRequest 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