use of com.google.api.tools.framework.model.MessageType in project toolkit by googleapis.
the class CSharpModelTypeNameConverter method getTypeName.
@Override
public TypeName getTypeName(ProtoElement elem) {
// Handle nested types, construct the required type prefix
ProtoElement parentEl = elem.getParent();
String shortNamePrefix = "";
while (parentEl != null && parentEl instanceof MessageType) {
shortNamePrefix = parentEl.getSimpleName() + "+Types+" + shortNamePrefix;
parentEl = parentEl.getParent();
}
String prefix = "";
if (parentEl instanceof ProtoFile) {
ProtoFile protoFile = (ProtoFile) parentEl;
String namespace = protoFile.getProto().getOptions().getCsharpNamespace();
if (Strings.isNullOrEmpty(namespace)) {
for (String name : Splitter.on('.').split(parentEl.getFullName())) {
prefix += Name.from(name).toUpperCamelAndDigits() + ".";
}
} else {
prefix = namespace + ".";
}
}
String shortName = shortNamePrefix + elem.getSimpleName();
return new TypeName(prefix + shortName, shortName);
}
use of com.google.api.tools.framework.model.MessageType in project toolkit by googleapis.
the class ResourceNameMessageConfigs method createMessageResourceTypesConfig.
@Nullable
public static ResourceNameMessageConfigs createMessageResourceTypesConfig(Model model, ConfigProto configProto, String defaultPackage) {
ImmutableMap.Builder<String, ResourceNameMessageConfig> builder = ImmutableMap.builder();
for (ResourceNameMessageConfigProto messageResourceTypesProto : configProto.getResourceNameGenerationList()) {
ResourceNameMessageConfig messageResourceTypeConfig = ResourceNameMessageConfig.createResourceNameMessageConfig(model.getDiagCollector(), messageResourceTypesProto, defaultPackage);
builder.put(messageResourceTypeConfig.messageName(), messageResourceTypeConfig);
}
ImmutableMap<String, ResourceNameMessageConfig> messageResourceTypeConfigMap = builder.build();
ListMultimap<String, FieldModel> fieldsByMessage = ArrayListMultimap.create();
Set<String> seenProtoFiles = new HashSet<>();
for (ProtoFile protoFile : model.getFiles()) {
if (!seenProtoFiles.contains(protoFile.getSimpleName())) {
seenProtoFiles.add(protoFile.getSimpleName());
for (MessageType msg : protoFile.getMessages()) {
ResourceNameMessageConfig messageConfig = messageResourceTypeConfigMap.get(msg.getFullName());
if (messageConfig == null) {
continue;
}
for (Field field : msg.getFields()) {
if (messageConfig.getEntityNameForField(field.getSimpleName()) != null) {
fieldsByMessage.put(msg.getFullName(), new ProtoField(field));
}
}
}
}
}
return new AutoValue_ResourceNameMessageConfigs(messageResourceTypeConfigMap, fieldsByMessage);
}
use of com.google.api.tools.framework.model.MessageType 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.tools.framework.model.MessageType in project toolkit by googleapis.
the class ProtoFileView method protoFiles.
private Set<ProtoFile> protoFiles(Field field) {
Set<ProtoFile> fields = new HashSet<ProtoFile>();
if (field.getType().getKind() != Type.TYPE_MESSAGE) {
return fields;
}
MessageType messageType = field.getType().getMessageType();
fields.add(messageType.getFile());
for (Field f : messageType.getNonCyclicFields()) {
fields.addAll(protoFiles(f));
}
return fields;
}
Aggregations