use of org.projectnessie.cel.common.types.traits.Indexer in project cel-java by projectnessie.
the class ProviderTest method typeRegistryGetters.
@Test
void typeRegistryGetters() {
TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
Val sourceInfo = reg.newValue("google.api.expr.v1alpha1.SourceInfo", mapOf("location", stringOf("TestTypeRegistryGetFieldValue"), "line_offsets", newGenericArrayList(reg, new Long[] { 0L, 2L }), "positions", newMaybeWrappedMap(reg, mapOf(1L, 2L, 3L, 4L))));
assertThat(sourceInfo).matches(v -> !Err.isError(v));
Indexer si = (Indexer) sourceInfo;
Val loc = si.get(stringOf("location"));
assertThat(loc.equal(stringOf("TestTypeRegistryGetFieldValue"))).isSameAs(True);
Val pos = si.get(stringOf("positions"));
assertThat(pos.equal(newMaybeWrappedMap(reg, mapOf(1, 2, 3, 4)))).isSameAs(True);
Val posKeyVal = ((Indexer) pos).get(intOf(1));
assertThat(posKeyVal.intValue()).isEqualTo(2);
Val offsets = si.get(stringOf("line_offsets"));
assertThat(offsets).matches(v -> !Err.isError(v));
Val offset1 = ((Lister) offsets).get(intOf(1));
assertThat(offset1).matches(v -> !Err.isError(v)).isEqualTo(intOf(2));
}
use of org.projectnessie.cel.common.types.traits.Indexer in project cel-java by projectnessie.
the class AttributeFactory method refResolve.
/**
* refResolve attempts to convert the value to a CEL value and then uses reflection methods to try
* and resolve the qualifier.
*/
static Val refResolve(TypeAdapter adapter, Val idx, Object obj) {
Val celVal = adapter.nativeToValue(obj);
if (celVal instanceof Mapper) {
Mapper mapper = (Mapper) celVal;
Val elem = mapper.find(idx);
if (elem == null) {
return noSuchKey(idx);
}
return elem;
}
if (celVal instanceof Indexer) {
Indexer indexer = (Indexer) celVal;
return indexer.get(idx);
}
if (isUnknown(celVal)) {
return celVal;
}
// TODO: If the types.Err value contains more than just an error message at some point in the
// future, then it would be reasonable to return error values as ref.Val types rather than
// simple go error types.
throwErrorAsIllegalStateException(celVal);
return noSuchOverload(celVal, "ref-resolve", null);
}
use of org.projectnessie.cel.common.types.traits.Indexer in project cel-java by projectnessie.
the class PbObjectTest method newProtoObject.
@Test
void newProtoObject() {
ProtoTypeRegistry reg = newRegistry();
ParsedExpr parsedExpr = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
reg.registerMessage(parsedExpr);
Indexer obj = (Indexer) reg.nativeToValue(parsedExpr);
Indexer si = (Indexer) obj.get(stringOf("source_info"));
Indexer lo = (Indexer) si.get(stringOf("line_offsets"));
assertThat(lo.get(intOf(2)).equal(intOf(3))).isSameAs(True);
Indexer expr = (Indexer) obj.get(stringOf("expr"));
Indexer call = (Indexer) expr.get(stringOf("call_expr"));
assertThat(call.get(stringOf("function")).equal(stringOf(""))).isSameAs(True);
}
Aggregations