Search in sources :

Example 1 with ExprValue

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

the class ConformanceServiceImpl method eval.

@Override
public void eval(EvalRequest request, io.grpc.stub.StreamObserver<EvalResponse> responseObserver) {
    try {
        Env env = newEnv(container(request.getContainer()), types(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance(), com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance()));
        Program prg;
        Ast ast;
        switch(request.getExprKindCase()) {
            case PARSED_EXPR:
                ast = parsedExprToAst(request.getParsedExpr());
                break;
            case CHECKED_EXPR:
                ast = checkedExprToAst(request.getCheckedExpr());
                break;
            default:
                throw new IllegalArgumentException("No expression.");
        }
        prg = env.program(ast);
        Map<String, Object> args = new HashMap<>();
        request.getBindingsMap().forEach((name, exprValue) -> {
            Val refVal = exprValueToRefValue(env.getTypeAdapter(), exprValue);
            args.put(name, refVal);
        });
        // NOTE: the EvalState is currently discarded
        EvalResult res = prg.eval(args);
        ExprValue resultExprVal;
        if (!isError(res.getVal())) {
            resultExprVal = refValueToExprValue(res.getVal());
        } else {
            Err err = (Err) res.getVal();
            if (verboseEvalErrors) {
                System.err.printf("%n" + "Eval error (not necessarily a bug!!!):%n" + "  error: %s%n" + "%s", err, err.hasCause() ? (stacktrace(err.toRuntimeException()) + "\n") : "");
            }
            resultExprVal = ExprValue.newBuilder().setError(ErrorSet.newBuilder().addErrors(Status.newBuilder().setMessage(err.toString()))).build();
        }
        EvalResponse.Builder resp = EvalResponse.newBuilder().setResult(resultExprVal);
        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 : Val(org.projectnessie.cel.common.types.ref.Val) Program(org.projectnessie.cel.Program) Ast(org.projectnessie.cel.Ast) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) Err(org.projectnessie.cel.common.types.Err) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) HashMap(java.util.HashMap) EvalResult(org.projectnessie.cel.Program.EvalResult) ByteString(com.google.protobuf.ByteString) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env(org.projectnessie.cel.Env) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) EvalResponse(com.google.api.expr.v1alpha1.EvalResponse) ExprValue(com.google.api.expr.v1alpha1.ExprValue)

Example 2 with ExprValue

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

the class ConformanceServerTest method expectEvalTrue.

/**
 * expectEvalTrue parses, checks, and evaluates the CEL expression in source and checks that the
 * result is the boolean value 'true'.
 */
void expectEvalTrue(String source) {
    FullPipelineResult fp = fullPipeline(source);
    long rootID = fp.parseResponse.getParsedExpr().getExpr().getId();
    Type topType = fp.checkResponse.getCheckedExpr().getTypeMapMap().get(rootID);
    assertThat(topType).extracting(Type::getTypeKindCase).isEqualTo(Type.TypeKindCase.PRIMITIVE);
    assertThat(topType).extracting(Type::getPrimitive).isEqualTo(Type.PrimitiveType.BOOL);
    ExprValue er = fp.evalResponse.getResult();
    assertThat(er).extracting(ExprValue::getKindCase).isEqualTo(ExprValue.KindCase.VALUE);
    Value ev = er.getValue();
    assertThat(ev).extracting(Value::getKindCase).isEqualTo(Value.KindCase.BOOL_VALUE);
    assertThat(ev).extracting(Value::getBoolValue).isEqualTo(true);
}
Also used : PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) ExprValue(com.google.api.expr.v1alpha1.ExprValue) Value(com.google.api.expr.v1alpha1.Value) ExprValue(com.google.api.expr.v1alpha1.ExprValue)

Aggregations

ExprValue (com.google.api.expr.v1alpha1.ExprValue)2 EvalResponse (com.google.api.expr.v1alpha1.EvalResponse)1 Type (com.google.api.expr.v1alpha1.Type)1 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)1 Value (com.google.api.expr.v1alpha1.Value)1 ByteString (com.google.protobuf.ByteString)1 HashMap (java.util.HashMap)1 Ast (org.projectnessie.cel.Ast)1 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)1 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)1 Env (org.projectnessie.cel.Env)1 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)1 Env.newEnv (org.projectnessie.cel.Env.newEnv)1 Program (org.projectnessie.cel.Program)1 EvalResult (org.projectnessie.cel.Program.EvalResult)1 Err (org.projectnessie.cel.common.types.Err)1 Err.newErr (org.projectnessie.cel.common.types.Err.newErr)1 Val (org.projectnessie.cel.common.types.ref.Val)1