use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderTest method convertToNative.
@Test
void convertToNative() {
TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
// Core type conversion tests.
expectValueToNative(True, true);
expectValueToNative(True, True);
expectValueToNative(newGenericArrayList(reg, new Val[] { True, False }), new Object[] { true, false });
expectValueToNative(newGenericArrayList(reg, new Val[] { True, False }), new Val[] { True, False });
expectValueToNative(intOf(-1), -1);
expectValueToNative(intOf(2), 2L);
expectValueToNative(intOf(-1), -1);
expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(4) }), new Object[] { 4L });
expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(5) }), new Val[] { intOf(5) });
expectValueToNative(uintOf(3), ULong.valueOf(3));
expectValueToNative(uintOf(4), ULong.valueOf(4));
expectValueToNative(uintOf(5), 5);
expectValueToNative(newGenericArrayList(reg, new Val[] { uintOf(4) }), // loses "ULong" here
new Object[] { 4L });
expectValueToNative(newGenericArrayList(reg, new Val[] { uintOf(5) }), new Val[] { uintOf(5) });
expectValueToNative(doubleOf(5.5d), 5.5f);
expectValueToNative(doubleOf(-5.5d), -5.5d);
expectValueToNative(newGenericArrayList(reg, new Val[] { doubleOf(-5.5) }), new Object[] { -5.5 });
expectValueToNative(newGenericArrayList(reg, new Val[] { doubleOf(-5.5) }), new Val[] { doubleOf(-5.5) });
expectValueToNative(doubleOf(-5.5), doubleOf(-5.5));
expectValueToNative(stringOf("hello"), "hello");
expectValueToNative(stringOf("hello"), stringOf("hello"));
expectValueToNative(NullValue, NULL_VALUE);
expectValueToNative(NullValue, NullValue);
expectValueToNative(newGenericArrayList(reg, new Val[] { NullValue }), new Object[] { null });
expectValueToNative(newGenericArrayList(reg, new Val[] { NullValue }), new Val[] { NullValue });
expectValueToNative(bytesOf("world"), "world".getBytes(StandardCharsets.UTF_8));
expectValueToNative(bytesOf("world"), "world".getBytes(StandardCharsets.UTF_8));
expectValueToNative(newGenericArrayList(reg, new Val[] { bytesOf("hello") }), new Object[] { ByteString.copyFromUtf8("hello") });
expectValueToNative(newGenericArrayList(reg, new Val[] { bytesOf("hello") }), new Val[] { bytesOf("hello") });
expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(1), intOf(2), intOf(3) }), new Object[] { 1L, 2L, 3L });
expectValueToNative(durationOf(Duration.ofSeconds(500)), Duration.ofSeconds(500));
expectValueToNative(durationOf(Duration.ofSeconds(500)), com.google.protobuf.Duration.newBuilder().setSeconds(500).build());
expectValueToNative(durationOf(Duration.ofSeconds(500)), durationOf(Duration.ofSeconds(500)));
expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), Instant.ofEpochSecond(12345, 0).atZone(ZoneIdZ));
expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), timestampOf(Timestamp.newBuilder().setSeconds(12345).build()));
expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), Timestamp.newBuilder().setSeconds(12345).build());
expectValueToNative(newMaybeWrappedMap(reg, mapOf(1L, 1L, 2L, 1L, 3L, 1L)), mapOf(1L, 1L, 2L, 1L, 3L, 1L));
// Null conversion tests.
expectValueToNative(NullValue, NULL_VALUE);
// Proto conversion tests.
ParsedExpr parsedExpr = ParsedExpr.getDefaultInstance();
expectValueToNative(reg.nativeToValue(parsedExpr), parsedExpr);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderTest method typeRegistryCopy.
@Test
void typeRegistryCopy() {
TypeRegistry reg = newEmptyRegistry();
TypeRegistry reg2 = reg.copy();
assertThat(reg).isEqualTo(reg2);
reg = newRegistry();
reg2 = reg.copy();
assertThat(reg).isEqualTo(reg2);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderTest method typeRegistryNewValue_WrapperFields.
@Test
void typeRegistryNewValue_WrapperFields() {
TypeRegistry reg = newRegistry(TestAllTypes.getDefaultInstance());
Val exp = reg.newValue("google.api.expr.test.v1.proto3.TestAllTypes", mapOf("single_int32_wrapper", intOf(123)));
assertThat(exp).matches(v -> !Err.isError(v));
TestAllTypes ce = exp.convertToNative(TestAllTypes.class);
assertThat(ce).extracting(TestAllTypes::getSingleInt32Wrapper).extracting(Int32Value::getValue).isEqualTo(123);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderTest method nativeToValue_Primitive.
@Test
void nativeToValue_Primitive() {
TypeRegistry reg = newEmptyRegistry();
// Core type conversions.
expectNativeToValue(true, True);
expectNativeToValue(-10, intOf(-10));
expectNativeToValue(-1, intOf(-1));
expectNativeToValue(2L, intOf(2));
expectNativeToValue(ULong.valueOf(6), uintOf(6));
expectNativeToValue(ULong.valueOf(3), uintOf(3));
expectNativeToValue(ULong.valueOf(4), uintOf(4));
expectNativeToValue(5.5f, doubleOf(5.5));
expectNativeToValue(-5.5d, doubleOf(-5.5));
expectNativeToValue("hello", stringOf("hello"));
expectNativeToValue("world".getBytes(StandardCharsets.UTF_8), bytesOf("world"));
expectNativeToValue(Duration.ofSeconds(500), durationOf(Duration.ofSeconds(500)));
expectNativeToValue(new Date(TimeUnit.SECONDS.toMillis(12345)), timestampOf(Timestamp.newBuilder().setSeconds(12345).build()));
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(TimeUnit.SECONDS.toMillis(12345));
expectNativeToValue(new Date(TimeUnit.SECONDS.toMillis(12345)), timestampOf(Timestamp.newBuilder().setSeconds(12345).build()));
expectNativeToValue(Instant.ofEpochSecond(12345).atZone(ZoneIdZ), timestampOf(Timestamp.newBuilder().setSeconds(12345).build()));
expectNativeToValue(Duration.ofSeconds(500), durationOf(Duration.ofSeconds(500)));
expectNativeToValue(new int[] { 1, 2, 3 }, newGenericArrayList(reg, new Object[] { 1, 2, 3 }));
expectNativeToValue(mapOf(1, 1, 2, 1, 3, 1), newMaybeWrappedMap(reg, mapOf(1, 1, 2, 1, 3, 1)));
// Null conversion test.
expectNativeToValue(null, NullValue);
expectNativeToValue(NULL_VALUE, NullValue);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class ProviderTest method typeRegistryNewValue_OneofFields.
@Test
void typeRegistryNewValue_OneofFields() {
TypeRegistry reg = newRegistry(CheckedExpr.getDefaultInstance(), ParsedExpr.getDefaultInstance());
Val exp = reg.newValue("google.api.expr.v1alpha1.CheckedExpr", mapOf("expr", reg.newValue("google.api.expr.v1alpha1.Expr", mapOf("const_expr", reg.newValue("google.api.expr.v1alpha1.Constant", mapOf("string_value", stringOf("oneof")))))));
assertThat(exp).matches(v -> !Err.isError(v));
CheckedExpr ce = exp.convertToNative(CheckedExpr.class);
assertThat(ce).extracting(CheckedExpr::getExpr).extracting(Expr::getConstExpr).extracting(Constant::getStringValue).isEqualTo("oneof");
}
Aggregations