Search in sources :

Example 1 with SourceInfo

use of com.google.api.expr.v1alpha1.SourceInfo 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 SourceInfo

use of com.google.api.expr.v1alpha1.SourceInfo in project cel-java by projectnessie.

the class CEL method astToString.

/**
 * AstToString converts an Ast back to a string if possible.
 *
 * <p>Note, the conversion may not be an exact replica of the original expression, but will
 * produce a string that is semantically equivalent and whose textual representation is stable.
 */
public static String astToString(Ast a) {
    Expr expr = a.getExpr();
    SourceInfo info = a.getSourceInfo();
    return unparse(expr, info);
}
Also used : ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) SourceInfo(com.google.api.expr.v1alpha1.SourceInfo)

Example 3 with SourceInfo

use of com.google.api.expr.v1alpha1.SourceInfo in project cel-java by projectnessie.

the class ProviderTest method typeRegistryNewValue.

@Test
void typeRegistryNewValue() {
    TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
    Val sourceInfo = reg.newValue("google.api.expr.v1alpha1.SourceInfo", mapOf("location", stringOf("TestTypeRegistryNewValue"), "line_offsets", newGenericArrayList(reg, new Long[] { 0L, 2L }), "positions", newMaybeWrappedMap(reg, mapOf(1L, 2, 3L, 4))));
    assertThat(sourceInfo).matches(v -> !Err.isError(v));
    Message info = (Message) sourceInfo.value();
    SourceInfo srcInfo = SourceInfo.newBuilder().mergeFrom(info).build();
    assertThat(srcInfo).extracting(SourceInfo::getLocation, SourceInfo::getLineOffsetsList, SourceInfo::getPositionsMap).containsExactly("TestTypeRegistryNewValue", asList(0, 2), mapOf(1L, 2L, 3L, 4L));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Message(com.google.protobuf.Message) SourceInfo(com.google.api.expr.v1alpha1.SourceInfo) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

SourceInfo (com.google.api.expr.v1alpha1.SourceInfo)3 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)2 Expr (com.google.api.expr.v1alpha1.Expr)2 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)2 Message (com.google.protobuf.Message)2 Test (org.junit.jupiter.api.Test)2 GlobalEnum (com.google.api.expr.test.v1.proto3.TestAllTypesProto.GlobalEnum)1 TestAllTypes (com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes)1 Constant (com.google.api.expr.v1alpha1.Constant)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 NULL_VALUE (com.google.protobuf.NullValue.NULL_VALUE)1 StringValue (com.google.protobuf.StringValue)1 Timestamp (com.google.protobuf.Timestamp)1 UInt32Value (com.google.protobuf.UInt32Value)1