Search in sources :

Example 1 with ProtoElement

use of com.google.api.tools.framework.model.ProtoElement in project toolkit by googleapis.

the class CommonGapicProvider method generateSnip.

@Nullable
private List<GeneratedResult> generateSnip(String snippetFileName) {
    // Establish required stage for generation.
    model.establishStage(Merged.KEY);
    if (model.getDiagCollector().getErrorCount() > 0) {
        return null;
    }
    // Run the generator for each service.
    List<GeneratedResult> generated = new ArrayList<>();
    for (ElementT element : view.getElementIterable(model)) {
        GeneratedResult result = generator.generate(element, snippetFileName, context);
        String subPath;
        // where the element is an Iterable<> instead of a ProtoElement.
        if (element instanceof ProtoElement) {
            subPath = pathMapper.getOutputPath(((ProtoElement) element).getFullName(), context.getApiConfig());
        } else {
            subPath = pathMapper.getOutputPath(null, context.getApiConfig());
        }
        if (!Strings.isNullOrEmpty(subPath)) {
            subPath = subPath + "/" + result.getFilename();
        } else {
            subPath = result.getFilename();
        }
        GeneratedResult outputResult = GeneratedResult.create(result.getDoc(), subPath);
        generated.add(outputResult);
    }
    // Return result.
    if (model.getDiagCollector().getErrorCount() > 0) {
        return null;
    }
    return generated;
}
Also used : ProtoElement(com.google.api.tools.framework.model.ProtoElement) GeneratedResult(com.google.api.codegen.GeneratedResult) ArrayList(java.util.ArrayList) Nullable(javax.annotation.Nullable)

Example 2 with ProtoElement

use of com.google.api.tools.framework.model.ProtoElement in project toolkit by googleapis.

the class PhpModelTypeNameConverter method getTypeNameString.

/**
 * This function determines the type name as follows: If the proto type name is in TYPE_NAME_MAP,
 * return that value. Else, split on ".", prepend '\' and capitalize each component of the
 * namespace except the message name
 */
private static String getTypeNameString(ProtoElement elem) {
    String fullName = elem.getFullName();
    if (TYPE_NAME_MAP.containsKey(fullName)) {
        return TYPE_NAME_MAP.get(fullName);
    }
    String[] components = fullName.split("\\.");
    String shortName = components[components.length - 1];
    StringBuilder builder = new StringBuilder();
    ProtoElement parentElem = elem.getParent();
    if (parentElem != null && parentElem instanceof ProtoFile) {
        ProtoFile protoFile = (ProtoFile) parentElem;
        String namespace = protoFile.getProto().getOptions().getPhpNamespace();
        if (Strings.isNullOrEmpty(namespace)) {
            for (int index = 0; index < components.length - 1; index++) {
                builder.append('\\').append(components[index].substring(0, 1).toUpperCase()).append(components[index].substring(1));
            }
        } else {
            builder.append('\\').append(CharMatcher.is('\\').trimFrom(namespace));
        }
    }
    builder.append('\\').append(shortName);
    return builder.toString();
}
Also used : ProtoElement(com.google.api.tools.framework.model.ProtoElement) ProtoFile(com.google.api.tools.framework.model.ProtoFile)

Example 3 with ProtoElement

use of com.google.api.tools.framework.model.ProtoElement 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 4 with ProtoElement

use of com.google.api.tools.framework.model.ProtoElement 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)

Aggregations

ProtoElement (com.google.api.tools.framework.model.ProtoElement)4 TypeName (com.google.api.codegen.util.TypeName)2 MessageType (com.google.api.tools.framework.model.MessageType)2 ProtoFile (com.google.api.tools.framework.model.ProtoFile)2 GeneratedResult (com.google.api.codegen.GeneratedResult)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1