Search in sources :

Example 1 with MapType

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

the class Checker method checkSelect.

void checkSelect(Expr.Builder e) {
    Select.Builder sel = e.getSelectExprBuilder();
    // Before traversing down the tree, try to interpret as qualified name.
    String qname = Container.toQualifiedName(e.build());
    if (qname != null) {
        Decl ident = env.lookupIdent(qname);
        if (ident != null) {
            if (sel.getTestOnly()) {
                errors.expressionDoesNotSelectField(location(e));
                setType(e, Decls.Bool);
                return;
            }
            // Rewrite the node to be a variable reference to the resolved fully-qualified
            // variable name.
            setType(e, ident.getIdent().getType());
            setReference(e, newIdentReference(ident.getName(), ident.getIdent().getValue()));
            String identName = ident.getName();
            e.getIdentExprBuilder().setName(identName);
            return;
        }
    }
    // Interpret as field selection, first traversing down the operand.
    check(sel.getOperandBuilder());
    Type targetType = getType(sel.getOperandBuilder());
    // Assume error type by default as most types do not support field selection.
    Type resultType = Decls.Error;
    switch(kindOf(targetType)) {
        case kindMap:
            // Maps yield their value type as the selection result type.
            MapType mapType = targetType.getMapType();
            resultType = mapType.getValueType();
            break;
        case kindObject:
            // Objects yield their field type declaration as the selection result type, but only if
            // the field is defined.
            FieldType fieldType = lookupFieldType(location(e), targetType.getMessageType(), sel.getField());
            if (fieldType != null) {
                resultType = fieldType.type;
            }
            break;
        case kindTypeParam:
            // Set the operand type to DYN to prevent assignment to a potentionally incorrect type
            // at a later point in type-checking. The isAssignable call will update the type
            // substitutions for the type param under the covers.
            isAssignable(Decls.Dyn, targetType);
            // Also, set the result type to DYN.
            resultType = Decls.Dyn;
            break;
        default:
            // in order to allow forward progress on the check.
            if (isDynOrError(targetType)) {
                resultType = Decls.Dyn;
            } else {
                errors.typeDoesNotSupportFieldSelection(location(e), targetType);
            }
            break;
    }
    if (sel.getTestOnly()) {
        resultType = Decls.Bool;
    }
    setType(e, resultType);
}
Also used : CheckerEnv.getObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType) CheckerEnv.isObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType) CheckerEnv.dynElementType(org.projectnessie.cel.checker.CheckerEnv.dynElementType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType) Select(com.google.api.expr.v1alpha1.Expr.Select) IdentDecl(com.google.api.expr.v1alpha1.Decl.IdentDecl) Decl(com.google.api.expr.v1alpha1.Decl) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType)

Example 2 with MapType

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

the class Types method substitute.

/**
 * substitute replaces all direct and indirect occurrences of bound type parameters. Unbound type
 * parameters are replaced by DYN if typeParamToDyn is true.
 */
static Type substitute(Mapping m, Type t, boolean typeParamToDyn) {
    Type tSub = m.find(t);
    if (tSub != null) {
        return substitute(m, tSub, typeParamToDyn);
    }
    Kind kind = kindOf(t);
    if (typeParamToDyn && kind == Kind.kindTypeParam) {
        return Decls.Dyn;
    }
    switch(kind) {
        case kindAbstract:
            // TODO: implement!
            AbstractType at = t.getAbstractType();
            List<Type> params = new ArrayList<>(at.getParameterTypesCount());
            for (Type p : at.getParameterTypesList()) {
                params.add(substitute(m, p, typeParamToDyn));
            }
            return Decls.newAbstractType(at.getName(), params);
        case kindFunction:
            FunctionType fn = t.getFunction();
            Type rt = substitute(m, fn.getResultType(), typeParamToDyn);
            List<Type> args = new ArrayList<>(fn.getArgTypesCount());
            for (Type a : fn.getArgTypesList()) {
                args.add(substitute(m, a, typeParamToDyn));
            }
            return Decls.newFunctionType(rt, args);
        case kindList:
            return Decls.newListType(substitute(m, t.getListType().getElemType(), typeParamToDyn));
        case kindMap:
            MapType mt = t.getMapType();
            return Decls.newMapType(substitute(m, mt.getKeyType(), typeParamToDyn), substitute(m, mt.getValueType(), typeParamToDyn));
        case kindType:
            if (t.getType() != Type.getDefaultInstance()) {
                return Decls.newTypeType(substitute(m, t.getType(), typeParamToDyn));
            }
            return t;
        default:
            return t;
    }
}
Also used : AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) WellKnownType(com.google.api.expr.v1alpha1.Type.WellKnownType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) MapType(com.google.api.expr.v1alpha1.Type.MapType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) ArrayList(java.util.ArrayList) MapType(com.google.api.expr.v1alpha1.Type.MapType)

