Search in sources :

Example 6 with Name

use of com.google.api.codegen.util.Name in project toolkit by googleapis.

the class PhpApiMethodParamTransformer method getOptionalArrayParamDoc.

private ParamDocView getOptionalArrayParamDoc(GapicMethodContext context, Iterable<FieldModel> fields) {
    MapParamDocView.Builder paramDoc = MapParamDocView.newBuilder();
    Name optionalArgsName = Name.from("optional", "args");
    paramDoc.paramName(context.getNamer().localVarName(optionalArgsName));
    paramDoc.typeName(context.getNamer().getOptionalArrayTypeName());
    paramDoc.firstLine("Optional.");
    paramDoc.remainingLines(ImmutableList.<String>of());
    paramDoc.arrayKeyDocs(ImmutableList.<ParamDocView>builder().addAll(getMethodParamDocs(context, fields)).addAll(getCallSettingsParamDocList(context)).build());
    return paramDoc.build();
}
Also used : MapParamDocView(com.google.api.codegen.viewmodel.MapParamDocView) MapParamDocView(com.google.api.codegen.viewmodel.MapParamDocView) SimpleParamDocView(com.google.api.codegen.viewmodel.SimpleParamDocView) ParamDocView(com.google.api.codegen.viewmodel.ParamDocView) Name(com.google.api.codegen.util.Name)

Example 7 with Name

use of com.google.api.codegen.util.Name in project toolkit by googleapis.

the class SurfaceNamer method getResourceNameFieldSetFunctionName.

/**
 * The function name to set a field that is a resource name class.
 */
public String getResourceNameFieldSetFunctionName(FieldConfig fieldConfig) {
    FieldModel type = fieldConfig.getField();
    Name identifier = fieldConfig.getField().getNameAsParameterName();
    Name resourceName = getResourceTypeNameObject(fieldConfig.getResourceNameConfig());
    if (type.isMap()) {
        return getNotImplementedString("SurfaceNamer.getResourceNameFieldSetFunctionName:map-type");
    } else if (type.isRepeated()) {
        return publicMethodName(Name.from("add", "all").join(identifier).join("with").join(resourceName).join("list"));
    } else {
        return publicMethodName(Name.from("set").join(identifier).join("with").join(resourceName));
    }
}
Also used : FieldModel(com.google.api.codegen.config.FieldModel) Name(com.google.api.codegen.util.Name)

Example 8 with Name

use of com.google.api.codegen.util.Name in project toolkit by googleapis.

the class DiscoConfigTransformer method generateConfig.

public ViewModel generateConfig(DiscoApiModel model, String outputPath) {
    // Map of methods to unique resource names.
    Map<Method, Name> methodToResourceName = new HashMap<>();
    // Map of Methods to resource name patterns.
    ImmutableMap.Builder<Method, String> methodToNamePattern = ImmutableMap.builder();
    // Maps visited simple resource names to canonical name patterns. Used to check for collisions in simple resource names.
    Map<Name, String> simpleResourceToFirstPatternMap = new HashMap<>();
    for (Method method : model.getDocument().methods()) {
        String namePattern = DiscoGapicParser.getCanonicalPath(method);
        methodToNamePattern.put(method, namePattern);
        Name simpleResourceName = DiscoGapicParser.getResourceIdentifier(method.flatPath());
        String collisionPattern = simpleResourceToFirstPatternMap.get(simpleResourceName);
        if (collisionPattern != null && !collisionPattern.equals(namePattern)) {
            // Collision with another path template with the same simple resource name; qualify this resource name.
            Name qualifiedResourceName = DiscoGapicParser.getQualifiedResourceIdentifier(namePattern);
            methodToResourceName.put(method, qualifiedResourceName);
        } else {
            simpleResourceToFirstPatternMap.put(simpleResourceName, namePattern);
            methodToResourceName.put(method, simpleResourceName);
        }
    }
    // Map of base resource identifiers to all canonical name patterns that use that identifier.
    ImmutableMap<Method, Name> methodToResourceNames = ImmutableMap.<Method, Name>builder().putAll(methodToResourceName).build();
    return ConfigView.newBuilder().templateFileName(CONFIG_TEMPLATE_FILE).outputPath(outputPath).type(CONFIG_PROTO_TYPE).configSchemaVersion(DEFAULT_CONFIG_SCHEMA_VERSION).languageSettings(generateLanguageSettings(model.getDocument())).license(generateLicense()).interfaces(generateInterfaces(model, methodToNamePattern.build(), methodToResourceNames)).resourceNameGeneration(generateResourceNameGenerations(model.getDocument(), methodToResourceNames)).build();
}
Also used : HashMap(java.util.HashMap) Method(com.google.api.codegen.discovery.Method) ImmutableMap(com.google.common.collect.ImmutableMap) Name(com.google.api.codegen.util.Name)

