use of org.projectnessie.cel.Program 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());
}
}
use of org.projectnessie.cel.Program 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);
}
}
Aggregations