Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bazel): support generation with x-api deps #138

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -68,6 +73,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 @@ -199,6 +206,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 @@ -250,7 +258,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 @@ -283,6 +291,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 @@ -353,6 +370,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 @@ -458,6 +477,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 @@ -475,6 +496,20 @@ 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.
// Ignore IAM deps, unnecessary for tests.
for (String protoImport : protoImports) {
if (protoImport.matches(PACKAGE_AND_VERSION.toString()) && !protoImport.contains("iam/v1")) {
pyImports.add(protoImport.replaceAll("_proto", "_py_proto"));
}
}
return pyImports;
}

// Simply take the last two segments of the package to use as the assembly name.
public static String typeOnlyAssemblyName(String pkg) {
String[] parts = pkg.split("\\.");
Expand Down
Original file line number Diff line number Diff line change
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,10 @@ py_test(
"library_py_gapic_test.py",
],
legacy_create_init = False,
deps = [":library_py_gapic"],
deps = [
"//google/cloud/kms/v1:kms_py_proto",
":library_py_gapic",
],
)

# Open Source Packages
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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
Original file line number Diff line number Diff line change
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;
import java.util.Arrays;
Expand All @@ -37,6 +40,25 @@ public void testAssembleGoImportPath() throws IOException {
}

@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);
}
}

@Test
public void testTypeOnlyAssemblyName() {
List<List<String>> tests = Arrays.asList(
Arrays.asList("type", "type"),
Expand Down