Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Fix compute resource parsing #2478

Merged
merged 6 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ jobs:
# Directory to output the generated Compute client.
COMPUTE_OUT: /tmp/discogapic_out
# Commit hash to use the discover-artifact-manager repo from
DISCOVERY_REPO_VERSION: 661dacf3b5c7d2ca359d9788e5498025e6ce0437
DISCOVERY_REPO_VERSION: d6b87916dd4205e4e8bdc398b09d2935c36a0636
DISCOVERY_REPO_PATH: https://raw.githubusercontent.com/googleapis/discovery-artifact-manager
docker:
- image: circleci/openjdk:8-jdk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,11 @@ public static Name getRequestName(Method method) {
}

/**
* Get the canonical path for a path, in the form "(%s/\{%s\})+" e.g. for a method path
* "{project}/regions/{foo}/addresses", this returns "projects/{project}/regions/{region}".
* Get the canonical path for a path, in the form "((\{%s\}|%s)/)+{%s\}" e.g. for a method path
* "{project}/regions/{foo}/addresses", this returns "{project}/regions/{region}". The resulting
* path will end with a {BINDING}.
*/
public static String getCanonicalPath(String namePattern) {
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ private StaticLangApiResourceNameView generateResourceNameClass(
resourceNameView.name(resourceName);
resourceNameView.typeName(nameFormatter.publicClassName(Name.anyCamel(resourceName)));
resourceNameView.pathTemplate(nameConfig.getNamePattern());
resourceNameView.serviceAddress(context.getDocContext().getServiceAddress());

List<StaticLangMemberView> properties = new LinkedList<>();
for (Map.Entry<String, Schema> entry : method.parameters().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public abstract class StaticLangApiResourceNameView
// The template for the path, e.g. "projects/{projects}/topic/{topic}"
public abstract String pathTemplate();

// The leading protocol+hostname string that qualifies a resource path.
// e.g. "https://www.googleapis.com/compute/v1/projects/"
public abstract String serviceAddress();

// The list of path parameter views.
public abstract List<StaticLangMemberView> pathParams();

Expand All @@ -47,6 +51,8 @@ public abstract static class Builder {

public abstract Builder pathTemplate(String val);

public abstract Builder serviceAddress(String val);

public abstract Builder pathParams(List<StaticLangMemberView> val);

public abstract StaticLangApiResourceNameView build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
@if interface.collections
collections:
@join collection : interface.collections
- name_pattern: {@collection.namePattern}
- name_pattern: '{@collection.namePattern}'
entity_name: {@collection.entityName}
@end
@else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("{@resourceName.pathTemplate}");

public static final String SERVICE_ADDRESS = "{@resourceName.serviceAddress}";

private volatile Map<String, String> fieldValuesMap;
@end

Expand Down Expand Up @@ -74,8 +76,12 @@
}

public static {@resourceName.typeName} parse(String formattedString) {
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "{@resourceName.typeName}.parse: formattedString not in valid format");
PATH_TEMPLATE.validatedMatch(resourcePath, "{@resourceName.typeName}.parse: formattedString not in valid format");
return of(
@join param : resourceName.pathParams on ",".add(BREAK)
matchMap.get("{@param.name}")
Expand All @@ -84,7 +90,11 @@
}

public static boolean isParsableFrom(String formattedString) {
return PATH_TEMPLATE.matches(formattedString);
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
return PATH_TEMPLATE.matches(resourcePath);
}
@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public final class ProjectDummyObjectName implements ResourceName {
private final String dummyObject;
private final String project;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}/dummyObjects/{dummyObject}");
PathTemplate.createWithoutUrlEncoding("{project}/dummyObjects/{dummyObject}");

public static final String SERVICE_ADDRESS = "https://www.googleapis.com/compute/v1/projects/";

private volatile Map<String, String> fieldValuesMap;

Expand Down Expand Up @@ -111,16 +113,24 @@ public final class ProjectDummyObjectName implements ResourceName {
}

public static ProjectDummyObjectName parse(String formattedString) {
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "ProjectDummyObjectName.parse: formattedString not in valid format");
PATH_TEMPLATE.validatedMatch(resourcePath, "ProjectDummyObjectName.parse: formattedString not in valid format");
return of(
matchMap.get("dummyObject"),
matchMap.get("project")
);
}

public static boolean isParsableFrom(String formattedString) {
return PATH_TEMPLATE.matches(formattedString);
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
return PATH_TEMPLATE.matches(resourcePath);
}

public static class Builder {
Expand Down Expand Up @@ -226,7 +236,9 @@ public final class ProjectDummyObjectResourceName implements ResourceName {
private final String project;
private final String resource;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}/dummyObjects/{resource}");
PathTemplate.createWithoutUrlEncoding("{project}/dummyObjects/{resource}");

public static final String SERVICE_ADDRESS = "https://www.googleapis.com/compute/v1/projects/";

private volatile Map<String, String> fieldValuesMap;

Expand Down Expand Up @@ -300,16 +312,24 @@ public final class ProjectDummyObjectResourceName implements ResourceName {
}

public static ProjectDummyObjectResourceName parse(String formattedString) {
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "ProjectDummyObjectResourceName.parse: formattedString not in valid format");
PATH_TEMPLATE.validatedMatch(resourcePath, "ProjectDummyObjectResourceName.parse: formattedString not in valid format");
return of(
matchMap.get("project"),
matchMap.get("resource")
);
}

public static boolean isParsableFrom(String formattedString) {
return PATH_TEMPLATE.matches(formattedString);
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
return PATH_TEMPLATE.matches(resourcePath);
}

public static class Builder {
Expand Down Expand Up @@ -415,7 +435,9 @@ public final class ProjectGlobalAddressName implements ResourceName {
private final String address;
private final String project;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}/global/addresses/{address}");
PathTemplate.createWithoutUrlEncoding("{project}/global/addresses/{address}");

public static final String SERVICE_ADDRESS = "https://www.googleapis.com/compute/v1/projects/";

private volatile Map<String, String> fieldValuesMap;

Expand Down Expand Up @@ -489,16 +511,24 @@ public final class ProjectGlobalAddressName implements ResourceName {
}

public static ProjectGlobalAddressName parse(String formattedString) {
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "ProjectGlobalAddressName.parse: formattedString not in valid format");
PATH_TEMPLATE.validatedMatch(resourcePath, "ProjectGlobalAddressName.parse: formattedString not in valid format");
return of(
matchMap.get("address"),
matchMap.get("project")
);
}

public static boolean isParsableFrom(String formattedString) {
return PATH_TEMPLATE.matches(formattedString);
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
return PATH_TEMPLATE.matches(resourcePath);
}

public static class Builder {
Expand Down Expand Up @@ -603,7 +633,9 @@ import javax.annotation.Generated;
public final class ProjectName implements ResourceName {
private final String project;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}");
PathTemplate.createWithoutUrlEncoding("{project}");

public static final String SERVICE_ADDRESS = "https://www.googleapis.com/compute/v1/projects/";

private volatile Map<String, String> fieldValuesMap;

Expand Down Expand Up @@ -667,15 +699,23 @@ public final class ProjectName implements ResourceName {
}

public static ProjectName parse(String formattedString) {
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "ProjectName.parse: formattedString not in valid format");
PATH_TEMPLATE.validatedMatch(resourcePath, "ProjectName.parse: formattedString not in valid format");
return of(
matchMap.get("project")
);
}

public static boolean isParsableFrom(String formattedString) {
return PATH_TEMPLATE.matches(formattedString);
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
return PATH_TEMPLATE.matches(resourcePath);
}

public static class Builder {
Expand Down Expand Up @@ -770,7 +810,9 @@ public final class ProjectRegionAddressName implements ResourceName {
private final String project;
private final String region;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}/regions/{region}/addresses/{address}");
PathTemplate.createWithoutUrlEncoding("{project}/regions/{region}/addresses/{address}");

public static final String SERVICE_ADDRESS = "https://www.googleapis.com/compute/v1/projects/";

private volatile Map<String, String> fieldValuesMap;

Expand Down Expand Up @@ -854,8 +896,12 @@ public final class ProjectRegionAddressName implements ResourceName {
}

public static ProjectRegionAddressName parse(String formattedString) {
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "ProjectRegionAddressName.parse: formattedString not in valid format");
PATH_TEMPLATE.validatedMatch(resourcePath, "ProjectRegionAddressName.parse: formattedString not in valid format");
return of(
matchMap.get("address"),
matchMap.get("project"),
Expand All @@ -864,7 +910,11 @@ public final class ProjectRegionAddressName implements ResourceName {
}

public static boolean isParsableFrom(String formattedString) {
return PATH_TEMPLATE.matches(formattedString);
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
return PATH_TEMPLATE.matches(resourcePath);
}

public static class Builder {
Expand Down Expand Up @@ -982,7 +1032,9 @@ public final class ProjectRegionName implements ResourceName {
private final String project;
private final String region;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}/regions/{region}");
PathTemplate.createWithoutUrlEncoding("{project}/regions/{region}");

public static final String SERVICE_ADDRESS = "https://www.googleapis.com/compute/v1/projects/";

private volatile Map<String, String> fieldValuesMap;

Expand Down Expand Up @@ -1056,16 +1108,24 @@ public final class ProjectRegionName implements ResourceName {
}

public static ProjectRegionName parse(String formattedString) {
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
Map<String, String> matchMap =
PATH_TEMPLATE.validatedMatch(formattedString, "ProjectRegionName.parse: formattedString not in valid format");
PATH_TEMPLATE.validatedMatch(resourcePath, "ProjectRegionName.parse: formattedString not in valid format");
return of(
matchMap.get("project"),
matchMap.get("region")
);
}

public static boolean isParsableFrom(String formattedString) {
return PATH_TEMPLATE.matches(formattedString);
String resourcePath = formattedString;
if (formattedString.startsWith(SERVICE_ADDRESS)) {
resourcePath = formattedString.substring(SERVICE_ADDRESS.length());
}
return PATH_TEMPLATE.matches(resourcePath);
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ interfaces:
# The entity_name is the name to be used as a basis for generated methods and
# classes.
collections:
- name_pattern: projects/{project}
- name_pattern: '{project}'
entity_name: project
- name_pattern: projects/{project}/global/addresses/{address}
- name_pattern: '{project}/global/addresses/{address}'
entity_name: projectGlobalAddress
- name_pattern: projects/{project}/regions/{region}
- name_pattern: '{project}/regions/{region}'
entity_name: projectRegion
- name_pattern: projects/{project}/regions/{region}/addresses/{address}
- name_pattern: '{project}/regions/{region}/addresses/{address}'
entity_name: projectRegionAddress
# Definition for retryable codes.
retry_codes_def:
Expand Down Expand Up @@ -284,11 +284,11 @@ interfaces:
# The entity_name is the name to be used as a basis for generated methods and
# classes.
collections:
- name_pattern: projects/{project}
- name_pattern: '{project}'
entity_name: project
- name_pattern: projects/{project}/dummyObjects/{dummyObject}
- name_pattern: '{project}/dummyObjects/{dummyObject}'
entity_name: projectDummyObject
- name_pattern: projects/{project}/dummyObjects/{resource}
- name_pattern: '{project}/dummyObjects/{resource}'
entity_name: projectDummyObjectResource
# Definition for retryable codes.
retry_codes_def:
Expand Down
Loading