Search in sources :

Example 11 with AstIssuesTuple

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

the class CELTest method Customtypes.

@Test
void Customtypes() {
    Type exprType = Decls.newObjectType("google.api.expr.v1alpha1.Expr");
    TypeRegistry reg = newEmptyRegistry();
    Env e = newEnv(customTypeAdapter(reg), customTypeProvider(reg), container("google.api.expr.v1alpha1"), types(Expr.getDefaultInstance(), BoolT.BoolType, IntT.IntType, StringT.StringType), declarations(Decls.newVar("expr", exprType)));
    AstIssuesTuple astIss = e.compile("expr == Expr{id: 2,\n" + "\t\t\tcall_expr: Expr.Call{\n" + "\t\t\t\tfunction: \"_==_\",\n" + "\t\t\t\targs: [\n" + "\t\t\t\t\tExpr{id: 1, ident_expr: Expr.Ident{ name: \"a\" }},\n" + "\t\t\t\t\tExpr{id: 3, ident_expr: Expr.Ident{ name: \"b\" }}]\n" + "\t\t\t}}");
    assertThat(astIss.getAst().getResultType()).isEqualTo(Decls.Bool);
    Program prg = e.program(astIss.getAst());
    Object vars = mapOf("expr", Expr.newBuilder().setId(2).setCallExpr(Call.newBuilder().setFunction("_==_").addAllArgs(asList(Expr.newBuilder().setId(1).setIdentExpr(Ident.newBuilder().setName("a")).build(), Expr.newBuilder().setId(3).setIdentExpr(Ident.newBuilder().setName("b")).build()))).build());
    EvalResult out = prg.eval(vars);
    assertThat(out.getVal()).isSameAs(True);
}
Also used : Type(com.google.api.expr.v1alpha1.Type) EvalResult(org.projectnessie.cel.Program.EvalResult) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 12 with AstIssuesTuple

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

the class CELTest method AstIsChecked.

@Test
void AstIsChecked() {
    Env e = newEnv();
    AstIssuesTuple astIss = e.compile("true");
    assertThat(astIss.hasIssues()).isFalse();
    assertThat(astIss.getAst()).extracting(Ast::isChecked).isEqualTo(true);
    CheckedExpr ce = astToCheckedExpr(astIss.getAst());
    Ast ast2 = checkedExprToAst(ce);
    assertThat(ast2).extracting(Ast::isChecked).isEqualTo(true);
    assertThat(astIss.getAst().getExpr()).isEqualTo(ast2.getExpr());
}
Also used : CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Example 13 with AstIssuesTuple

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

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

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

the class CELTest method EnvExtensionIsolation.

@Test
void EnvExtensionIsolation() {
    Env baseEnv = newEnv(container("google.api.expr.test.v1"), declarations(Decls.newVar("age", Decls.Int), Decls.newVar("gender", Decls.String), Decls.newVar("country", Decls.String)));
    Env env1 = baseEnv.extend(types(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance()), declarations(Decls.newVar("name", Decls.String)));
    Env env2 = baseEnv.extend(types(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance()), declarations(Decls.newVar("group", Decls.String)));
    AstIssuesTuple astIss = env2.compile("size(group) > 10 && !has(proto3.TestAllTypes{}.single_int32)");
    assertThat(astIss.hasIssues()).isFalse();
    astIss = env2.compile("size(name) > 10");
    assertThat(astIss.getIssues().err()).withFailMessage("env2 contains 'name', but should not").isNotNull();
    astIss = env2.compile("!has(proto2.TestAllTypes{}.single_int32)");
    assertThat(astIss.hasIssues()).isTrue();
    astIss = env1.compile("size(name) > 10 && !has(proto2.TestAllTypes{}.single_int32)");
    assertThat(astIss.hasIssues()).isFalse();
    astIss = env1.compile("size(group) > 10");
    assertThat(astIss.hasIssues()).isTrue();
    astIss = env1.compile("!has(proto3.TestAllTypes{}.single_int32)");
    assertThat(astIss.hasIssues()).isTrue();
}
Also used : Env.newEnv(org.projectnessie.cel.Env.newEnv) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Test(org.junit.jupiter.api.Test)

Aggregations

AstIssuesTuple (org.projectnessie.cel.Env.AstIssuesTuple)26 Env.newCustomEnv (org.projectnessie.cel.Env.newCustomEnv)26 Env.newEnv (org.projectnessie.cel.Env.newEnv)26 Test (org.junit.jupiter.api.Test)24 EvalResult (org.projectnessie.cel.Program.EvalResult)17 CEL.checkedExprToAst (org.projectnessie.cel.CEL.checkedExprToAst)12 CEL.parsedExprToAst (org.projectnessie.cel.CEL.parsedExprToAst)12 CEL.astToString (org.projectnessie.cel.CEL.astToString)11 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)10 CEL.astToCheckedExpr (org.projectnessie.cel.CEL.astToCheckedExpr)10 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)8 CEL.astToParsedExpr (org.projectnessie.cel.CEL.astToParsedExpr)8 Expr (com.google.api.expr.v1alpha1.Expr)7 Type (com.google.api.expr.v1alpha1.Type)6 CEL.estimateCost (org.projectnessie.cel.CEL.estimateCost)6 Call (com.google.api.expr.v1alpha1.Expr.Call)5 Ident (com.google.api.expr.v1alpha1.Expr.Ident)5 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.emptyMap (java.util.Collections.emptyMap)5