Skip to content

Commit

Permalink
feat: Make transport = "grpc+rest" default for java and golang (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
vam-google committed Jun 17, 2022
1 parent 055db84 commit 993109d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void injectFieldsFromTopLevel() {

boolean topLevelCloudScope = parent.getCloudScopes().getOrDefault(version, false);
cloudScope = topLevelCloudScope ? topLevelCloudScope : cloudScope;

boolean topLevelContainsLocations = parent.getContainsLocations().getOrDefault(version, false);
containsLocations = topLevelContainsLocations ? topLevelContainsLocations : containsLocations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface FileWriter {
private final Path srcDir;
private final Path destDir;
private final boolean overwrite;
private final String transport;
private boolean writerMode;
private final FileWriter fileWriter;

Expand All @@ -50,13 +51,15 @@ interface FileWriter {
String rootApiTempl,
String rawApiTempl,
boolean overwrite,
String transport,
FileWriter fileWriter) {
this.gapicApiTempl = new BazelBuildFileTemplate(gapicApiTempl);
this.rootApiTempl = new BazelBuildFileTemplate(rootApiTempl);
this.rawApiTempl = new BazelBuildFileTemplate(rawApiTempl);
this.srcDir = srcDir.normalize();
this.destDir = destDir.normalize();
this.overwrite = overwrite;
this.transport = transport;
this.writerMode = false;
this.fileWriter =
(fileWriter != null)
Expand Down Expand Up @@ -165,7 +168,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce

if (!outDir.exists()) {
if (!outDir.mkdirs()) {
System.out.println("WARNING: Could not create directory: " + outDir.toString());
System.out.println("WARNING: Could not create directory: " + outDir);
return FileVisitResult.CONTINUE;
}
}
Expand All @@ -181,7 +184,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce
System.out.println(
"Write File [" + tmplType + "]: " + outDir.toString() + File.separator + "BUILD.bazel");
try {
BazelBuildFileView bpv = new BazelBuildFileView(bp);
BazelBuildFileView bpv = new BazelBuildFileView(bp, transport);
fileWriter.write(outFilePath, template.expand(bpv));
} catch (RuntimeException ex) {
ex.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ static void printUsage() {
+ "Command line options:\n"
+ " --src=path: location of googleapis directory\n"
+ " --dest=path: destination folder, defaults to the value of --src\n"
+ " --overwrite: do not preserve any of the manually changed values in the generated BUILD.bazel files\n";
+ " --overwrite: do not preserve any of the manually changed values in the generated"
+ " BUILD.bazel files\n";
System.out.println(helpMessage);
}

Expand All @@ -79,6 +80,10 @@ ApisVisitor createApisVisitor(ApisVisitor.FileWriter fileWriter, String relative
if (overwrite == null) {
overwrite = "false";
}
String transport = parsedArgs.get("--transport");
if (transport == null) {
transport = "grpc+rest";
}

Path srcPath = Paths.get(parsedArgs.get("--src")).normalize();
Path destPath = srcPath;
Expand Down Expand Up @@ -117,6 +122,7 @@ ApisVisitor createApisVisitor(ApisVisitor.FileWriter fileWriter, String relative
? readResource("BUILD.bazel.raw_api.mustache")
: ApisVisitor.readFile(rawApiTempl),
overwrite.equals("true"),
transport,
fileWriter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ java_gapic_library(
test_deps = [
":{{name}}_java_grpc",{{java_gapic_test_deps}}
],
transport = {{transport}},
deps = [
":{{name}}_java_proto",{{java_gapic_deps}}
],
Expand All @@ -84,6 +85,7 @@ java_gapic_test(
# Open Source Packages
java_gapic_assembly_gradle_pkg(
name = "google-cloud-{{assembly_name}}-{{version}}-java",
transport = {{transport}},
deps = [
":{{name}}_java_gapic",
":{{name}}_java_grpc",
Expand Down Expand Up @@ -119,8 +121,9 @@ go_gapic_library(
srcs = [":{{name}}_proto_with_info"],
grpc_service_config = {{grpc_service_config}},
importpath = "{{go_gapic_importpath}}",
service_yaml = {{service_yaml}},
metadata = True,
service_yaml = {{service_yaml}},
transport = {{transport}},
deps = [
":{{name}}_go_proto",{{go_gapic_deps}}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BazelBuildFileView {
private final Map<String, Map<String, List<String>>> overriddenListAttributes = new HashMap<>();
private final Map<String, String> assemblyPkgRulesNames = new HashMap<>();

BazelBuildFileView(ApiVersionedDir bp) {
BazelBuildFileView(ApiVersionedDir bp, String transport) {
if (bp.getProtoPackage() == null) {
return;
}
Expand All @@ -54,7 +54,8 @@ class BazelBuildFileView {
extraImports.add("//google/iam/v1:iam_policy_proto");
}
tokens.put("extra_imports", joinSetWithIndentation(extraImports));
// Remove common_resources.proto because it is only needed for the proto_library_with_info target.
// Remove common_resources.proto because it is only needed for the proto_library_with_info
// target.
extraImports.remove(COMMON_RESOURCES_PROTO);

String packPrefix = bp.getProtoPackage().replace(".", "/") + '/';
Expand Down Expand Up @@ -100,8 +101,9 @@ class BazelBuildFileView {

String serviceYaml = "None";
if (bp.getServiceYamlPath() != null) {
// Wrap the label in quotations, because None doesn't need them, so they can't be in the template.
serviceYaml = "\""+convertPathToLabel(bp.getProtoPackage(), bp.getServiceYamlPath())+"\"";
// Wrap the label in quotations, because None doesn't need them, so they can't be in the
// template.
serviceYaml = "\"" + convertPathToLabel(bp.getProtoPackage(), bp.getServiceYamlPath()) + "\"";
}
tokens.put("service_yaml", serviceYaml);

Expand All @@ -110,7 +112,7 @@ class BazelBuildFileView {
String gapicYaml = "None";
String gapicYamlPath = bp.getGapicYamlPath();
if (gapicYamlPath != null && !gapicYamlPath.isEmpty()) {
gapicYaml = "\""+convertPathToLabel(bp.getProtoPackage(), gapicYamlPath)+"\"";
gapicYaml = "\"" + convertPathToLabel(bp.getProtoPackage(), gapicYamlPath) + "\"";
}
tokens.put("gapic_yaml", gapicYaml);

Expand All @@ -135,6 +137,7 @@ class BazelBuildFileView {
// Default service name as it appears in the proto.
: service;
javaTests.add(javaPackage + "." + actualService + "ClientTest");
javaTests.add(javaPackage + "." + actualService + "ClientHttpJsonTest");
}

actualImports.addAll(extraImports);
Expand All @@ -158,6 +161,8 @@ class BazelBuildFileView {
overriddenStringAttributes.putAll(bp.getOverriddenStringAttributes());
overriddenListAttributes.putAll(bp.getOverriddenListAttributes());
assemblyPkgRulesNames.putAll(bp.getAssemblyPkgRulesNames());

tokens.put("transport", '"' + transport + '"');
}

private String assembleGoImportPath(boolean isCloud, String protoPkg, String goPkg) {
Expand Down
14 changes: 3 additions & 11 deletions bazel/src/main/java/com/google/api/codegen/bazel/Buildozer.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,13 @@ public void batchSetAttribute(Path bazelBuildFile, String target, String attribu
batch.add(
String.format(
"set %s \"%s\"|%s:%s",
attribute,
value.replace(" ", "\\ "),
bazelBuildFile.toString(), target));
attribute, value.replace(" ", "\\ "), bazelBuildFile.toString(), target));
}

// Remove the given attribute of the given target. Apply changes immediately.
public void batchRemoveAttribute(Path bazelBuildFile, String target, String attribute)
throws IOException {
batch.add(
String.format("remove %s|%s:%s",
attribute,
bazelBuildFile.toString(),
target));
batch.add(String.format("remove %s|%s:%s", attribute, bazelBuildFile.toString(), target));
}

// Add the value to the given list attribute of the given target.
Expand All @@ -121,9 +115,7 @@ public void batchAddAttribute(Path bazelBuildFile, String target, String attribu
batch.add(
String.format(
"add %s \"%s\"|%s:%s",
attribute,
value.replace(" ", "\\ "),
bazelBuildFile.toString(), target));
attribute, value.replace(" ", "\\ "), bazelBuildFile.toString(), target));
}

// Make all changes that are waiting in the batch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ java_gapic_library(
"//google/cloud/location:location_java_grpc",
"//google/iam/v1:iam_java_grpc",
],
transport = "grpc+rest",
deps = [
":library_java_proto",
"//google/api:api_java_proto",
Expand All @@ -91,6 +92,7 @@ java_gapic_library(
java_gapic_test(
name = "library_java_gapic_test_suite",
test_classes = [
"com.google.example.library.v1.LibraryServiceClientHttpJsonTest",
"com.google.example.library.v1.LibraryServiceClientTest",
],
runtime_deps = [":library_java_gapic_test"],
Expand All @@ -99,6 +101,7 @@ java_gapic_test(
# Open Source Packages
java_gapic_assembly_gradle_pkg(
name = "google-cloud-example-library-v1-java",
transport = "grpc+rest",
deps = [
":library_java_gapic",
":library_java_grpc",
Expand Down Expand Up @@ -137,8 +140,9 @@ go_gapic_library(
srcs = [":library_proto_with_info"],
grpc_service_config = "library_example_grpc_service_config.json",
importpath = "cloud.google.com/go/example/library/apiv1;library",
service_yaml = "library_example_v1.yaml",
metadata = True,
service_yaml = "library_example_v1.yaml",
transport = "grpc+rest",
deps = [
":library_go_proto",
"//google/api:httpbody_go_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ java_gapic_library(
"//google/cloud/location:location_java_grpc",
"//google/iam/v1:iam_java_grpc",
],
transport = "grpc+rest",
deps = [
":library_java_proto",
"//google/api:api_java_proto",
Expand All @@ -88,6 +89,7 @@ java_gapic_library(
java_gapic_test(
name = "library_java_gapic_test_suite",
test_classes = [
"com.google.cloud.example.library.v1.LibraryClientHttpJsonTest",
"com.google.cloud.example.library.v1.LibraryClientTest",
],
runtime_deps = [":library_java_gapic_test"],
Expand All @@ -96,6 +98,7 @@ java_gapic_test(
# Open Source Packages
java_gapic_assembly_gradle_pkg(
name = "google-cloud-example-library-v1-java",
transport = "grpc+rest",
deps = [
":library_java_gapic",
":library_java_grpc",
Expand Down Expand Up @@ -132,8 +135,9 @@ go_gapic_library(
srcs = [":library_proto_with_info"],
grpc_service_config = "library_example_grpc_service_config.json",
importpath = "cloud.google.com/go/example/library/apiv1;library",
service_yaml = "//google/example/library:library_example_v1.yaml",
metadata = True,
service_yaml = "//google/example/library:library_example_v1.yaml",
transport = "grpc+rest",
deps = [
":library_go_proto",
"//google/api:httpbody_go_proto",
Expand Down

0 comments on commit 993109d

Please sign in to comment.