Example 9 with Name

use of com.google.api.codegen.util.Name in project toolkit by googleapis.

the class DiscoGapicParser method methodAsName.

/**
 * Formats the method as a Name. Methods are generally in the format
 * "[api].[resource].[function]".
 */
public static Name methodAsName(Method method) {
    String[] pieces = method.id().split(REGEX_DELIMITER);
    String resourceLastName = pieces[pieces.length - 2];
    if (!method.isPluralMethod()) {
        resourceLastName = Inflector.singularize(resourceLastName);
    }
    Name resource = Name.anyCamel(resourceLastName);
    for (int i = pieces.length - 3; i > 0; i--) {
        resource = Name.anyCamel(pieces[i]).join(resource);
    }
    Name function = Name.anyCamel(pieces[pieces.length - 1]);
    return function.join(resource);
}
Also used : Name(com.google.api.codegen.util.Name)

Example 10 with Name

use of com.google.api.codegen.util.Name in project toolkit by googleapis.

the class DiscoGapicParser method getCanonicalPath.

/**
 * Get the canonical path for a method, in the form "(%s/\{%s\})+" e.g. for a method path
 * "{project}/regions/{region}/addresses", this returns "projects/{project}/regions/{region}".
 */
public static String getCanonicalPath(Method method) {
    String namePattern = method.flatPath();
    // Ensure the first path segment is a string literal representing a resource type.
    if (namePattern.charAt(0) == '{') {
        String firstResource = Inflector.pluralize(namePattern.substring(1, namePattern.indexOf("}")));
        namePattern = String.format("%s/%s", firstResource, namePattern);
    }
    // Remove any trailing non-bracketed substring
    if (!namePattern.endsWith("}") && namePattern.contains("}")) {
        namePattern = namePattern.substring(0, namePattern.lastIndexOf('}') + 1);
    }
    // For each sequence of consecutive non-bracketed path segments,
    // replace those segments with the last one in the sequence.
    Matcher m = UNBRACKETED_PATH_SEGMENTS_PATTERN.matcher(namePattern);
    if (m.find()) {
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= m.groupCount(); i++) {
            String multipleSegment = m.group(i);
            String[] segmentPieces = multipleSegment.split("/");
            Name segment = Name.anyCamel(segmentPieces[segmentPieces.length - 1]);
            m.appendReplacement(sb, String.format("}/%s/{", segment.toLowerCamel()));
        }
        namePattern = m.appendTail(sb).toString();
    }
    return namePattern;
}
Also used : Matcher(java.util.regex.Matcher) Name(com.google.api.codegen.util.Name)

Aggregations

Name (com.google.api.codegen.util.Name)10 FieldModel (com.google.api.codegen.config.FieldModel)3 MapParamDocView (com.google.api.codegen.viewmodel.MapParamDocView)2 ParamDocView (com.google.api.codegen.viewmodel.ParamDocView)2 SimpleParamDocView (com.google.api.codegen.viewmodel.SimpleParamDocView)2 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 Method (com.google.api.codegen.discovery.Method)1 GapicInterfaceContext (com.google.api.codegen.transformer.GapicInterfaceContext)1 TypeName (com.google.api.codegen.util.TypeName)1 ApiMethodView (com.google.api.codegen.viewmodel.ApiMethodView)1 ViewModel (com.google.api.codegen.viewmodel.ViewModel)1 VersionIndexRequireView (com.google.api.codegen.viewmodel.metadata.VersionIndexRequireView)1 VersionIndexView (com.google.api.codegen.viewmodel.metadata.VersionIndexView)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1