use of org.projectnessie.cel.common.types.ref.TypeAdapter 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.TypeAdapter in project cel-java by projectnessie.
the class Env method extend.
/**
* Extend the current environment with additional options to produce a new Env.
*
* <p>Note, the extended Env value should not share memory with the original. It is possible,
* however, that a CustomTypeAdapter or CustomTypeProvider options could provide values which are
* mutable. To ensure separation of state between extended environments either make sure the
* TypeAdapter and TypeProvider are immutable, or that their underlying implementations are based
* on the ref.TypeRegistry which provides a Copy method which will be invoked by this method.
*/
public Env extend(List<EnvOption> opts) {
if (chkErr != null) {
throw chkErr;
}
// Copy slices.
List<Decl> decsCopy = new ArrayList<>(declarations);
List<Macro> macsCopy = new ArrayList<>(macros);
List<ProgramOption> progOptsCopy = new ArrayList<>(progOpts);
// Copy the adapter / provider if they appear to be mutable.
TypeAdapter adapter = this.adapter;
TypeProvider provider = this.provider;
// TypeRegistry as the base implementation are captured below.
if (this.adapter instanceof TypeRegistry && this.provider instanceof TypeRegistry) {
TypeRegistry adapterReg = (TypeRegistry) this.adapter;
TypeRegistry providerReg = (TypeRegistry) this.provider;
TypeRegistry reg = providerReg.copy();
provider = reg;
// to the same ref.TypeRegistry as the provider.
if (adapterReg.equals(providerReg)) {
adapter = reg;
} else {
// Otherwise, make a copy of the adapter.
adapter = adapterReg.copy();
}
} else if (this.provider instanceof TypeRegistry) {
provider = ((TypeRegistry) this.provider).copy();
} else if (this.adapter instanceof TypeRegistry) {
adapter = ((TypeRegistry) this.adapter).copy();
}
Set<EnvFeature> featuresCopy = EnumSet.copyOf(this.features);
Env ext = new Env(this.container, decsCopy, macsCopy, adapter, provider, featuresCopy, progOptsCopy);
return ext.configure(opts);
}
Aggregations