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);
}
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());
}
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());
}
}
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());
}
}
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();
}
Aggregations