Search in sources :

Example 1 with Indexer

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));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Lister(org.projectnessie.cel.common.types.traits.Lister) Indexer(org.projectnessie.cel.common.types.traits.Indexer) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StringValue(com.google.protobuf.StringValue) ULong(org.projectnessie.cel.common.ULong) Disabled(org.junit.jupiter.api.Disabled) True(org.projectnessie.cel.common.types.BoolT.True) Arrays.asList(java.util.Arrays.asList) Duration(java.time.Duration) ProtoTypeRegistry.newEmptyRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newEmptyRegistry) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Val(org.projectnessie.cel.common.types.ref.Val) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) FloatValue(com.google.protobuf.FloatValue) False(org.projectnessie.cel.common.types.BoolT.False) DoubleT.doubleOf(org.projectnessie.cel.common.types.DoubleT.doubleOf) Expr(com.google.api.expr.v1alpha1.Expr) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) Instant(java.time.Instant) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) StandardCharsets(java.nio.charset.StandardCharsets) UintT.uintOf(org.projectnessie.cel.common.types.UintT.uintOf) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) TimestampT.timestampOf(org.projectnessie.cel.common.types.TimestampT.timestampOf) BytesValue(com.google.protobuf.BytesValue) ListT.newGenericArrayList(org.projectnessie.cel.common.types.ListT.newGenericArrayList) NULL_VALUE(com.google.protobuf.NullValue.NULL_VALUE) ZoneIdZ(org.projectnessie.cel.common.types.TimestampT.ZoneIdZ) SourceInfo(com.google.api.expr.v1alpha1.SourceInfo) Int64Value(com.google.protobuf.Int64Value) BoolValue(com.google.protobuf.BoolValue) UInt64Value(com.google.protobuf.UInt64Value) DurationT.durationOf(org.projectnessie.cel.common.types.DurationT.durationOf) Timestamp(com.google.protobuf.Timestamp) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) ProtoTypeRegistry.newRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry.newRegistry) Calendar(java.util.Calendar) GlobalEnum(com.google.api.expr.test.v1.proto3.TestAllTypesProto.GlobalEnum) Constant(com.google.api.expr.v1alpha1.Constant) DoubleValue(com.google.protobuf.DoubleValue) Int32Value(com.google.protobuf.Int32Value) UInt32Value(com.google.protobuf.UInt32Value) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) BytesT.bytesOf(org.projectnessie.cel.common.types.BytesT.bytesOf) TimeUnit(java.util.concurrent.TimeUnit) IntZero(org.projectnessie.cel.common.types.IntT.IntZero) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) Message(com.google.protobuf.Message) Util.mapOf(org.projectnessie.cel.Util.mapOf) MapT.newMaybeWrappedMap(org.projectnessie.cel.common.types.MapT.newMaybeWrappedMap) NullValue(org.projectnessie.cel.common.types.NullT.NullValue) Indexer(org.projectnessie.cel.common.types.traits.Indexer) Lister(org.projectnessie.cel.common.types.traits.Lister) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 2 with Indexer

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);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Mapper(org.projectnessie.cel.common.types.traits.Mapper) Indexer(org.projectnessie.cel.common.types.traits.Indexer)

Example 3 with Indexer

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);
}
Also used : Indexer(org.projectnessie.cel.common.types.traits.Indexer) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Test(org.junit.jupiter.api.Test)

Aggregations

ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)2 Test (org.junit.jupiter.api.Test)2 Indexer (org.projectnessie.cel.common.types.traits.Indexer)2 GlobalEnum (com.google.api.expr.test.v1.proto3.TestAllTypesProto.GlobalEnum)1 TestAllTypes (com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes)1 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)1 Constant (com.google.api.expr.v1alpha1.Constant)1 Expr (com.google.api.expr.v1alpha1.Expr)1 SourceInfo (com.google.api.expr.v1alpha1.SourceInfo)1 BoolValue (com.google.protobuf.BoolValue)1 ByteString (com.google.protobuf.ByteString)1 BytesValue (com.google.protobuf.BytesValue)1 DoubleValue (com.google.protobuf.DoubleValue)1 FloatValue (com.google.protobuf.FloatValue)1 Int32Value (com.google.protobuf.Int32Value)1 Int64Value (com.google.protobuf.Int64Value)1 Message (com.google.protobuf.Message)1 NULL_VALUE (com.google.protobuf.NullValue.NULL_VALUE)1 StringValue (com.google.protobuf.StringValue)1 Timestamp (com.google.protobuf.Timestamp)1