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();
}
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));
}
}
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();
}
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);
}
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;
}
Aggregations