Search in sources :

Example 1 with Env

use of org.projectnessie.cel.Env 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 Env

use of org.projectnessie.cel.Env 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 Env

use of org.projectnessie.cel.Env 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 4 with Env

use of org.projectnessie.cel.Env in project cel-java by projectnessie.

the class StringsTest method testExpression.

private static void testExpression(TestData testData) {
    Env env = Env.newCustomEnv(ProtoTypeRegistry.newRegistry(), Arrays.asList(Library.StdLib(), StringsLib.strings()));
    Env.AstIssuesTuple astIssue = env.parse(testData.expression);
    assertThat(astIssue.hasIssues()).isFalse();
    Env.AstIssuesTuple checked = env.check(astIssue.getAst());
    if (testData.isParseOnly()) {
        assertThat(checked.hasIssues()).isTrue();
        assertThat(checked.getIssues().toString()).contains(testData.getErr());
        return;
    } else {
        assertThat(checked.hasIssues()).isFalse();
    }
    Program program = env.program(astIssue.getAst());
    Program.EvalResult result = program.eval(new HashMap<>());
    if (testData.getErr() != null) {
        assertThat(result.getVal() instanceof Err).isTrue();
        assertThat(result.getVal()).extracting(Val::toString).isEqualTo(testData.getErr());
    } else {
        assertThat(result.getVal()).isEqualTo(BoolT.True);
    }
}
Also used : Program(org.projectnessie.cel.Program) Err(org.projectnessie.cel.common.types.Err) Env(org.projectnessie.cel.Env)

Aggregations

Env (org.projectnessie.cel.Env)4 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)3 Env.newEnv (org.projectnessie.cel.Env.newEnv)3 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)2 EnvOption (org.projectnessie.cel.EnvOption)2 Program (org.projectnessie.cel.Program)2 Err (org.projectnessie.cel.common.types.Err)2 CheckResponse (com.google.api.expr.v1alpha1.CheckResponse)1 EvalResponse (com.google.api.expr.v1alpha1.EvalResponse)1 ExprValue (com.google.api.expr.v1alpha1.ExprValue)1 ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)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 EvalResult (org.projectnessie.cel.Program.EvalResult)1 Err.newErr (org.projectnessie.cel.common.types.Err.newErr)1 Val (org.projectnessie.cel.common.types.ref.Val)1