Search in sources :

Example 6 with MessageType

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);
}
Also used : TypeName(com.google.api.codegen.util.TypeName) ProtoElement(com.google.api.tools.framework.model.ProtoElement) ProtoFile(com.google.api.tools.framework.model.ProtoFile) MessageType(com.google.api.tools.framework.model.MessageType)

Example 7 with MessageType

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);
}
Also used : ProtoFile(com.google.api.tools.framework.model.ProtoFile) ImmutableMap(com.google.common.collect.ImmutableMap) Field(com.google.api.tools.framework.model.Field) ResourceNameMessageConfigProto(com.google.api.codegen.ResourceNameMessageConfigProto) MessageType(com.google.api.tools.framework.model.MessageType) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Example 8 with MessageType

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));
}
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 9 with MessageType

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;
}
Also used : Field(com.google.api.tools.framework.model.Field) ProtoFile(com.google.api.tools.framework.model.ProtoFile) MessageType(com.google.api.tools.framework.model.MessageType) HashSet(java.util.HashSet)

Aggregations

MessageType (com.google.api.tools.framework.model.MessageType)9 ImmutableList (com.google.common.collect.ImmutableList)4 Field (com.google.api.tools.framework.model.Field)3 ProtoFile (com.google.api.tools.framework.model.ProtoFile)3 TypeName (com.google.api.codegen.util.TypeName)2 GrpcElementDocView (com.google.api.codegen.viewmodel.GrpcElementDocView)2 GrpcMessageDocView (com.google.api.codegen.viewmodel.GrpcMessageDocView)2 HeaderRequestParamView (com.google.api.codegen.viewmodel.HeaderRequestParamView)2 ProtoElement (com.google.api.tools.framework.model.ProtoElement)2 HashSet (java.util.HashSet)2 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)1 GapicMethodConfig (com.google.api.codegen.config.GapicMethodConfig)1 ProtoField (com.google.api.codegen.config.ProtoField)1 EnumType (com.google.api.tools.framework.model.EnumType)1 Method (com.google.api.tools.framework.model.Method)1 Model (com.google.api.tools.framework.model.Model)1 ProtoContainerElement (com.google.api.tools.framework.model.ProtoContainerElement)1 TestDataLocator (com.google.api.tools.framework.model.testing.TestDataLocator)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Nullable (javax.annotation.Nullable)1