use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class Unparser method visitStructMsg.
void visitStructMsg(CreateStruct expr) {
List<Entry> entries = expr.getEntriesList();
str.append(expr.getMessageName());
str.append("{");
for (int i = 0; i < entries.size(); i++) {
if (i > 0) {
str.append(", ");
}
Entry entry = entries.get(i);
String f = entry.getFieldKey();
str.append(f);
str.append(": ");
Expr v = entry.getValue();
visit(v);
}
str.append("}");
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class AstPruner method pruneAst.
public static Expr pruneAst(Expr expr, EvalState state) {
AstPruner pruner = new AstPruner(expr, state, 1);
Expr newExpr = pruner.prune(expr);
return newExpr;
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class AstPruner method maybeCreateLiteral.
Expr maybeCreateLiteral(long id, Val v) {
Type t = v.type();
switch(t.typeEnum()) {
case Bool:
return createLiteral(id, Constant.newBuilder().setBoolValue((Boolean) v.value()).build());
case Int:
return createLiteral(id, Constant.newBuilder().setInt64Value(((Number) v.value()).longValue()).build());
case Uint:
return createLiteral(id, Constant.newBuilder().setUint64Value(((Number) v.value()).longValue()).build());
case String:
return createLiteral(id, Constant.newBuilder().setStringValue(v.value().toString()).build());
case Double:
return createLiteral(id, Constant.newBuilder().setDoubleValue(((Number) v.value()).doubleValue()).build());
case Bytes:
return createLiteral(id, Constant.newBuilder().setBytesValue(ByteString.copyFrom((byte[]) v.value())).build());
case Null:
return createLiteral(id, Constant.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
}
// Attempt to build a list literal.
if (v instanceof Lister) {
Lister list = (Lister) v;
int sz = (int) list.size().intValue();
List<Expr> elemExprs = new ArrayList<>(sz);
for (int i = 0; i < sz; i++) {
Val elem = list.get(intOf(i));
if (isUnknownOrError(elem)) {
return null;
}
Expr elemExpr = maybeCreateLiteral(nextID(), elem);
if (elemExpr == null) {
return null;
}
elemExprs.add(elemExpr);
}
return Expr.newBuilder().setId(id).setListExpr(CreateList.newBuilder().addAllElements(elemExprs).build()).build();
}
// Create a map literal if possible.
if (v instanceof Mapper) {
Mapper mp = (Mapper) v;
IteratorT it = mp.iterator();
List<Entry> entries = new ArrayList<>((int) mp.size().intValue());
while (it.hasNext() == True) {
Val key = it.next();
Val val = mp.get(key);
if (isUnknownOrError(key) || isUnknownOrError(val)) {
return null;
}
Expr keyExpr = maybeCreateLiteral(nextID(), key);
if (keyExpr == null) {
return null;
}
Expr valExpr = maybeCreateLiteral(nextID(), val);
if (valExpr == null) {
return null;
}
Entry entry = Entry.newBuilder().setId(nextID()).setMapKey(keyExpr).setValue(valExpr).build();
entries.add(entry);
}
return Expr.newBuilder().setId(id).setStructExpr(CreateStruct.newBuilder().addAllEntries(entries)).build();
}
// the enumeration the fields for a given message.
return null;
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class Macro method makeFilter.
static Expr makeFilter(ExprHelper eh, Expr target, List<Expr> args) {
String v = extractIdent(args.get(0));
if (v == null) {
throw new ErrorWithLocation(null, "argument is not an identifier");
}
Expr filter = args.get(1);
Expr accuExpr = eh.ident(AccumulatorName);
Expr init = eh.newList();
Expr condition = eh.literalBool(true);
Expr step = eh.globalCall(Operator.Add.id, accuExpr, eh.newList(args.get(0)));
step = eh.globalCall(Operator.Conditional.id, filter, step, accuExpr);
return eh.fold(v, target, AccumulatorName, init, condition, step, accuExpr);
}
use of com.google.api.expr.v1alpha1.Expr in project cel-java by projectnessie.
the class Macro method makeQuantifier.
static Expr makeQuantifier(QuantifierKind kind, ExprHelper eh, Expr target, List<Expr> args) {
String v = extractIdent(args.get(0));
if (v == null) {
Location location = eh.offsetLocation(args.get(0).getId());
throw new ErrorWithLocation(location, "argument must be a simple name");
}
Supplier<Expr> accuIdent = () -> eh.ident(AccumulatorName);
Expr init;
Expr condition;
Expr step;
Expr result;
switch(kind) {
case quantifierAll:
init = eh.literalBool(true);
condition = eh.globalCall(Operator.NotStrictlyFalse.id, accuIdent.get());
step = eh.globalCall(Operator.LogicalAnd.id, accuIdent.get(), args.get(1));
result = accuIdent.get();
break;
case quantifierExists:
init = eh.literalBool(false);
condition = eh.globalCall(Operator.NotStrictlyFalse.id, eh.globalCall(Operator.LogicalNot.id, accuIdent.get()));
step = eh.globalCall(Operator.LogicalOr.id, accuIdent.get(), args.get(1));
result = accuIdent.get();
break;
case quantifierExistsOne:
Expr zeroExpr = eh.literalInt(0);
Expr oneExpr = eh.literalInt(1);
init = zeroExpr;
condition = eh.literalBool(true);
step = eh.globalCall(Operator.Conditional.id, args.get(1), eh.globalCall(Operator.Add.id, accuIdent.get(), oneExpr), accuIdent.get());
result = eh.globalCall(Operator.Equals.id, accuIdent.get(), oneExpr);
break;
default:
throw new ErrorWithLocation(null, String.format("unrecognized quantifier '%s'", kind));
}
return eh.fold(v, target, AccumulatorName, init, condition, step, result);
}
Aggregations