Skip to content

Commit

Permalink
feat(bazel): support generation with x-api deps
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdietz committed Feb 24, 2023
1 parent de34d3c commit b9a62ad
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
Expand Up @@ -167,7 +167,9 @@ py_test(
"{{name}}_py_gapic_test.py",
],
legacy_create_init = False,
deps = [":{{name}}_py_gapic"],
deps = [
{{py_test_deps}}
],
)

# Open Source Packages
Expand Down
Expand Up @@ -22,10 +22,15 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class BazelBuildFileView {
private static final String COMMON_RESOURCES_PROTO = "//google/cloud:common_resources_proto";
private static final String API_PACKAGE_GROUP = "apiPackage";
private static final String API_VERSION_GROUP = "version";
private static final Pattern PACKAGE_AND_VERSION =
Pattern.compile("[a-z0-9/]+/(?<"+API_PACKAGE_GROUP+">[a-z0-9]+)/(?<"+API_VERSION_GROUP+">v[a-z0-9]+)[/:][_a-z0-9]+");
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 Down Expand Up @@ -83,6 +88,8 @@ class BazelBuildFileView {
actualImport = "//google/cloud/common:common_proto";
extraProtosNodeJS.add(actualImport);
} else {
// This is handles X-API depenedencies, converting the proto file-specific import
// into the package-level import label.
actualImport = convertPathToLabel("", actualImport);
}
actualImports.add(actualImport);
Expand Down Expand Up @@ -188,6 +195,7 @@ class BazelBuildFileView {
tokens.put("go_gapic_deps", joinSetWithIndentationNl(mapGoGapicDeps(actualImports)));

tokens.put("py_gapic_deps", joinSetWithIndentation(mapPyGapicDeps(actualImports)));
tokens.put("py_test_deps", joinSetWithIndentation(mapPyTestDeps(bp.getName(), actualImports)));

overriddenStringAttributes.putAll(bp.getOverriddenStringAttributes());
overriddenNonStringAttributes.putAll(bp.getOverriddenNonStringAttributes());
Expand Down Expand Up @@ -239,7 +247,7 @@ static String assembleGoImportPath(boolean isCloud, String protoPkg, String goPk
return goImport + goPkg;
}

private String convertPathToLabel(String pkg, String path) {
public static String convertPathToLabel(String pkg, String path) {
if (path == null) {
return path;
}
Expand Down Expand Up @@ -272,6 +280,15 @@ private String convertPathToLabel(String pkg, String path) {
}
int lastSlashIndex = sb.lastIndexOf("/");
sb.replace(lastSlashIndex, lastSlashIndex + 1, ":");

// Handle x-API dependencies by changing the per-proto-file dependency
// into a package-level dependency label.
int protoLabel = sb.indexOf("_proto");
Matcher m = PACKAGE_AND_VERSION.matcher(sb);
if (m.matches() && protoLabel != -1) {
String apiPackage = m.group(API_PACKAGE_GROUP);
sb.replace(lastSlashIndex+1, protoLabel, apiPackage);
}

return sb.toString();
}
Expand Down Expand Up @@ -342,6 +359,8 @@ private Set<String> mapJavaGapicDeps(Set<String> protoImports) {
javaImports.add("//google/cloud/location:location_java_proto");
} else if (protoImport.endsWith(":common_proto")) {
javaImports.add(replaceLabelName(protoImport, ":common_java_proto"));
} else if (protoImport.matches(PACKAGE_AND_VERSION.toString())) {
javaImports.add(protoImport.replaceAll("_proto", "_java_proto"));
}
}
return javaImports;
Expand Down Expand Up @@ -447,6 +466,8 @@ private Set<String> mapGoGapicDeps(Set<String> protoImports) {
goImports.add(replaceLabelName(protoImport, ":location_go_proto"));
} else if (protoImport.endsWith(":common_proto")) {
goImports.add(replaceLabelName(protoImport, ":common_go_proto"));
} else if (protoImport.matches(PACKAGE_AND_VERSION.toString())) {
goImports.add(protoImport.replaceAll("_proto", "_go_proto"));
}
}
return goImports;
Expand All @@ -464,6 +485,19 @@ private Set<String> mapPyGapicDeps(Set<String> protoImports) {
return pyImports;
}

private Set<String> mapPyTestDeps(String name, Set<String> protoImports) {
Set<String> pyImports = new TreeSet<>();
pyImports.add(":"+name+"_py_gapic");

// This is specifically for X-API depenedencies.
for (String protoImport : protoImports) {
if (protoImport.matches(PACKAGE_AND_VERSION.toString())) {
pyImports.add(protoImport.replaceAll("_proto", "_py_proto"));
}
}
return pyImports;
}

Map<String, String> getTokens() {
return Collections.unmodifiableMap(this.tokens);
}
Expand Down
Expand Up @@ -30,6 +30,7 @@ proto_library(
"//google/api:httpbody_proto",
"//google/api:resource_proto",
"//google/cloud/common:common_proto",
"//google/cloud/kms/v1:kms_proto",
"//google/longrunning:operations_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:empty_proto",
Expand Down Expand Up @@ -86,6 +87,7 @@ java_gapic_library(
":library_java_proto",
"//google/api:api_java_proto",
"//google/cloud/common:common_java_proto",
"//google/cloud/kms/v1:kms_java_proto",
"//google/cloud/location:location_java_proto",
"//google/iam/v1:iam_java_proto",
],
Expand Down Expand Up @@ -133,6 +135,7 @@ go_proto_library(
"//google/api:annotations_go_proto",
"//google/api:httpbody_go_proto",
"//google/cloud/common:common_go_proto",
"//google/cloud/kms/v1:kms_go_proto",
"//google/longrunning:longrunning_go_proto",
],
)
Expand All @@ -151,6 +154,7 @@ go_gapic_library(
":library_go_proto",
"//google/api:httpbody_go_proto",
"//google/cloud/common:common_go_proto",
"//google/cloud/kms/v1:kms_go_proto",
"//google/cloud/location:location_go_proto",
"//google/iam/v1:iam_go_proto",
"//google/longrunning:longrunning_go_proto",
Expand Down Expand Up @@ -206,7 +210,9 @@ py_test(
"library_py_gapic_test.py",
],
legacy_create_init = False,
deps = [":library_py_gapic"],
deps = [
":library_py_gapic",
],
)

# Open Source Packages
Expand Down
Expand Up @@ -30,6 +30,7 @@ proto_library(
"//google/api:httpbody_proto",
"//google/api:resource_proto",
"//google/cloud/common:common_proto",
"//google/cloud/kms/v1:kms_proto",
"//google/longrunning:operations_proto",
"@com_google_protobuf//:descriptor_proto",
"@com_google_protobuf//:empty_proto",
Expand Down Expand Up @@ -76,6 +77,7 @@ java_gapic_library(
":library_java_proto",
"//google/api:api_java_proto",
"//google/cloud/common:common_java_proto",
"//google/cloud/kms/v1:kms_java_proto",
"//google/cloud/location:location_java_proto",
"//google/iam/v1:iam_java_proto",
],
Expand Down Expand Up @@ -121,6 +123,7 @@ go_proto_library(
"//google/api:annotations_go_proto",
"//google/api:httpbody_go_proto",
"//google/cloud/common:common_go_proto",
"//google/cloud/kms/v1:kms_go_proto",
"//google/longrunning:longrunning_go_proto",
],
)
Expand All @@ -139,6 +142,7 @@ go_gapic_library(
":library_go_proto",
"//google/api:httpbody_go_proto",
"//google/cloud/common:common_go_proto",
"//google/cloud/kms/v1:kms_go_proto",
"//google/cloud/location:location_go_proto",
"//google/iam/v1:iam_go_proto",
"//google/longrunning:longrunning_go_proto",
Expand Down Expand Up @@ -194,7 +198,10 @@ py_test(
"library_py_gapic_test.py",
],
legacy_create_init = False,
deps = [":library_py_gapic"],
deps = [
":library_py_gapic",
"//google/cloud/kms/v1:kms_py_proto",
],
)

# Open Source Packages
Expand Down
Expand Up @@ -27,6 +27,7 @@ import "google/api/httpbody.proto";
import "google/cloud/common/operation_metadata.proto";
import "google/longrunning/operations.proto";
import "google/protobuf/empty.proto";
import "google/cloud/kms/v1/resources.proto";

option go_package = "google.golang.org/genproto/googleapis/example/library/v1;library";
option java_multiple_files = true;
Expand Down
Expand Up @@ -196,7 +196,10 @@ py_test(
"library_py_gapic_test.py",
],
legacy_create_init = False,
deps = [":library_py_gapic"],
deps = [
":library_py_gapic",
"//google/cloud/kms/v1:kms_py_proto",
],
)

# Open Source Packages
Expand Down
Expand Up @@ -15,6 +15,9 @@
package com.google.api.codegen.bazel;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -33,4 +36,23 @@ public void testAssembleGoImportPath() throws IOException {
actual = BazelBuildFileView.assembleGoImportPath(true, "google.cloud.foo.v1", "cloud.google.com/go/foo/apiv1/foopb;foopb");
Assert.assertEquals("cloud.google.com/go/foo/apiv1;foo", actual);
}

@Test
public void testConvertPathToLabel() {
// Proto imports aren't actually evaluated with a package while converting them.
String emptyPkg = "";
List<List<String>> tests = Arrays.asList(
Arrays.asList(emptyPkg, "google/type/interval_proto", "//google/type:interval_proto"),
// X-API dependency.
Arrays.asList(emptyPkg, "google/cloud/foo/v1/bar_proto", "//google/cloud/foo/v1:foo_proto")
);
for (List<String> testCase : tests) {
String pkg = testCase.get(0);
String path = testCase.get(1);
String want = testCase.get(2);

String got = BazelBuildFileView.convertPathToLabel(pkg, path);
Assert.assertEquals(want, got);
}
}
}

0 comments on commit b9a62ad

Please sign in to comment.