use of org.projectnessie.cel.common.types.ref.Val in project cel-java by projectnessie.
the class DurationTest method durationNegate.
@Test
void durationNegate() {
Val neg = durationOf(ofSeconds(1234, 1)).negate();
Duration want = ofSeconds(-1234, -1);
assertThat(neg.value()).isEqualTo(want);
assertThat(durationOf(ofSeconds(Long.MIN_VALUE)).negate()).matches(Err::isError);
assertThat(durationOf(ofSeconds(Long.MAX_VALUE)).negate().equal(durationOf(ofSeconds(Long.MIN_VALUE + 1)))).isSameAs(True);
}
use of org.projectnessie.cel.common.types.ref.Val 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()));
}
}
use of org.projectnessie.cel.common.types.ref.Val 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.Val in project cel-java by projectnessie.
the class CheckerEnv method lookupIdent.
/**
* LookupIdent returns a Decl proto for typeName as an identifier in the Env. Returns nil if no
* such identifier is found in the Env.
*/
public Decl lookupIdent(String name) {
for (String candidate : container.resolveCandidateNames(name)) {
Decl ident = declarations.findIdent(candidate);
if (ident != null) {
return ident;
}
// Next try to import the name as a reference to a message type. If found,
// the declaration is added to the outest (global) scope of the
// environment, so next time we can access it faster.
Type t = provider.findType(candidate);
if (t != null) {
Decl decl = Decls.newVar(candidate, t);
declarations.addIdent(decl);
return decl;
}
// Next try to import this as an enum value by splitting the name in a type prefix and
// the enum inside.
Val enumValue = provider.enumValue(candidate);
if (enumValue.type() != ErrType) {
Decl decl = Decls.newIdent(candidate, Decls.Int, Constant.newBuilder().setInt64Value(enumValue.intValue()).build());
declarations.addIdent(decl);
return decl;
}
}
return null;
}
use of org.projectnessie.cel.common.types.ref.Val in project cel-java by projectnessie.
the class DefaultTypeAdapter method nativeToValue.
/**
* nativeToValue returns the converted (ref.Val, true) of a conversion is found, otherwise (nil,
* false)
*/
public static Val nativeToValue(Db db, TypeAdapter a, Object value) {
Val v = maybeNativeToValue(a, value);
if (v != null) {
return v;
}
// additional specializations may be added upon request / need.
if (value instanceof Val) {
return (Val) value;
}
if (value instanceof Message) {
Message msg = (Message) value;
String typeName = typeNameFromMessage(msg);
if (typeName.isEmpty()) {
return anyWithEmptyType();
}
PbTypeDescription type = db.describeType(typeName);
if (type == null) {
return unknownType(typeName);
}
value = type.maybeUnwrap(db, msg);
if (value instanceof Message) {
value = type.maybeUnwrap(db, value);
}
return a.nativeToValue(value);
}
return newErr("unsupported conversion from '%s' to value", value.getClass());
}
Aggregations