Search in sources :

Example 1 with PathSegment

use of com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment in project toolkit by googleapis.

the class CollectionPattern method buildSimpleWildcardEntityName.

/**
 * Builds the entity name for collection resource.
 *
 * <p>Does not handle singular resources or unbounded wildcards.
 *
 * @param pathSegments the full path containing the wildcard segment at wildcardIndex
 * @param wildcardIndex the index of the wildcard segment
 * @return the entity name for a collection resource
 */
private static String buildSimpleWildcardEntityName(List<PathSegment> pathSegments, int wildcardIndex) {
    PathSegment wildcardSegment = pathSegments.get(wildcardIndex);
    if (wildcardIndex == 0) {
        return "unknown";
    }
    PathSegment prevSegment = pathSegments.get(wildcardIndex - 1);
    if (!(prevSegment instanceof LiteralSegment)) {
        return "unknown";
    }
    return LanguageUtil.upperCamelToLowerUnderscore(Inflector.singularize(prevSegment.syntax()));
}
Also used : LiteralSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.LiteralSegment) PathSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment)

Example 2 with PathSegment

use of com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment in project toolkit by googleapis.

the class CollectionPattern method isValidCollectionPattern.

private static boolean isValidCollectionPattern(FieldSegment fieldSegment) {
    ImmutableList<PathSegment> subPath = fieldSegment.getSubPath();
    if (subPath == null) {
        return false;
    }
    if (subPath.size() <= 1) {
        return false;
    }
    boolean containsLiteralWildcardPair = false;
    PathSegment lastSegment = null;
    for (PathSegment segment : subPath) {
        if (segment instanceof WildcardSegment && lastSegment != null && lastSegment instanceof LiteralSegment) {
            containsLiteralWildcardPair = true;
            break;
        }
        lastSegment = segment;
    }
    if (!containsLiteralWildcardPair) {
        return false;
    }
    return true;
}
Also used : LiteralSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.LiteralSegment) PathSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment) WildcardSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.WildcardSegment)

Example 3 with PathSegment

use of com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment in project toolkit by googleapis.

the class CollectionPattern method buildLastWildcardEntityName.

/**
 * Builds an entity name for the final wildcard path segment in pathSegments.
 *
 * <p>The entity name of a singular resource will be prefixed by its parent collection resource.
 *
 * @param pathSegments the full path containing the wildcard segment at wildcardIndex
 * @param wildcardIndex the index of the wildcard segment
 * @return the entity name that corresponds to the wildcard segment
 */
private static String buildLastWildcardEntityName(List<PathSegment> pathSegments, int wildcardIndex) {
    String collectionEntityName = buildSimpleWildcardEntityName(pathSegments, wildcardIndex);
    // If there are an odd number of remaining segments, then the last segment is for a singular
    // resource. For example, "users/*/profile" has an odd number (1) segment after the wildcard.
    // "users/*" has an even number (0) segments after the wildcard.
    int remainingSegments = pathSegments.size() - wildcardIndex - 1;
    if (remainingSegments > 0) {
        PathSegment nextSegment = pathSegments.get(wildcardIndex + 1);
        String singularEntityName = LanguageUtil.upperCamelToLowerUnderscore(nextSegment.syntax());
        String entityName = String.format("%s_%s", collectionEntityName, singularEntityName);
        return appendUnboundWildcardSuffix(pathSegments, wildcardIndex, entityName);
    }
    return appendUnboundWildcardSuffix(pathSegments, wildcardIndex, collectionEntityName);
}
Also used : PathSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment)

Example 4 with PathSegment

use of com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment in project toolkit by googleapis.

the class CollectionPattern method getCollectionPatternsFromMethod.

/**
 * Returns a list of CollectionPattern objects.
 */
public static List<CollectionPattern> getCollectionPatternsFromMethod(Method method) {
    List<CollectionPattern> collectionPatterns = new LinkedList<CollectionPattern>();
    HttpAttribute httpAttr = method.getAttribute(HttpAttribute.KEY);
    if (httpAttr != null) {
        for (PathSegment pathSegment : httpAttr.getPath()) {
            if (CollectionPattern.isValidCollectionPattern(pathSegment)) {
                collectionPatterns.add(CollectionPattern.create((FieldSegment) pathSegment));
            }
        }
    }
    return collectionPatterns;
}
Also used : HttpAttribute(com.google.api.tools.framework.aspects.http.model.HttpAttribute) FieldSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.FieldSegment) PathSegment(com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment) LinkedList(java.util.LinkedList)

Aggregations

PathSegment (com.google.api.tools.framework.aspects.http.model.HttpAttribute.PathSegment)4 LiteralSegment (com.google.api.tools.framework.aspects.http.model.HttpAttribute.LiteralSegment)2 HttpAttribute (com.google.api.tools.framework.aspects.http.model.HttpAttribute)1 FieldSegment (com.google.api.tools.framework.aspects.http.model.HttpAttribute.FieldSegment)1 WildcardSegment (com.google.api.tools.framework.aspects.http.model.HttpAttribute.WildcardSegment)1 LinkedList (java.util.LinkedList)1