Skip to content

Commit

Permalink
fix(build_gen): support proto java_package, make gapic.yaml dep optio…
Browse files Browse the repository at this point in the history
…nal, add tests [rules_gapic] (#26)

* fix(build_gen): support proto java_package, make gapic.yaml dep optional, add tests

* fix: tests

* fix: concat printfs

Co-authored-by: Alexander Fenster <fenster@google.com>
  • Loading branch information
miraleung and alexander-fenster committed Feb 19, 2021
1 parent 1ea56a4 commit dc4dd84
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 86 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ jobs:

- name: Run generated tests
run: bazel test '//bazel:*'

- name: Show test output
run: cat bazel-out/*/testlogs/bazel/build_file_generator_test/test.log
if: ${{ failure() }}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class ApiVersionedDir {
Pattern.compile("(?m)^package\\s+(?<protoPackage>[\\w+\\.]+)\\s*;\\s*$");
private static final Pattern IMPORTS =
Pattern.compile("(?m)^import\\s+\"(?<import>[\\w+\\\\./]+)\"\\s*;\\s*$");
private static final Pattern PROTO_OPTIONS =
// A proto's language package options.
private static final Pattern PROTO_LANG_PACKAGE =
Pattern.compile(
"(?m)^option\\s+(?<optName>\\w+)\\s+=\\s+\"(?<optValue>[\\w./;\\\\\\-]+)\"\\s*;\\s*$");
"(?m)^option\\s+(?<optName>(java|go|csharp|ruby|php|javascript)_(namespace|package))\\s+=\\s+\"(?<optValue>[\\w./;\\\\\\-]+)\"\\s*;\\s*$");
private static final Pattern SERVICE =
Pattern.compile("(?m)^service\\s+(?<service>\\w+)\\s+(\\{)*\\s*$");
private static final Pattern LANG_PACKAGES =
Expand All @@ -60,9 +61,13 @@ class ApiVersionedDir {

private static final String[] PRESERVED_PROTO_LIBRARY_STRING_ATTRIBUTES = {
// TypeScript:
"package_name", "main_service", "bundle_config", "iam_service",
"package_name",
"main_service",
"bundle_config",
"iam_service",
// Ruby:
"ruby_cloud_title", "ruby_cloud_description",
"ruby_cloud_title",
"ruby_cloud_description",
// Other languages: add below
};

Expand Down Expand Up @@ -316,7 +321,7 @@ void parseProtoFile(String fileName, String fileBody) {
}

// Parse file-level options
m = PROTO_OPTIONS.matcher(fileBody);
m = PROTO_LANG_PACKAGE.matcher(fileBody);
while (m.find()) {
String optName = m.group("optName");
String optValue = m.group("optValue");
Expand Down Expand Up @@ -359,11 +364,12 @@ void parseBazelBuildFile(Path file) {
if (kind.contains("_gapic_assembly_")) {
if (this.assemblyPkgRulesNames.containsKey(kind)) {
// Duplicated rule of the same kind will break our logic for preserving rule name.
System.err.println("There are more than one rule of kind " + kind + ".");
System.err.println(
"Bazel build file generator does not support regenerating BUILD.bazel in this case.");
System.err.println(
"Please run it with --overwrite option to overwrite the existing BUILD.bazel completely.");
String.format(
"There is more than one rule of kind %s. Bazel build file generator does not"
+ " support regenerating BUILD.bazel in this case. Please run it with"
+ " --overwrite option to overwrite the existing BUILD.bazel completely.",
kind));
throw new RuntimeException("Duplicated rule " + kind);
}
this.assemblyPkgRulesNames.put(kind, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ ruby_grpc_library(

ruby_cloud_gapic_library(
name = "{{name}}_ruby_gapic",
srcs = [":{{name}}_proto_with_info",],
srcs = [":{{name}}_proto_with_info"],
extra_protoc_parameters = [
"ruby-cloud-gem-name=google-cloud-{{assembly_name}}-{{version}}",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,24 @@ class BazelBuildFileView {

Set<String> javaTests = new TreeSet<>();
for (String service : bp.getServices()) {
String javaPackage = bp.getLangGapicPackages().get("java");
// Prioritize the language override in gapic.yaml if it is present.
// New APIs (circa 2020) should rely on the protobuf options instead.
String javaPackage =
bp.getLangGapicPackages().containsKey("java")
? bp.getLangGapicPackages().get("java")
: bp.getLangProtoPackages().get("java");
if (javaPackage == null) {
continue;
}

String actualService =
bp.getLangGapicNameOverrides()
.get("java")
.getOrDefault(bp.getProtoPackage() + "." + service, service);
bp.getLangGapicNameOverrides().containsKey("java")
// The service name is overridden in gapic.yaml.
? bp.getLangGapicNameOverrides()
.get("java")
.getOrDefault(bp.getProtoPackage() + "." + service, service)
// Default service name as it appears in the proto.
: service;
if (actualService.startsWith("IAM")) {
actualService = actualService.replaceAll("^IAM", "Iam");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ java_gapic_library(
java_gapic_test(
name = "library_java_gapic_test_suite",
test_classes = [
"com.google.cloud.example.library.v1.LibraryServiceClientTest",
"com.google.example.library.v1.LibraryServiceClientTest",
],
runtime_deps = [":library_java_gapic_test"],
)
Expand Down Expand Up @@ -269,7 +269,7 @@ ruby_grpc_library(

ruby_cloud_gapic_library(
name = "library_ruby_gapic",
srcs = [":library_proto_with_info",],
srcs = [":library_proto_with_info"],
extra_protoc_parameters = [
"ruby-cloud-gem-name=google-cloud-example-library-v1",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ message Shelf {
// The name is ignored when creating a shelf.
string name = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference).type = "library-example.googleapis.com/Shelf"
(google.api.resource_reference).type =
"library-example.googleapis.com/Shelf"
];

// The theme of the shelf
Expand All @@ -72,4 +73,3 @@ message CreateShelfRequest {
// The shelf to create.
Shelf shelf = 1 [(google.api.field_behavior) = REQUIRED];
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,5 @@
# limitations under the License.

type: com.google.api.codegen.ConfigProto
config_schema_version: 1.0.0
# The settings of generated code in a specific language.
language_settings:
java:
package_name: com.google.cloud.example.library.v1
python:
package_name: google.cloud.example.library_v1.gapic
go:
package_name: cloud.google.com/go/example/library/apiv1
csharp:
package_name: Google.Example.Library.V1
ruby:
package_name: Google::Cloud::Example::Library::V1
php:
package_name: Google\Cloud\Example\Library\V1
nodejs:
package_name: library.v1
# A list of API interface configurations.
interfaces:
# The fully qualified name of the API interface.
- name: google.example.library.v1.LibraryService
# A list of resource collection configurations.
# Consists of a name_pattern and an entity_name.
# The name_pattern is a pattern to describe the names of the resources of this
# collection, using the platform's conventions for URI patterns. A generator
# may use this to generate methods to compose and decompose such names. The
# pattern should use named placeholders as in `shelves/{shelf}/books/{book}`;
# those will be taken as hints for the parameter names of the generated
# methods. If empty, no name methods are generated.
# The entity_name is the name to be used as a basis for generated methods and
# classes.
collections:
- name_pattern: shelves/{shelf}
entity_name: shelf
- name_pattern: shelves/{shelf}/books/{book}
entity_name: book
# Definition for retryable codes.
retry_codes_def:
- name: idempotent
retry_codes:
- DEADLINE_EXCEEDED
- UNAVAILABLE
- name: non_idempotent
retry_codes: []
# Definition for retry/backoff parameters.
retry_params_def:
- name: default
initial_retry_delay_millis: 100
retry_delay_multiplier: 1.3
max_retry_delay_millis: 60000
initial_rpc_timeout_millis: 20000
rpc_timeout_multiplier: 1
max_rpc_timeout_millis: 20000
total_timeout_millis: 600000
methods:
- name: CreateShelf
flattening:
groups:
- parameters:
- shelf
required_fields:
- shelf
retry_codes_name: non_idempotent
retry_params_name: default
timeout_millis: 10000
config_schema_version: 2.0.0

0 comments on commit dc4dd84

Please sign in to comment.