Skip to content

Commit ade36dc

Browse files
authored
[ggj][engx] feat: allow example/library to lack the grpc_config file (#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
1 parent 0daff2f commit ade36dc

17 files changed

+4299
-6
lines changed

rules_java_gapic/java_gapic.bzl

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
load("@com_google_api_codegen//rules_gapic:gapic.bzl", "proto_custom_library", "unzipped_srcjar")
1616

1717
SERVICE_YAML_ALLOWLIST = ["googleads"]
18+
NO_GRPC_CONFIG_ALLOWLIST = ["library"]
1819

1920
def _java_gapic_postprocess_srcjar_impl(ctx):
2021
gapic_srcjar = ctx.file.gapic_srcjar
@@ -113,7 +114,9 @@ def java_gapic_library(
113114
if grpc_service_config:
114115
file_args_dict[grpc_service_config] = "grpc-service-config"
115116
else:
116-
fail("Missing a gRPC service config file")
117+
for keyword in NO_GRPC_CONFIG_ALLOWLIST:
118+
if keyword not in name:
119+
fail("Missing a gRPC service config file")
117120

118121
if gapic_yaml:
119122
file_args_dict[gapic_yaml] = "gapic-config"

src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ private GapicServiceConfig(
5555
this.methodConfigTable = methodConfigTable;
5656
}
5757

58-
public static GapicServiceConfig create(ServiceConfig serviceConfig) {
58+
public static GapicServiceConfig create(Optional<ServiceConfig> serviceConfigOpt) {
59+
if (!serviceConfigOpt.isPresent()) {
60+
return new GapicServiceConfig(Collections.emptyList(), Collections.emptyMap());
61+
}
62+
63+
ServiceConfig serviceConfig = serviceConfigOpt.get();
5964
Map<MethodConfig.Name, Integer> methodConfigTable = new HashMap<>();
6065
List<MethodConfig> methodConfigs = serviceConfig.getMethodConfigList();
6166
for (int i = 0; i < methodConfigs.size(); i++) {

src/main/java/com/google/api/generator/gapic/protoparser/ServiceConfigParser.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
public class ServiceConfigParser {
2828
public static Optional<GapicServiceConfig> parse(String serviceConfigFilePath) {
2929
Optional<ServiceConfig> rawConfigOpt = parseFile(serviceConfigFilePath);
30-
if (!rawConfigOpt.isPresent()) {
31-
return Optional.empty();
32-
}
33-
return Optional.of(GapicServiceConfig.create(rawConfigOpt.get()));
30+
return Optional.of(GapicServiceConfig.create(rawConfigOpt));
3431
}
3532

3633
@VisibleForTesting

test/integration/BUILD.bazel

+35
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ INTEGRATION_TEST_LIBRARIES = [
2020
"asset",
2121
"logging",
2222
"redis",
23+
"library", # No gRPC service config.
2324
]
2425

2526
[integration_test(
@@ -147,3 +148,37 @@ java_gapic_assembly_gradle_pkg(
147148
"@com_google_googleapis//google/logging/v2:logging_proto",
148149
],
149150
)
151+
152+
# example/library API.
153+
# Tests the edge case of a legitimately missing gRPC service config file.
154+
java_gapic_library(
155+
name = "library_java_gapic",
156+
srcs = ["@com_google_googleapis//google/example/library/v1:library_proto_with_info"],
157+
gapic_yaml = "@com_google_googleapis//google/example/library/v1:library_example_gapic.yaml",
158+
package = "google.example.library.v1",
159+
test_deps = [
160+
"@com_google_googleapis//google/example/library/v1:library_java_grpc",
161+
],
162+
deps = [
163+
"@com_google_googleapis//google/example/library/v1:library_java_proto",
164+
],
165+
)
166+
167+
java_gapic_test(
168+
name = "library_java_gapic_test_suite",
169+
test_classes = [
170+
"com.google.cloud.example.library.v1.LibraryServiceClientTest",
171+
],
172+
runtime_deps = [":library_java_gapic_test"],
173+
)
174+
175+
# Open Source Packages
176+
java_gapic_assembly_gradle_pkg(
177+
name = "google-cloud-example-library-v1-java",
178+
deps = [
179+
":library_java_gapic",
180+
"@com_google_googleapis//google/example/library/v1:library_java_grpc",
181+
"@com_google_googleapis//google/example/library/v1:library_java_proto",
182+
"@com_google_googleapis//google/example/library/v1:library_proto",
183+
],
184+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
filegroup(
4+
name = "goldens_files",
5+
srcs = glob(["*.java"]),
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.example.library.v1;
18+
19+
import com.google.api.pathtemplate.PathTemplate;
20+
import com.google.api.resourcenames.ResourceName;
21+
import com.google.common.base.Preconditions;
22+
import com.google.common.collect.ImmutableMap;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.Objects;
27+
import javax.annotation.Generated;
28+
29+
// AUTO-GENERATED DOCUMENTATION AND CLASS.
30+
@Generated("by gapic-generator-java")
31+
public class BookName implements ResourceName {
32+
private static final PathTemplate SHELF_ID_BOOK_ID =
33+
PathTemplate.createWithoutUrlEncoding("shelves/{shelf_id}/books/{book_id}");
34+
private volatile Map<String, String> fieldValuesMap;
35+
private final String shelfId;
36+
private final String bookId;
37+
38+
@Deprecated
39+
protected BookName() {
40+
shelfId = null;
41+
bookId = null;
42+
}
43+
44+
private BookName(Builder builder) {
45+
shelfId = Preconditions.checkNotNull(builder.getShelfId());
46+
bookId = Preconditions.checkNotNull(builder.getBookId());
47+
}
48+
49+
public String getShelfId() {
50+
return shelfId;
51+
}
52+
53+
public String getBookId() {
54+
return bookId;
55+
}
56+
57+
public static Builder newBuilder() {
58+
return new Builder();
59+
}
60+
61+
public Builder toBuilder() {
62+
return new Builder(this);
63+
}
64+
65+
public static BookName of(String shelfId, String bookId) {
66+
return newBuilder().setShelfId(shelfId).setBookId(bookId).build();
67+
}
68+
69+
public static String format(String shelfId, String bookId) {
70+
return newBuilder().setShelfId(shelfId).setBookId(bookId).build().toString();
71+
}
72+
73+
public static BookName parse(String formattedString) {
74+
if (formattedString.isEmpty()) {
75+
return null;
76+
}
77+
Map<String, String> matchMap =
78+
SHELF_ID_BOOK_ID.validatedMatch(
79+
formattedString, "BookName.parse: formattedString not in valid format");
80+
return of(matchMap.get("shelf_id"), matchMap.get("book_id"));
81+
}
82+
83+
public static List<BookName> parseList(List<String> formattedStrings) {
84+
List<BookName> list = new ArrayList<>(formattedStrings.size());
85+
for (String formattedString : formattedStrings) {
86+
list.add(parse(formattedString));
87+
}
88+
return list;
89+
}
90+
91+
public static List<String> toStringList(List<BookName> values) {
92+
List<String> list = new ArrayList<>(values.size());
93+
for (BookName value : values) {
94+
if (Objects.isNull(value)) {
95+
list.add("");
96+
} else {
97+
list.add(value.toString());
98+
}
99+
}
100+
return list;
101+
}
102+
103+
public static boolean isParsableFrom(String formattedString) {
104+
return SHELF_ID_BOOK_ID.matches(formattedString);
105+
}
106+
107+
@Override
108+
public Map<String, String> getFieldValuesMap() {
109+
if (Objects.isNull(fieldValuesMap)) {
110+
synchronized (this) {
111+
if (Objects.isNull(fieldValuesMap)) {
112+
ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder();
113+
if (!Objects.isNull(shelfId)) {
114+
fieldMapBuilder.put("shelf_id", shelfId);
115+
}
116+
if (!Objects.isNull(bookId)) {
117+
fieldMapBuilder.put("book_id", bookId);
118+
}
119+
fieldValuesMap = fieldMapBuilder.build();
120+
}
121+
}
122+
}
123+
return fieldValuesMap;
124+
}
125+
126+
public String getFieldValue(String fieldName) {
127+
return getFieldValuesMap().get(fieldName);
128+
}
129+
130+
@Override
131+
public String toString() {
132+
return SHELF_ID_BOOK_ID.instantiate("shelf_id", shelfId, "book_id", bookId);
133+
}
134+
135+
@Override
136+
public boolean equals(Object o) {
137+
if (o == this) {
138+
return true;
139+
}
140+
if (o != null || getClass() == o.getClass()) {
141+
BookName that = ((BookName) o);
142+
return Objects.equals(this.shelfId, that.shelfId) && Objects.equals(this.bookId, that.bookId);
143+
}
144+
return false;
145+
}
146+
147+
@Override
148+
public int hashCode() {
149+
int h = 1;
150+
h *= 1000003;
151+
h ^= Objects.hashCode(shelfId);
152+
h *= 1000003;
153+
h ^= Objects.hashCode(bookId);
154+
return h;
155+
}
156+
157+
/** Builder for shelves/{shelf_id}/books/{book_id}. */
158+
public static class Builder {
159+
private String shelfId;
160+
private String bookId;
161+
162+
protected Builder() {}
163+
164+
public String getShelfId() {
165+
return shelfId;
166+
}
167+
168+
public String getBookId() {
169+
return bookId;
170+
}
171+
172+
public Builder setShelfId(String shelfId) {
173+
this.shelfId = shelfId;
174+
return this;
175+
}
176+
177+
public Builder setBookId(String bookId) {
178+
this.bookId = bookId;
179+
return this;
180+
}
181+
182+
private Builder(BookName bookName) {
183+
shelfId = bookName.shelfId;
184+
bookId = bookName.bookId;
185+
}
186+
187+
public BookName build() {
188+
return new BookName(this);
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)