Search in sources :

Example 71 with Type

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

the class CheckerTest method check.

@ParameterizedTest
@MethodSource("checkTestCases")
void check(TestCase tc) {
    Assumptions.assumeTrue(tc.disabled == null, tc.disabled);
    Source src = newTextSource(tc.i);
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.getErrors().getErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isEmpty();
    TypeRegistry reg = ProtoTypeRegistry.newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance(), com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance());
    Container cont = Container.newContainer(Container.name(tc.container));
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    if (tc.disableStdEnv) {
        env = newCheckerEnv(cont, reg);
    }
    if (tc.homogeneousAggregateLiterals) {
        env.enableDynamicAggregateLiterals(false);
    }
    if (tc.env != null) {
        if (tc.env.idents != null) {
            for (Decl ident : tc.env.idents) {
                env.add(ident);
            }
        }
        if (tc.env.functions != null) {
            for (Decl fn : tc.env.functions) {
                env.add(fn);
            }
        }
    }
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (checkResult.hasErrors()) {
        String errorString = checkResult.getErrors().toDisplayString();
        if (tc.error != null) {
            assertThat(errorString).isEqualTo(tc.error);
        } else {
            fail(String.format("Unexpected type-check errors: %s", errorString));
        }
    } else if (tc.error != null) {
        assertThat(tc.error).withFailMessage(String.format("Expected error not thrown: %s", tc.error)).isNull();
    }
    Type actual = checkResult.getCheckedExpr().getTypeMapMap().get(parsed.getExpr().getId());
    if (tc.error == null) {
        if (actual == null || !actual.equals(tc.type)) {
            fail(String.format("Type Error: '%s' vs expected '%s'", actual, tc.type));
        }
    }
    if (tc.r != null) {
        String actualStr = print(checkResult.getCheckedExpr().getExpr(), checkResult.getCheckedExpr());
        String actualCmp = actualStr.replaceAll("[ \n\t]", "");
        String rCmp = tc.r.replaceAll("[ \n\t]", "");
        assertThat(actualCmp).isEqualTo(rCmp);
    }
}
Also used : Container(org.projectnessie.cel.common.containers.Container) Type(com.google.api.expr.v1alpha1.Type) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) CheckerEnv.newCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newCheckerEnv) Decl(com.google.api.expr.v1alpha1.Decl) ProtoTypeRegistry(org.projectnessie.cel.common.types.pb.ProtoTypeRegistry) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 72 with Type

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

the class AstPruner method maybeCreateLiteral.

Expr maybeCreateLiteral(long id, Val v) {
    Type t = v.type();
    switch(t.typeEnum()) {
        case Bool:
            return createLiteral(id, Constant.newBuilder().setBoolValue((Boolean) v.value()).build());
        case Int:
            return createLiteral(id, Constant.newBuilder().setInt64Value(((Number) v.value()).longValue()).build());
        case Uint:
            return createLiteral(id, Constant.newBuilder().setUint64Value(((Number) v.value()).longValue()).build());
        case String:
            return createLiteral(id, Constant.newBuilder().setStringValue(v.value().toString()).build());
        case Double:
            return createLiteral(id, Constant.newBuilder().setDoubleValue(((Number) v.value()).doubleValue()).build());
        case Bytes:
            return createLiteral(id, Constant.newBuilder().setBytesValue(ByteString.copyFrom((byte[]) v.value())).build());
        case Null:
            return createLiteral(id, Constant.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
    }
    // Attempt to build a list literal.
    if (v instanceof Lister) {
        Lister list = (Lister) v;
        int sz = (int) list.size().intValue();
        List<Expr> elemExprs = new ArrayList<>(sz);
        for (int i = 0; i < sz; i++) {
            Val elem = list.get(intOf(i));
            if (isUnknownOrError(elem)) {
                return null;
            }
            Expr elemExpr = maybeCreateLiteral(nextID(), elem);
            if (elemExpr == null) {
                return null;
            }
            elemExprs.add(elemExpr);
        }
        return Expr.newBuilder().setId(id).setListExpr(CreateList.newBuilder().addAllElements(elemExprs).build()).build();
    }
    // Create a map literal if possible.
    if (v instanceof Mapper) {
        Mapper mp = (Mapper) v;
        IteratorT it = mp.iterator();
        List<Entry> entries = new ArrayList<>((int) mp.size().intValue());
        while (it.hasNext() == True) {
            Val key = it.next();
            Val val = mp.get(key);
            if (isUnknownOrError(key) || isUnknownOrError(val)) {
                return null;
            }
            Expr keyExpr = maybeCreateLiteral(nextID(), key);
            if (keyExpr == null) {
                return null;
            }
            Expr valExpr = maybeCreateLiteral(nextID(), val);
            if (valExpr == null) {
                return null;
            }
            Entry entry = Entry.newBuilder().setId(nextID()).setMapKey(keyExpr).setValue(valExpr).build();
            entries.add(entry);
        }
        return Expr.newBuilder().setId(id).setStructExpr(CreateStruct.newBuilder().addAllEntries(entries)).build();
    }
    // the enumeration the fields for a given message.
    return null;
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Mapper(org.projectnessie.cel.common.types.traits.Mapper) IteratorT(org.projectnessie.cel.common.types.IteratorT) Type(org.projectnessie.cel.common.types.ref.Type) Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr) Lister(org.projectnessie.cel.common.types.traits.Lister) ArrayList(java.util.ArrayList)

Example 73 with Type

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

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

the class PbObjectTest method protoObjectConvertToType.

@Test
void protoObjectConvertToType() {
    TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
    ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
    Val obj = reg.nativeToValue(msg);
    assertThat(obj).isInstanceOf(ObjectT.class);
    ObjectT objVal = (ObjectT) obj;
    Type tv = objVal.type();
    assertThat(objVal.convertToType(TypeType).equal(tv)).isSameAs(True);
    assertThat(objVal.convertToType(objVal.type())).isSameAs(objVal);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TypeType(org.projectnessie.cel.common.types.TypeT.TypeType) Type(org.projectnessie.cel.common.types.ref.Type) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 75 with Type

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

the class JacksonTypeDescriptionTest method checkListType.

private void checkListType(JacksonRegistry reg, String prop, Class<?> valueClass, com.google.api.expr.v1alpha1.Type valueType) {
    JacksonFieldType ft = (JacksonFieldType) reg.findFieldType(CollectionsObject.class.getName(), prop);
    assertThat(ft).isNotNull();
    JavaType javaType = ft.propertyWriter().getType();
    assertThat(javaType).extracting(JavaType::isCollectionLikeType).isEqualTo(true);
    assertThat(javaType.getContentType()).extracting(JavaType::getRawClass).isSameAs(valueClass);
    assertThat(ft.type).extracting(com.google.api.expr.v1alpha1.Type::getListType).extracting(ListType::getElemType).isSameAs(valueType);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ListType(com.google.api.expr.v1alpha1.Type.ListType) JavaType(com.fasterxml.jackson.databind.JavaType) MapType(com.google.api.expr.v1alpha1.Type.MapType) InnerType(org.projectnessie.cel.types.jackson.types.InnerType)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)16 MapType (com.google.api.expr.v1alpha1.Type.MapType)15 Type (edu.stanford.CVC4.Type)14 Type (com.google.spanner.v1.Type)12 Test (org.junit.jupiter.api.Test)12 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)11 ByteString (com.google.protobuf.ByteString)11 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 ArrayList (java.util.ArrayList)11 Type (org.apache.xbean.asm9.Type)10 Expr (com.google.api.expr.v1alpha1.Expr)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 ListValue (com.google.protobuf.ListValue)8 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7