use of org.projectnessie.cel.common.types.TypeT 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()));
}
}
Aggregations