use of org.projectnessie.cel.common.types.ref.Type in project cel-java by projectnessie.
the class ConformanceServiceImpl method valueToRefValue.
/**
* ValueToRefValue converts between exprpb.Value and ref.Val.
*/
static Val valueToRefValue(TypeAdapter adapter, Value v) {
switch(v.getKindCase()) {
case NULL_VALUE:
return NullT.NullValue;
case BOOL_VALUE:
return boolOf(v.getBoolValue());
case INT64_VALUE:
return intOf(v.getInt64Value());
case UINT64_VALUE:
return uintOf(v.getUint64Value());
case DOUBLE_VALUE:
return doubleOf(v.getDoubleValue());
case STRING_VALUE:
return stringOf(v.getStringValue());
case BYTES_VALUE:
return bytesOf(v.getBytesValue().toByteArray());
case OBJECT_VALUE:
Any any = v.getObjectValue();
return adapter.nativeToValue(any);
case MAP_VALUE:
MapValue m = v.getMapValue();
Map<Val, Val> entries = new HashMap<>();
for (Entry entry : m.getEntriesList()) {
Val key = valueToRefValue(adapter, entry.getKey());
Val pb = valueToRefValue(adapter, entry.getValue());
entries.put(key, pb);
}
return adapter.nativeToValue(entries);
case LIST_VALUE:
ListValue l = v.getListValue();
List<Val> elts = l.getValuesList().stream().map(el -> valueToRefValue(adapter, el)).collect(Collectors.toList());
return adapter.nativeToValue(elts);
case TYPE_VALUE:
String typeName = v.getTypeValue();
Type tv = Types.getTypeByName(typeName);
if (tv != null) {
return tv;
}
return newObjectTypeValue(typeName);
default:
throw new IllegalArgumentException("unknown value " + v.getKindCase());
}
}
use of org.projectnessie.cel.common.types.ref.Type in project cel-java by projectnessie.
the class TypeTest method typeConvertToType.
@Test
void typeConvertToType() {
Type[] stdTypes = new Type[] { BoolType, BytesType, DoubleType, DurationType, IntType, ListType, MapType, NullType, StringType, TimestampType, TypeType, UintType };
for (Type stdType : stdTypes) {
Val cnv = stdType.convertToType(TypeType);
assertThat(cnv).isEqualTo(TypeType);
}
}
use of org.projectnessie.cel.common.types.ref.Type 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 org.projectnessie.cel.common.types.ref.Type 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);
}
use of org.projectnessie.cel.common.types.ref.Type in project cel-java by projectnessie.
the class PbObjectTest method protoObjectConvertToType.
@Test
void protoObjectConvertToType() {
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;
Type tv = objVal.type();
assertThat(objVal.convertToType(TypeType).equal(tv)).isSameAs(True);
assertThat(objVal.convertToType(objVal.type())).isSameAs(objVal);
}
Aggregations