use of com.google.api.expr.v1alpha1.Expr 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 com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class ContainerTest method ToQualifiedName.
@Test
void ToQualifiedName() {
Expr ident = Expr.newBuilder().setId(0).setIdentExpr(Ident.newBuilder().setName("var")).build();
String idName = toQualifiedName(ident);
assertThat(idName).isEqualTo("var");
Expr sel = Expr.newBuilder().setId(0).setSelectExpr(Select.newBuilder().setOperand(ident).setField("qualifier")).build();
String qualName = toQualifiedName(sel);
assertThat(qualName).isEqualTo("var.qualifier");
sel = Expr.newBuilder().setId(0).setSelectExpr(Select.newBuilder().setOperand(ident).setField("qualifier").setTestOnly(true)).build();
assertThat(toQualifiedName(sel)).isNull();
Expr unary = Expr.newBuilder().setId(0).setCallExpr(Call.newBuilder().setFunction("!_").addArgs(ident)).build();
sel = Expr.newBuilder().setId(0).setSelectExpr(Select.newBuilder().setOperand(unary).setField("qualifier")).build();
assertThat(toQualifiedName(sel)).isNull();
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class Checker method Check.
/**
* Check performs type checking, giving a typed AST. The input is a ParsedExpr proto and an env
* which encapsulates type binding of variables, declarations of built-in functions, descriptions
* of protocol buffers, and a registry for errors. Returns a CheckedExpr proto, which might not be
* usable if there are errors in the error registry.
*/
public static CheckResult Check(ParseResult parsedExpr, Source source, CheckerEnv env) {
TypeErrors errors = new TypeErrors(source);
Checker c = new Checker(env, errors, newMapping(), 0, parsedExpr.getSourceInfo());
Expr.Builder b = parsedExpr.getExpr().toBuilder();
c.check(b);
Expr e = b.build();
// Walk over the final type map substituting any type parameters either by their bound value or
// by DYN.
Map<Long, Type> m = new HashMap<>();
c.types.forEach((k, v) -> m.put(k, substitute(c.mappings, v, true)));
CheckedExpr checkedExpr = CheckedExpr.newBuilder().setExpr(e).setSourceInfo(parsedExpr.getSourceInfo()).putAllTypeMap(m).putAllReferenceMap(c.references).build();
return new CheckResult(checkedExpr, errors);
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class PbObjectTest method protoObjectIsSet.
@Test
void protoObjectIsSet() {
TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
Val obj = reg.nativeToValue(msg);
assertThat(obj).isInstanceOf(ObjectT.class);
ObjectT objVal = (ObjectT) obj;
assertThat(objVal.isSet(stringOf("source_info"))).isSameAs(True);
assertThat(objVal.isSet(stringOf("expr"))).isSameAs(False);
assertThat(objVal.isSet(stringOf("bad_field"))).matches(Err::isError);
assertThat(objVal.isSet(IntZero)).matches(Err::isError);
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class UnparserTest method unparseIdentical.
@ParameterizedTest
@MethodSource("unparseIdenticalSource")
void unparseIdentical(String name, String in) {
Parser parser = new Parser(Options.builder().build());
ParseResult p = parser.parse(Source.newTextSource(in));
if (p.hasErrors()) {
fail(p.getErrors().toDisplayString());
}
String out = Unparser.unparse(p.getExpr(), p.getSourceInfo());
assertThat(out).isEqualTo(in);
ParseResult p2 = parser.parse(Source.newTextSource(out));
if (p2.hasErrors()) {
fail(p2.getErrors().toDisplayString());
}
Expr before = p.getExpr();
Expr after = p2.getExpr();
assertThat(before).isEqualTo(after);
}
Aggregations