Search in sources :

Example 1 with CheckResponse

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

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

the class ConformanceServiceImpl method check.

@Override
public void check(CheckRequest request, io.grpc.stub.StreamObserver<CheckResponse> responseObserver) {
    try {
        // Build the environment.
        List<EnvOption> checkOptions = new ArrayList<>();
        if (!request.getNoStdEnv()) {
            checkOptions.add(StdLib());
        }
        checkOptions.add(container(request.getContainer()));
        checkOptions.add(declarations(request.getTypeEnvList()));
        checkOptions.add(types(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance(), com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance()));
        Env env = newCustomEnv(checkOptions.toArray(new EnvOption[0]));
        // Check the expression.
        AstIssuesTuple astIss = env.check(parsedExprToAst(request.getParsedExpr()));
        CheckResponse.Builder resp = CheckResponse.newBuilder();
        if (!astIss.hasIssues()) {
            // Success
            resp.setCheckedExpr(astToCheckedExpr(astIss.getAst()));
        } else {
            // Failure
            appendErrors(astIss.getIssues().getErrors(), resp::addIssuesBuilder);
        }
        responseObserver.onNext(resp.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) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) 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)

Example 3 with CheckResponse

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

the class ConformanceServerTest method Check.

/**
 * TestCheck tests the Check method.
 */
@Test
void Check() {
    // If TestParse() passes, it validates a good chunk
    // of the server mechanisms for data conversion, so we
    // won't be as fussy here..
    CheckRequest req = CheckRequest.newBuilder().setParsedExpr(parsed).build();
    CheckResponse res = stub.check(req);
    assertThat(res.isInitialized()).isTrue();
    assertThat(res.getCheckedExpr().isInitialized()).isTrue();
    Type tp = res.getCheckedExpr().getTypeMapMap().get(1L);
    assertThat(tp).isNotNull();
    assertThat(tp.getTypeKindCase()).isSameAs(TypeKindCase.PRIMITIVE);
    assertThat(tp.getPrimitive()).isSameAs(PrimitiveType.INT64);
}
Also used : PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) CheckRequest(com.google.api.expr.v1alpha1.CheckRequest) Test(org.junit.jupiter.api.Test)

Example 4 with CheckResponse

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

CheckResponse (com.google.api.expr.v1alpha1.CheckResponse)4 CheckRequest (com.google.api.expr.v1alpha1.CheckRequest)3 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)2 EvalRequest (com.google.api.expr.v1alpha1.EvalRequest)2 EvalResponse (com.google.api.expr.v1alpha1.EvalResponse)2 ParseRequest (com.google.api.expr.v1alpha1.ParseRequest)2 ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)2 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)2 Type (com.google.api.expr.v1alpha1.Type)2 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)2 Test (org.junit.jupiter.api.Test)2 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 EnvOption (org.projectnessie.cel.EnvOption)1