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));
}
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);
}
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;
}
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));
}
Aggregations