Search in sources :

Example 1 with EnvOption

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

the class StringsLib method getCompileOptions.

@Override
public List<EnvOption> getCompileOptions() {
    List<EnvOption> list = new ArrayList<>();
    EnvOption option = EnvOption.declarations(Decls.newFunction(CHAR_AT, Decls.newInstanceOverload("string_char_at_int", Arrays.asList(Decls.String, Decls.Int), Decls.String)), Decls.newFunction(INDEX_OF, Decls.newInstanceOverload("string_index_of_string", Arrays.asList(Decls.String, Decls.String), Decls.Int)), Decls.newFunction(LAST_INDEX_OF, Decls.newInstanceOverload("string_last_index_of_string", Arrays.asList(Decls.String, Decls.String), Decls.Int)), Decls.newFunction(LOWER_ASCII, Decls.newInstanceOverload("string_lower_ascii", Arrays.asList(Decls.String), Decls.String)), Decls.newFunction(REPLACE, Decls.newInstanceOverload("string_replace_string_string", Arrays.asList(Decls.String, Decls.String, Decls.String), Decls.String)), Decls.newFunction(SPLIT, Decls.newInstanceOverload("string_split_string", Arrays.asList(Decls.String, Decls.String), Decls.Dyn)), Decls.newFunction(SUBSTR, Decls.newInstanceOverload("string_substring_int", Arrays.asList(Decls.String, Decls.Int), Decls.String)), Decls.newFunction(TRIM_SPACE, Decls.newInstanceOverload("string_trim", Arrays.asList(Decls.String), Decls.String)), Decls.newFunction(UPPER_ASCII, Decls.newInstanceOverload("string_upper_ascii", Arrays.asList(Decls.String), Decls.String)));
    list.add(option);
    return list;
}
Also used : EnvOption(org.projectnessie.cel.EnvOption)

Example 2 with EnvOption

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

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

Aggregations

EnvOption (org.projectnessie.cel.EnvOption)3 ArrayList (java.util.ArrayList)2 Env (org.projectnessie.cel.Env)2 AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)2 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)2 Env.newEnv (org.projectnessie.cel.Env.newEnv)2 CheckResponse (com.google.api.expr.v1alpha1.CheckResponse)1 ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)1 ByteString (com.google.protobuf.ByteString)1