Example 3 with MapType

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

the class PbTypeDescriptionTest method checkedType.

@Test
void checkedType() {
    Db pbdb = newDb();
    TestAllTypes msg = TestAllTypes.getDefaultInstance();
    String msgName = msg.getDescriptorForType().getFullName();
    pbdb.registerMessage(msg);
    PbTypeDescription td = pbdb.describeType(msgName);
    assertThat(td).isNotNull();
    FieldDescription field = td.fieldByName("map_string_string");
    assertThat(field).isNotNull();
    Type mapType = Decls.newMapType(Decls.String, Decls.String);
    assertThat(field.checkedType()).isEqualTo(mapType);
    field = td.fieldByName("repeated_nested_message");
    assertThat(field).isNotNull();
    Type listType = Decls.newListType(Decls.newObjectType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage"));
    assertThat(field.checkedType()).isEqualTo(listType);
}
Also used : Type(com.google.api.expr.v1alpha1.Type) ByteString(com.google.protobuf.ByteString) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) NestedTestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.NestedTestAllTypes) Db.newDb(org.projectnessie.cel.common.types.pb.Db.newDb) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with MapType

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

the class JacksonTypeDescriptionTest method checkMapType.

private void checkMapType(JacksonRegistry reg, String prop, Class<?> keyClass, com.google.api.expr.v1alpha1.Type keyType, 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::isMapLikeType).isEqualTo(true);
    assertThat(javaType.getKeyType()).extracting(JavaType::getRawClass).isSameAs(keyClass);
    assertThat(javaType.getContentType()).extracting(JavaType::getRawClass).isSameAs(valueClass);
    assertThat(ft.type).extracting(com.google.api.expr.v1alpha1.Type::getMapType).extracting(MapType::getKeyType, MapType::getValueType).containsExactly(keyType, 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)

Example 5 with MapType

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

the class Types method notReferencedIn.

/**
 * notReferencedIn checks whether the type doesn't appear directly or transitively within the
 * other type. This is a standard requirement for type unification, commonly referred to as the
 * "occurs check".
 */
static boolean notReferencedIn(Mapping m, Type t, Type withinType) {
    if (t.equals(withinType)) {
        return false;
    }
    Kind withinKind = kindOf(withinType);
    switch(withinKind) {
        case kindTypeParam:
            Type wtSub = m.find(withinType);
            if (wtSub == null) {
                return true;
            }
            return notReferencedIn(m, t, wtSub);
        case kindAbstract:
            for (Type pt : withinType.getAbstractType().getParameterTypesList()) {
                if (!notReferencedIn(m, t, pt)) {
                    return false;
                }
            }
            return true;
        case kindFunction:
            FunctionType fn = withinType.getFunction();
            List<Type> types = flattenFunctionTypes(fn);
            for (Type a : types) {
                if (!notReferencedIn(m, t, a)) {
                    return false;
                }
            }
            return true;
        case kindList:
            return notReferencedIn(m, t, withinType.getListType().getElemType());
        case kindMap:
            MapType mt = withinType.getMapType();
            return notReferencedIn(m, t, mt.getKeyType()) && notReferencedIn(m, t, mt.getValueType());
        case kindWrapper:
            return notReferencedIn(m, t, Decls.newPrimitiveType(withinType.getWrapper()));
        default:
            return true;
    }
}
Also used : AbstractType(com.google.api.expr.v1alpha1.Type.AbstractType) PrimitiveType(com.google.api.expr.v1alpha1.Type.PrimitiveType) Type(com.google.api.expr.v1alpha1.Type) WellKnownType(com.google.api.expr.v1alpha1.Type.WellKnownType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) MapType(com.google.api.expr.v1alpha1.Type.MapType) FunctionType(com.google.api.expr.v1alpha1.Type.FunctionType) MapType(com.google.api.expr.v1alpha1.Type.MapType)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)4 MapType (com.google.api.expr.v1alpha1.Type.MapType)4 AbstractType (com.google.api.expr.v1alpha1.Type.AbstractType)2 FunctionType (com.google.api.expr.v1alpha1.Type.FunctionType)2 PrimitiveType (com.google.api.expr.v1alpha1.Type.PrimitiveType)2 WellKnownType (com.google.api.expr.v1alpha1.Type.WellKnownType)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 NestedTestAllTypes (com.google.api.expr.test.v1.proto3.TestAllTypesProto.NestedTestAllTypes)1 TestAllTypes (com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes)1 Decl (com.google.api.expr.v1alpha1.Decl)1 IdentDecl (com.google.api.expr.v1alpha1.Decl.IdentDecl)1 Select (com.google.api.expr.v1alpha1.Expr.Select)1 ListType (com.google.api.expr.v1alpha1.Type.ListType)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)1 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)1 CheckerEnv.isObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType)1