Search in sources :

Example 1 with ParseResponse

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

use of com.google.api.expr.v1alpha1.ParseResponse 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());
    }
}
Also used : EnvOption(org.projectnessie.cel.EnvOption) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env(org.projectnessie.cel.Env) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse)

Example 3 with ParseResponse

use of com.google.api.expr.v1alpha1.ParseResponse 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);
    }
}
Also used : Call(com.google.api.expr.v1alpha1.Expr.Call) ExprCall(org.projectnessie.cel.TestExpr.ExprCall) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) Constant(com.google.api.expr.v1alpha1.Constant) ParseRequest(com.google.api.expr.v1alpha1.ParseRequest) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ParseResponse

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

ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)4 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)3 ParseRequest (com.google.api.expr.v1alpha1.ParseRequest)3 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)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 Test (org.junit.jupiter.api.Test)2 Constant (com.google.api.expr.v1alpha1.Constant)1 Expr (com.google.api.expr.v1alpha1.Expr)1 Call (com.google.api.expr.v1alpha1.Expr.Call)1 Type (com.google.api.expr.v1alpha1.Type)1 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 Env (org.projectnessie.cel.Env)1 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)1 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)1 Env.newEnv (org.projectnessie.cel.Env.newEnv)1