Search in sources :

Example 16 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class PhpModelTypeNameConverter method getTypeName.

/**
 * This function recursively determines the PHP type name for a proto message. Recursion is
 * required because in PHP nested messages are handled differently from non-nested messages. For
 * example, the following proto definition: <code>
 * package example;
 * message Top {
 *  message Nested {
 *    message DeepNested {}
 *  }
 * }
 * </code> will generate these PHP types:
 *
 * <ul>
 *   <li>\Example\Top
 *   <li>\Example\Top_Nested
 *   <li>\Example\Top_Nested_DeepNested
 * </ul>
 *
 * <p>To correctly output these type names, we need to check whether the parent of a proto element
 * is a message, and if so use '_' as a separator.
 */
private TypeName getTypeName(ProtoElement elem, int maxDepth) {
    ProtoElement parent = elem.getParent();
    if (parent != null && parent instanceof MessageType) {
        MessageType parentMessage = (MessageType) parent;
        if (parentMessage.isCyclic()) {
            throw new IllegalStateException("Cannot determine type for cyclic message: " + parentMessage);
        }
        if (maxDepth == 0) {
            throw new IllegalStateException("Cannot determine type for deeply nested message");
        }
        String parentFullName = getTypeName(parent, maxDepth - 1).getFullName();
        String fullName = String.format("%s_%s", parentFullName, elem.getSimpleName());
        String nickName = fullName.substring(fullName.lastIndexOf("\\") + 1);
        return new TypeName(fullName, nickName);
    }
    return typeNameConverter.getTypeName(getTypeNameString(elem));
}
Also used : TypeName(com.google.api.codegen.util.TypeName) ProtoElement(com.google.api.tools.framework.model.ProtoElement) MessageType(com.google.api.tools.framework.model.MessageType)

Example 17 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class PythonSurfaceNamer method getParamTypeName.

@Override
public String getParamTypeName(ImportTypeTable typeTable, TypeModel type) {
    if (type.isMap()) {
        TypeName mapTypeName = new TypeName("dict");
        TypeName keyTypeName = new TypeName(getParamTypeNameForElementType(type.getMapKeyField().getType()));
        TypeName valueTypeName = new TypeName(getParamTypeNameForElementType(type.getMapValueField().getType()));
        return new TypeName(mapTypeName.getFullName(), mapTypeName.getNickname(), "%s[%i -> %i]", keyTypeName, valueTypeName).getFullName();
    }
    if (type.isRepeated()) {
        TypeName listTypeName = new TypeName("list");
        TypeName elementTypeName = new TypeName(getParamTypeNameForElementType(type));
        return new TypeName(listTypeName.getFullName(), listTypeName.getNickname(), "%s[%i]", elementTypeName).getFullName();
    }
    return getParamTypeNameForElementType(type);
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 18 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class RubySurfaceNamer method getParamTypeName.

@Override
public String getParamTypeName(ImportTypeTable typeTable, TypeModel type) {
    if (type.isMap()) {
        String keyTypeName = typeTable.getFullNameForElementType(type.getMapKeyField());
        String valueTypeName = typeTable.getFullNameForElementType(type.getMapValueField());
        if (type.getMapValueField().isMessage()) {
            valueTypeName += " | Hash";
        }
        return new TypeName(typeTable.getFullNameFor(type), typeTable.getNicknameFor(type), "%s{%i => %i}", new TypeName(keyTypeName), new TypeName(valueTypeName)).getFullName();
    }
    String elementTypeName = typeTable.getFullNameForElementType(type);
    if (type.isMessage()) {
        elementTypeName += " | Hash";
    }
    if (type.isRepeated()) {
        return new TypeName(typeTable.getFullNameFor(type), typeTable.getNicknameFor(type), "%s<%i>", new TypeName(elementTypeName)).getFullName();
    }
    return elementTypeName;
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Example 19 with TypeName

use of com.google.api.codegen.util.TypeName in project toolkit by googleapis.

the class JavaTypeTable method getAndSaveNicknameForInnerType.

@Override
public String getAndSaveNicknameForInnerType(String containerFullName, String innerTypeShortName) {
    int lastDotIndex = innerTypeShortName.lastIndexOf('.');
    if (lastDotIndex != -1) {
        throw new IllegalArgumentException("Cannot have qualified innerTypeShortName.");
    }
    String fullName = containerFullName + "." + innerTypeShortName;
    return getAndSaveNicknameFor(new TypeName(fullName, innerTypeShortName, containerFullName));
}
Also used : TypeName(com.google.api.codegen.util.TypeName)

Aggregations

TypeName (com.google.api.codegen.util.TypeName)19 MessageType (com.google.api.tools.framework.model.MessageType)2 ProtoElement (com.google.api.tools.framework.model.ProtoElement)2 FieldModel (com.google.api.codegen.config.FieldModel)1 Schema (com.google.api.codegen.discovery.Schema)1 ImportTypeTable (com.google.api.codegen.transformer.ImportTypeTable)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 TypeNameConverter (com.google.api.codegen.util.TypeNameConverter)1 ProtoFile (com.google.api.tools.framework.model.ProtoFile)1