Search in sources :

Example 41 with TypeRegistry

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

Example 42 with TypeRegistry

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);
}
Also used : TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 43 with TypeRegistry

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);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 44 with TypeRegistry

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);
}
Also used : Calendar(java.util.Calendar) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 45 with TypeRegistry

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");
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)51 Test (org.junit.jupiter.api.Test)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)26 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)25 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)21 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)21 Val (org.projectnessie.cel.common.types.ref.Val)20 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)20 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)19 Qualifier (org.projectnessie.cel.interpreter.AttributeFactory.Qualifier)16 Container (org.projectnessie.cel.common.containers.Container)14 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)12 Attribute (org.projectnessie.cel.interpreter.AttributeFactory.Attribute)12 MethodSource (org.junit.jupiter.params.provider.MethodSource)11 ProtoTypeRegistry (org.projectnessie.cel.common.types.pb.ProtoTypeRegistry)10 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)10 Source (org.projectnessie.cel.common.Source)9 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)9 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)8