Skip to content

Commit

Permalink
feat(build_gen): inject Locations mixin proto deps (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz committed Jul 15, 2021
1 parent 72e03de commit 15c1f88
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 10 deletions.
9 changes: 9 additions & 0 deletions bazel/src/main/java/com/google/api/codegen/bazel/ApiDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class ApiDir {
private static Pattern SERVICE_YAML_NAME_VERSION =
Pattern.compile("_(?<version>[a-zA-Z]+\\d+[\\w]*)\\.yaml");
private static String CLOUD_AUTH_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
private static String LOCATIONS_MIXIN = "name: google.cloud.location.Locations";

private final Map<String, String> serviceYamlPaths = new TreeMap<>();
private final Map<String, Boolean> cloudScopes = new TreeMap<>();
private final Map<String, Boolean> containLocations = new TreeMap<>();

Map<String, String> getServiceYamlPaths() {
return serviceYamlPaths;
Expand All @@ -37,6 +39,10 @@ Map<String, Boolean> getCloudScopes() {
return cloudScopes;
}

Map<String, Boolean> getContainsLocations() {
return containLocations;
}

void parseYamlFile(String fileName, String fileBody) {
// It is a service yaml
Matcher m = SERVICE_YAML_TYPE.matcher(fileBody);
Expand All @@ -48,6 +54,9 @@ void parseYamlFile(String fileName, String fileBody) {
if (fileBody.contains(CLOUD_AUTH_SCOPE)) {
cloudScopes.put(verKey, true);
}
if (fileBody.contains(LOCATIONS_MIXIN)) {
containLocations.put(verKey, true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ApiVersionedDir {

private static String CLOUD_AUTH_SCOPE = "https://www.googleapis.com/auth/cloud-platform";

private static String LOCATIONS_MIXIN = "google.cloud.location.Locations";

private static final String[] PRESERVED_PROTO_LIBRARY_STRING_ATTRIBUTES = {
// TypeScript:
"package_name",
Expand Down Expand Up @@ -163,6 +165,8 @@ class ApiVersionedDir {
// https://www.googleapis.com/auth/cloud-platform
private boolean cloudScope;

private boolean containsLocations;

// Names of *_gapic_assembly_* rules (since they may be overridden by the user)
private final Map<String, String> assemblyPkgRulesNames = new HashMap<>();

Expand Down Expand Up @@ -242,6 +246,10 @@ Map<String, String> getAssemblyPkgRulesNames() {
return assemblyPkgRulesNames;
}

boolean hasLocations() {
return this.containsLocations;
}

void parseYamlFile(String fileName, String fileBody) {
// It is a gapic yaml
Matcher m = GAPIC_YAML_TYPE.matcher(fileBody);
Expand Down Expand Up @@ -271,9 +279,11 @@ void parseYamlFile(String fileName, String fileBody) {
if (m.find()) {
serviceYamlPath = fileName;

if (fileBody.contains(CLOUD_AUTH_SCOPE)) {
cloudScope = true;
}
// API Servic config specifies the use of the Cloud oauth scope.
this.cloudScope = fileBody.contains(CLOUD_AUTH_SCOPE);

// API Serivce config has Locations API.
this.containsLocations = fileBody.contains(LOCATIONS_MIXIN);
}
}

Expand Down Expand Up @@ -411,10 +421,10 @@ void injectFieldsFromTopLevel() {
serviceYamlPath = version + '/' + topLevelServiceYaml;
}

Boolean topLevelCloudScope = parent.getCloudScopes().get(version);
if (topLevelCloudScope == null) {
topLevelCloudScope = parent.getCloudScopes().getOrDefault("", false);
}
cloudScope = topLevelCloudScope;
boolean topLevelCloudScope = parent.getCloudScopes().getOrDefault(version, false);
cloudScope = topLevelCloudScope ? topLevelCloudScope : cloudScope;

boolean topLevelContainsLocations = parent.getContainsLocations().getOrDefault(version, false);
containsLocations = topLevelContainsLocations ? topLevelContainsLocations : containsLocations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ proto_library_with_info(
name = "{{name}}_proto_with_info",
deps = [
":{{name}}_proto",
"//google/cloud:common_resources_proto",
{{extra_imports}}
],
)

Expand Down Expand Up @@ -87,7 +87,7 @@ java_gapic_assembly_gradle_pkg(
":{{name}}_java_gapic",
":{{name}}_java_grpc",
":{{name}}_java_proto",
":{{name}}_proto",
":{{name}}_proto",{{extra_imports_java}}
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.regex.Pattern;

class BazelBuildFileView {
private static final String COMMON_RESOURCES_PROTO = "//google/cloud:common_resources_proto";
private static final Pattern LABEL_NAME = Pattern.compile(":\\w+$");
private final Map<String, String> tokens = new HashMap<>();
private final Map<String, Map<String, String>> overriddenStringAttributes = new HashMap<>();
Expand All @@ -41,6 +42,13 @@ class BazelBuildFileView {
tokens.put("version", bp.getVersion());
tokens.put("package", bp.getProtoPackage());

Set<String> extraImports = new TreeSet<>();
extraImports.add(COMMON_RESOURCES_PROTO);
if (bp.hasLocations() && !bp.getProtoPackage().equals("google.cloud.location")) {
extraImports.add("//google/cloud/location:location_proto");
}
tokens.put("extra_imports", joinSetWithIndentation(extraImports));

String packPrefix = bp.getProtoPackage().replace(".", "/") + '/';
Set<String> actualImports = new TreeSet<>();
for (String imp : bp.getImports()) {
Expand Down Expand Up @@ -107,10 +115,16 @@ class BazelBuildFileView {
: service;
javaTests.add(javaPackage + "." + actualService + "ClientTest");
}

// Remove common_resources.proto because it is only needed for the proto_library_with_info target.
extraImports.remove(COMMON_RESOURCES_PROTO);
actualImports.addAll(extraImports);

tokens.put("java_tests", joinSetWithIndentation(javaTests));
tokens.put("java_gapic_deps", joinSetWithIndentationNl(mapJavaGapicDeps(actualImports)));
tokens.put(
"java_gapic_test_deps", joinSetWithIndentationNl(mapJavaGapicTestDeps(actualImports)));
tokens.put("extra_imports_java", joinSetWithIndentationNl(mapJavaGapicAssemblyPkgDeps(extraImports)));

// Construct GAPIC import path & package name based on go_package proto option
String protoPkg = bp.getProtoPackage();
Expand Down Expand Up @@ -207,18 +221,35 @@ private Set<String> mapJavaGapicDeps(Set<String> protoImports) {
} else if (protoImport.endsWith(":service_proto")
|| protoImport.endsWith(":httpbody_proto")) {
javaImports.add(replaceLabelName(protoImport, ":api_java_proto"));
} else if (protoImport.endsWith(":location_proto")) {
javaImports.add("//google/cloud/location:location_java_proto");
javaImports.add("//google/cloud/location:location_java_grpc");
}
}
return javaImports;
}

private Set<String> mapJavaGapicAssemblyPkgDeps(Set<String> protoImports) {
Set<String> asemmblyPkgDeps = new TreeSet<>();
for (String protoImport : protoImports) {
if (protoImport.endsWith(":location_proto")) {
asemmblyPkgDeps.add("//google/cloud/location:location_java_proto");
asemmblyPkgDeps.add("//google/cloud/location:location_java_grpc");
}
asemmblyPkgDeps.add(protoImport);
}
return asemmblyPkgDeps;
}

private Set<String> mapJavaGapicTestDeps(Set<String> protoImports) {
Set<String> javaImports = new TreeSet<>();
for (String protoImport : protoImports) {
if (protoImport.endsWith(":iam_policy_proto")
|| protoImport.endsWith(":policy_proto")
|| protoImport.endsWith(":options_proto")) {
javaImports.add(replaceLabelName(protoImport, ":iam_java_grpc"));
} else if (protoImport.endsWith(":location_proto")) {
javaImports.add("//google/cloud/location:location_java_grpc");
}
}
return javaImports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ title: Example Library API

apis:
- name: google.example.library.v1.LibraryService
- name: google.cloud.location.Locations

documentation:
summary: A simple Google Example Library API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ proto_library_with_info(
name = "library_proto_with_info",
deps = [
":library_proto",
"//google/cloud/location:location_proto",
"//google/cloud:common_resources_proto",
],
)
Expand Down Expand Up @@ -71,10 +72,13 @@ java_gapic_library(
service_yaml = "//google/example/library:library_example_v1.yaml",
test_deps = [
":library_java_grpc",
"//google/cloud/location:location_java_grpc",
],
deps = [
":library_java_proto",
"//google/api:api_java_proto",
"//google/cloud/location:location_java_grpc",
"//google/cloud/location:location_java_proto",
],
)

Expand All @@ -94,6 +98,9 @@ java_gapic_assembly_gradle_pkg(
":library_java_grpc",
":library_java_proto",
":library_proto",
"//google/cloud/location:location_java_grpc",
"//google/cloud/location:location_java_proto",
"//google/cloud/location:location_proto",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ proto_library_with_info(
name = "library_proto_with_info",
deps = [
":library_proto",
"//google/cloud/location:location_proto",
"//google/cloud:common_resources_proto",
],
)
Expand Down Expand Up @@ -71,10 +72,13 @@ java_gapic_library(
service_yaml = "//google/example/library:library_example_v1.yaml",
test_deps = [
":library_java_grpc",
"//google/cloud/location:location_java_grpc",
],
deps = [
":library_java_proto",
"//google/api:api_java_proto",
"//google/cloud/location:location_java_grpc",
"//google/cloud/location:location_java_proto",
],
)

Expand All @@ -94,6 +98,9 @@ java_gapic_assembly_gradle_pkg(
":library_java_grpc",
":library_java_proto",
":library_proto",
"//google/cloud/location:location_java_grpc",
"//google/cloud/location:location_java_proto",
"//google/cloud/location:location_proto",
],
)

Expand Down

0 comments on commit 15c1f88

Please sign in to comment.