Search in sources :

Example 1 with IteratorT

use of org.projectnessie.cel.common.types.IteratorT in project cel-java by projectnessie.

the class ConformanceServiceImpl method refValueToValue.

// TODO(jimlarson): The following conversion code should be moved to
// common/types/provider.go and consolidated/refactored as appropriate.
// In particular, make judicious use of types.NativeToValue().
/**
 * RefValueToValue converts between ref.Val and Value. The ref.Val must not be error or unknown.
 */
static Value refValueToValue(Val res) {
    switch(res.type().typeEnum()) {
        case Bool:
            return Value.newBuilder().setBoolValue(res.booleanValue()).build();
        case Bytes:
            return Value.newBuilder().setBytesValue(res.convertToNative(ByteString.class)).build();
        case Double:
            return Value.newBuilder().setDoubleValue(res.convertToNative(Double.class)).build();
        case Int:
            return Value.newBuilder().setInt64Value(res.intValue()).build();
        case Null:
            return Value.newBuilder().setNullValueValue(0).build();
        case String:
            return Value.newBuilder().setStringValue(res.value().toString()).build();
        case Type:
            return Value.newBuilder().setTypeValue(((TypeT) res).typeName()).build();
        case Uint:
            return Value.newBuilder().setUint64Value(res.intValue()).build();
        case Duration:
            Duration d = res.convertToNative(Duration.class);
            return Value.newBuilder().setObjectValue(Any.pack(d)).build();
        case Timestamp:
            Timestamp t = res.convertToNative(Timestamp.class);
            return Value.newBuilder().setObjectValue(Any.pack(t)).build();
        case List:
            Lister l = (Lister) res;
            ListValue.Builder elts = ListValue.newBuilder();
            for (IteratorT i = l.iterator(); i.hasNext() == True; ) {
                Val v = i.next();
                elts.addValues(refValueToValue(v));
            }
            return Value.newBuilder().setListValue(elts).build();
        case Map:
            Mapper m = (Mapper) res;
            MapValue.Builder elems = MapValue.newBuilder();
            for (IteratorT i = m.iterator(); i.hasNext() == True; ) {
                Val k = i.next();
                Val v = m.get(k);
                Value kv = refValueToValue(k);
                Value vv = refValueToValue(v);
                elems.addEntriesBuilder().setKey(kv).setValue(vv);
            }
            return Value.newBuilder().setMapValue(elems).build();
        case Object:
            // Object type
            Message pb = (Message) res.value();
            Value.Builder v = Value.newBuilder();
            // Somehow the conformance tests
            if (pb instanceof ListValue) {
                v.setListValue((ListValue) pb);
            } else if (pb instanceof MapValue) {
                v.setMapValue((MapValue) pb);
            } else {
                v.setObjectValue(Any.pack(pb));
            }
            return v.build();
        default:
            throw new IllegalStateException(String.format("Unknown %s", res.type().typeEnum()));
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TypeT(org.projectnessie.cel.common.types.TypeT) Message(com.google.protobuf.Message) Lister(org.projectnessie.cel.common.types.traits.Lister) ListValue(com.google.api.expr.v1alpha1.ListValue) Duration(com.google.protobuf.Duration) MapValue(com.google.api.expr.v1alpha1.MapValue) Timestamp(com.google.protobuf.Timestamp) IteratorT(org.projectnessie.cel.common.types.IteratorT) Mapper(org.projectnessie.cel.common.types.traits.Mapper) MapValue(com.google.api.expr.v1alpha1.MapValue) Value(com.google.api.expr.v1alpha1.Value) ExprValue(com.google.api.expr.v1alpha1.ExprValue) ListValue(com.google.api.expr.v1alpha1.ListValue) TypeT.newObjectTypeValue(org.projectnessie.cel.common.types.TypeT.newObjectTypeValue)

Example 2 with IteratorT

use of org.projectnessie.cel.common.types.IteratorT 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;
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Mapper(org.projectnessie.cel.common.types.traits.Mapper) IteratorT(org.projectnessie.cel.common.types.IteratorT) Type(org.projectnessie.cel.common.types.ref.Type) Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr) Lister(org.projectnessie.cel.common.types.traits.Lister) ArrayList(java.util.ArrayList)

Example 3 with IteratorT

use of org.projectnessie.cel.common.types.IteratorT in project cel-java by projectnessie.

the class InterpretableDecorator method maybeOptimizeSetMembership.

/**
 * maybeOptimizeSetMembership may convert an 'in' operation against a list to map key membership
 * test if the following conditions are true:
 *
 * <ul>
 *   <li>the list is a constant with homogeneous element types.
 *   <li>the elements are all of primitive type.
 * </ul>
 */
static Interpretable maybeOptimizeSetMembership(Interpretable i, InterpretableCall inlist) {
    Interpretable[] args = inlist.args();
    Interpretable lhs = args[0];
    Interpretable rhs = args[1];
    if (!(rhs instanceof InterpretableConst)) {
        return i;
    }
    InterpretableConst l = (InterpretableConst) rhs;
    // When the incoming binary call is flagged with as the InList overload, the value will
    // always be convertible to a `traits.Lister` type.
    Lister list = (Lister) l.value();
    if (list.size() == IntZero) {
        return newConstValue(inlist.id(), False);
    }
    IteratorT it = list.iterator();
    Type typ = null;
    Set<Val> valueSet = new HashSet<>();
    while (it.hasNext() == True) {
        Val elem = it.next();
        if (!Util.isPrimitiveType(elem)) {
            // Note, non-primitive type are not yet supported.
            return i;
        }
        if (typ == null) {
            typ = elem.type();
        } else if (!typ.typeName().equals(elem.type().typeName())) {
            return i;
        }
        valueSet.add(elem);
    }
    return new EvalSetMembership(inlist, lhs, typ.typeName(), valueSet);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) IteratorT(org.projectnessie.cel.common.types.IteratorT) Type(org.projectnessie.cel.common.types.ref.Type) Lister(org.projectnessie.cel.common.types.traits.Lister) EvalSetMembership(org.projectnessie.cel.interpreter.Interpretable.EvalSetMembership) InterpretableConst(org.projectnessie.cel.interpreter.Interpretable.InterpretableConst) HashSet(java.util.HashSet)

Aggregations

IteratorT (org.projectnessie.cel.common.types.IteratorT)3 Val (org.projectnessie.cel.common.types.ref.Val)3 Lister (org.projectnessie.cel.common.types.traits.Lister)3 Type (org.projectnessie.cel.common.types.ref.Type)2 Mapper (org.projectnessie.cel.common.types.traits.Mapper)2 Expr (com.google.api.expr.v1alpha1.Expr)1 Entry (com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry)1 ExprValue (com.google.api.expr.v1alpha1.ExprValue)1 ListValue (com.google.api.expr.v1alpha1.ListValue)1 MapValue (com.google.api.expr.v1alpha1.MapValue)1 Value (com.google.api.expr.v1alpha1.Value)1 Duration (com.google.protobuf.Duration)1 Message (com.google.protobuf.Message)1 Timestamp (com.google.protobuf.Timestamp)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TypeT (org.projectnessie.cel.common.types.TypeT)1 TypeT.newObjectTypeValue (org.projectnessie.cel.common.types.TypeT.newObjectTypeValue)1 EvalSetMembership (org.projectnessie.cel.interpreter.Interpretable.EvalSetMembership)1 InterpretableConst (org.projectnessie.cel.interpreter.Interpretable.InterpretableConst)1