Skip to content

Commit

Permalink
[ggj][engx] feat: allow example/library to lack the grpc_config file (#…
Browse files Browse the repository at this point in the history
…566)

* fix: handle wildcard-typed resrefs when parsing methods

* fix: support older type-only resrefs w/o a service prefix

* feat: allow example/library to lack the grpc_config file
  • Loading branch information
miraleung authored Nov 25, 2020
1 parent 0daff2f commit ade36dc
Show file tree
Hide file tree
Showing 17 changed files with 4,299 additions and 6 deletions.
5 changes: 4 additions & 1 deletion rules_java_gapic/java_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
load("@com_google_api_codegen//rules_gapic:gapic.bzl", "proto_custom_library", "unzipped_srcjar")

SERVICE_YAML_ALLOWLIST = ["googleads"]
NO_GRPC_CONFIG_ALLOWLIST = ["library"]

def _java_gapic_postprocess_srcjar_impl(ctx):
gapic_srcjar = ctx.file.gapic_srcjar
Expand Down Expand Up @@ -113,7 +114,9 @@ def java_gapic_library(
if grpc_service_config:
file_args_dict[grpc_service_config] = "grpc-service-config"
else:
fail("Missing a gRPC service config file")
for keyword in NO_GRPC_CONFIG_ALLOWLIST:
if keyword not in name:
fail("Missing a gRPC service config file")

if gapic_yaml:
file_args_dict[gapic_yaml] = "gapic-config"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ private GapicServiceConfig(
this.methodConfigTable = methodConfigTable;
}

public static GapicServiceConfig create(ServiceConfig serviceConfig) {
public static GapicServiceConfig create(Optional<ServiceConfig> serviceConfigOpt) {
if (!serviceConfigOpt.isPresent()) {
return new GapicServiceConfig(Collections.emptyList(), Collections.emptyMap());
}

ServiceConfig serviceConfig = serviceConfigOpt.get();
Map<MethodConfig.Name, Integer> methodConfigTable = new HashMap<>();
List<MethodConfig> methodConfigs = serviceConfig.getMethodConfigList();
for (int i = 0; i < methodConfigs.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
public class ServiceConfigParser {
public static Optional<GapicServiceConfig> parse(String serviceConfigFilePath) {
Optional<ServiceConfig> rawConfigOpt = parseFile(serviceConfigFilePath);
if (!rawConfigOpt.isPresent()) {
return Optional.empty();
}
return Optional.of(GapicServiceConfig.create(rawConfigOpt.get()));
return Optional.of(GapicServiceConfig.create(rawConfigOpt));
}

@VisibleForTesting
Expand Down
35 changes: 35 additions & 0 deletions test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ INTEGRATION_TEST_LIBRARIES = [
"asset",
"logging",
"redis",
"library", # No gRPC service config.
]

[integration_test(
Expand Down Expand Up @@ -147,3 +148,37 @@ java_gapic_assembly_gradle_pkg(
"@com_google_googleapis//google/logging/v2:logging_proto",
],
)

# example/library API.
# Tests the edge case of a legitimately missing gRPC service config file.
java_gapic_library(
name = "library_java_gapic",
srcs = ["@com_google_googleapis//google/example/library/v1:library_proto_with_info"],
gapic_yaml = "@com_google_googleapis//google/example/library/v1:library_example_gapic.yaml",
package = "google.example.library.v1",
test_deps = [
"@com_google_googleapis//google/example/library/v1:library_java_grpc",
],
deps = [
"@com_google_googleapis//google/example/library/v1:library_java_proto",
],
)

java_gapic_test(
name = "library_java_gapic_test_suite",
test_classes = [
"com.google.cloud.example.library.v1.LibraryServiceClientTest",
],
runtime_deps = [":library_java_gapic_test"],
)

# Open Source Packages
java_gapic_assembly_gradle_pkg(
name = "google-cloud-example-library-v1-java",
deps = [
":library_java_gapic",
"@com_google_googleapis//google/example/library/v1:library_java_grpc",
"@com_google_googleapis//google/example/library/v1:library_java_proto",
"@com_google_googleapis//google/example/library/v1:library_proto",
],
)
6 changes: 6 additions & 0 deletions test/integration/goldens/library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "goldens_files",
srcs = glob(["*.java"]),
)
191 changes: 191 additions & 0 deletions test/integration/goldens/library/BookName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.example.library.v1;

import com.google.api.pathtemplate.PathTemplate;
import com.google.api.resourcenames.ResourceName;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Generated;

// AUTO-GENERATED DOCUMENTATION AND CLASS.
@Generated("by gapic-generator-java")
public class BookName implements ResourceName {
private static final PathTemplate SHELF_ID_BOOK_ID =
PathTemplate.createWithoutUrlEncoding("shelves/{shelf_id}/books/{book_id}");
private volatile Map<String, String> fieldValuesMap;
private final String shelfId;
private final String bookId;

@Deprecated
protected BookName() {
shelfId = null;
bookId = null;
}

private BookName(Builder builder) {
shelfId = Preconditions.checkNotNull(builder.getShelfId());
bookId = Preconditions.checkNotNull(builder.getBookId());
}

public String getShelfId() {
return shelfId;
}

public String getBookId() {
return bookId;
}

public static Builder newBuilder() {
return new Builder();
}

public Builder toBuilder() {
return new Builder(this);
}

public static BookName of(String shelfId, String bookId) {
return newBuilder().setShelfId(shelfId).setBookId(bookId).build();
}

public static String format(String shelfId, String bookId) {
return newBuilder().setShelfId(shelfId).setBookId(bookId).build().toString();
}

public static BookName parse(String formattedString) {
if (formattedString.isEmpty()) {
return null;
}
Map<String, String> matchMap =
SHELF_ID_BOOK_ID.validatedMatch(
formattedString, "BookName.parse: formattedString not in valid format");
return of(matchMap.get("shelf_id"), matchMap.get("book_id"));
}

public static List<BookName> parseList(List<String> formattedStrings) {
List<BookName> list = new ArrayList<>(formattedStrings.size());
for (String formattedString : formattedStrings) {
list.add(parse(formattedString));
}
return list;
}

public static List<String> toStringList(List<BookName> values) {
List<String> list = new ArrayList<>(values.size());
for (BookName value : values) {
if (Objects.isNull(value)) {
list.add("");
} else {
list.add(value.toString());
}
}
return list;
}

public static boolean isParsableFrom(String formattedString) {
return SHELF_ID_BOOK_ID.matches(formattedString);
}

@Override
public Map<String, String> getFieldValuesMap() {
if (Objects.isNull(fieldValuesMap)) {
synchronized (this) {
if (Objects.isNull(fieldValuesMap)) {
ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
if (!Objects.isNull(shelfId)) {
fieldMapBuilder.put("shelf_id", shelfId);
}
if (!Objects.isNull(bookId)) {
fieldMapBuilder.put("book_id", bookId);
}
fieldValuesMap = fieldMapBuilder.build();
}
}
}
return fieldValuesMap;
}

public String getFieldValue(String fieldName) {
return getFieldValuesMap().get(fieldName);
}

@Override
public String toString() {
return SHELF_ID_BOOK_ID.instantiate("shelf_id", shelfId, "book_id", bookId);
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o != null || getClass() == o.getClass()) {
BookName that = ((BookName) o);
return Objects.equals(this.shelfId, that.shelfId) && Objects.equals(this.bookId, that.bookId);
}
return false;
}

@Override
public int hashCode() {
int h = 1;
h *= 1000003;
h ^= Objects.hashCode(shelfId);
h *= 1000003;
h ^= Objects.hashCode(bookId);
return h;
}

/** Builder for shelves/{shelf_id}/books/{book_id}. */
public static class Builder {
private String shelfId;
private String bookId;

protected Builder() {}

public String getShelfId() {
return shelfId;
}

public String getBookId() {
return bookId;
}

public Builder setShelfId(String shelfId) {
this.shelfId = shelfId;
return this;
}

public Builder setBookId(String bookId) {
this.bookId = bookId;
return this;
}

private Builder(BookName bookName) {
shelfId = bookName.shelfId;
bookId = bookName.bookId;
}

public BookName build() {
return new BookName(this);
}
}
}
Loading

0 comments on commit ade36dc

Please sign in to comment.