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