Skip to content

Commit 6c06c55

Browse files
authored
[ggj][engx][codegen] fix: handle back-compat casing for IAMCredentials API (#601)
* fix: fix dep ordering in Bazel dedupe rules * chore: centralize all Gapic class name getters * fix: use gapic-remapped names in package-info.java * fix: revert unrelated changes * fix: handle back-compat casing for IAMCredentials API
1 parent eb26841 commit 6c06c55

19 files changed

+2484
-31
lines changed

src/main/java/com/google/api/generator/gapic/composer/ClassNames.java

+37-15
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,66 @@
2020
public class ClassNames {
2121
// Using constants since many of these class names are used often.
2222
private static final String MOCK_SERVICE_CLASS_NAME_PATTERN = "Mock%s";
23+
private static final String MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN = "Mock%sImpl";
2324
private static final String SERVICE_CLIENT_CLASS_NAME_PATTERN = "%sClient";
25+
private static final String SERVICE_CLIENT_TEST_CLASS_NAME_PATTERN = "%sClientTest";
2426
private static final String SERVICE_SETTINGS_CLASS_NAME_PATTERN = "%sSettings";
2527
private static final String SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN = "%sStubSettings";
2628
private static final String SERVICE_STUB_CLASS_NAME_PATTERN = "%sStub";
2729
private static final String GRPC_SERVICE_STUB_CLASS_NAME_PATTERN = "Grpc%sStub";
2830
private static final String GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN =
2931
"Grpc%sCallableFactory";
30-
private static final String MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN = "Mock%sImpl";
3132

3233
protected static String getServiceClientClassName(Service service) {
33-
return String.format(SERVICE_CLIENT_CLASS_NAME_PATTERN, service.overriddenName());
34+
return String.format(
35+
SERVICE_CLIENT_CLASS_NAME_PATTERN,
36+
monolithBackwardsCompatibleName(service.overriddenName()));
3437
}
3538

3639
protected static String getServiceSettingsClassName(Service service) {
37-
return String.format(SERVICE_SETTINGS_CLASS_NAME_PATTERN, service.overriddenName());
38-
}
39-
40-
protected static String getMockServiceClassName(Service service) {
41-
return String.format(MOCK_SERVICE_CLASS_NAME_PATTERN, service.name());
42-
}
43-
44-
protected static String getMockServiceImplClassName(Service service) {
45-
return String.format(MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN, service.name());
40+
return String.format(
41+
SERVICE_SETTINGS_CLASS_NAME_PATTERN,
42+
monolithBackwardsCompatibleName(service.overriddenName()));
4643
}
4744

4845
protected static String getServiceStubSettingsClassName(Service service) {
49-
return String.format(SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN, service.name());
46+
return String.format(
47+
SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN, monolithBackwardsCompatibleName(service.name()));
5048
}
5149

5250
protected static String getServiceStubClassName(Service service) {
53-
return String.format(SERVICE_STUB_CLASS_NAME_PATTERN, service.name());
51+
return String.format(
52+
SERVICE_STUB_CLASS_NAME_PATTERN, monolithBackwardsCompatibleName(service.name()));
5453
}
5554

5655
protected static String getGrpcServiceCallableFactoryClassName(Service service) {
57-
return String.format(GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN, service.name());
56+
return String.format(
57+
GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN,
58+
monolithBackwardsCompatibleName(service.name()));
5859
}
5960

6061
protected static String getGrpcServiceStubClassName(Service service) {
61-
return String.format(GRPC_SERVICE_STUB_CLASS_NAME_PATTERN, service.name());
62+
return String.format(
63+
GRPC_SERVICE_STUB_CLASS_NAME_PATTERN, monolithBackwardsCompatibleName(service.name()));
64+
}
65+
66+
protected static String getServiceClientTestClassName(Service service) {
67+
return String.format(
68+
SERVICE_CLIENT_TEST_CLASS_NAME_PATTERN,
69+
monolithBackwardsCompatibleName(service.overriddenName()));
70+
}
71+
72+
protected static String getMockServiceClassName(Service service) {
73+
return String.format(MOCK_SERVICE_CLASS_NAME_PATTERN, service.name());
74+
}
75+
76+
protected static String getMockServiceImplClassName(Service service) {
77+
return String.format(MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN, service.name());
78+
}
79+
80+
private static String monolithBackwardsCompatibleName(String rawServiceName) {
81+
return rawServiceName.startsWith("IAMCredentials")
82+
? rawServiceName.replace("IAM", "Iam")
83+
: rawServiceName;
6284
}
6385
}

src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public GapicClass generate(Service service, Map<String, Message> ignore) {
163163
.setAnnotations(createClassAnnotations())
164164
.setScope(ScopeNode.PUBLIC)
165165
.setName(className)
166-
.setExtendsType(types.get(String.format(STUB_PATTERN, service.name())))
166+
.setExtendsType(types.get(ClassNames.getServiceStubClassName(service)))
167167
.setStatements(classStatements)
168168
.setMethods(
169169
createClassMethods(
@@ -500,7 +500,7 @@ private static List<MethodDefinition> createConstructorMethods(
500500
Map<String, VariableExpr> classMemberVarExprs,
501501
Map<String, VariableExpr> callableClassMemberVarExprs,
502502
Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs) {
503-
TypeNode stubSettingsType = types.get(String.format(STUB_SETTINGS_PATTERN, service.name()));
503+
TypeNode stubSettingsType = types.get(ClassNames.getServiceStubSettingsClassName(service));
504504
VariableExpr settingsVarExpr =
505505
VariableExpr.withVariable(
506506
Variable.builder().setName("settings").setType(stubSettingsType).build());
@@ -549,8 +549,7 @@ private static List<MethodDefinition> createConstructorMethods(
549549
NewObjectExpr.builder()
550550
.setType(
551551
types.get(
552-
String.format(
553-
GRPC_SERVICE_CALLABLE_FACTORY_PATTERN, service.name())))
552+
ClassNames.getGrpcServiceCallableFactoryClassName(service)))
554553
.build())
555554
.build())));
556555

src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private static List<MethodDefinition> createConstructorMethods(
303303
String thisClientName = ClassNames.getServiceClientClassName(service);
304304
String settingsName = ClassNames.getServiceSettingsClassName(service);
305305
TypeNode thisClassType = types.get(thisClientName);
306-
TypeNode stubSettingsType = types.get(String.format("%sStubSettings", service.name()));
306+
TypeNode stubSettingsType = types.get(ClassNames.getServiceStubSettingsClassName(service));
307307
TypeNode operationsClientType = types.get("OperationsClient");
308308
TypeNode exceptionType = types.get("IOException");
309309

@@ -1430,26 +1430,32 @@ private static Map<String, TypeNode> createConcreteTypes() {
14301430
private static Map<String, TypeNode> createVaporTypes(Service service) {
14311431
// Client stub types.
14321432
Map<String, TypeNode> types =
1433-
Arrays.asList("%sStub", "%sStubSettings").stream()
1433+
Arrays.asList(
1434+
ClassNames.getServiceStubClassName(service),
1435+
ClassNames.getServiceStubSettingsClassName(service))
1436+
.stream()
14341437
.collect(
14351438
Collectors.toMap(
1436-
t -> String.format(t, service.name()),
1437-
t ->
1439+
n -> n,
1440+
n ->
14381441
TypeNode.withReference(
14391442
VaporReference.builder()
1440-
.setName(String.format(t, service.name()))
1443+
.setName(n)
14411444
.setPakkage(String.format("%s.stub", service.pakkage()))
14421445
.build())));
14431446
// Client ServiceClient and ServiceSettings types.
14441447
types.putAll(
1445-
Arrays.asList("%sClient", "%sSettings").stream()
1448+
Arrays.asList(
1449+
ClassNames.getServiceClientClassName(service),
1450+
ClassNames.getServiceSettingsClassName(service))
1451+
.stream()
14461452
.collect(
14471453
Collectors.toMap(
1448-
t -> String.format(t, service.overriddenName()),
1449-
t ->
1454+
n -> n,
1455+
n ->
14501456
TypeNode.withReference(
14511457
VaporReference.builder()
1452-
.setName(String.format(t, service.overriddenName()))
1458+
.setName(n)
14531459
.setPakkage(service.pakkage())
14541460
.build()))));
14551461

src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public GapicClass generate(
135135
Service service, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
136136
String pakkage = service.pakkage();
137137
Map<String, TypeNode> types = createDynamicTypes(service);
138-
String className = String.format("%sClientTest", service.overriddenName());
138+
String className = ClassNames.getServiceClientTestClassName(service);
139139
GapicClass.Kind kind = Kind.MAIN;
140140

141141
Map<String, VariableExpr> classMemberVarExprs = createClassMemberVarExprs(service, types);

src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static ServiceStubClassComposer instance() {
5959
@Override
6060
public GapicClass generate(Service service, Map<String, Message> messageTypes) {
6161
Map<String, TypeNode> types = createTypes(service, messageTypes);
62-
String className = String.format("%sStub", service.name());
62+
String className = ClassNames.getServiceStubClassName(service);
6363
GapicClass.Kind kind = Kind.STUB;
6464
String pakkage = String.format("%s.stub", service.pakkage());
6565

src/main/java/com/google/api/generator/gapic/utils/JavaStyle.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static String toLowerCamelCase(String s) {
2929
if (s.indexOf(UNDERSCORE) >= 0) {
3030
s = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, s);
3131
}
32+
3233
return capitalizeLettersAfterDigits(
3334
String.format("%s%s", s.substring(0, 1).toLowerCase(), s.substring(1)));
3435
}
@@ -46,7 +47,8 @@ public static String toUpperCamelCase(String s) {
4647
}
4748

4849
public static String toUpperSnakeCase(String s) {
49-
return CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, toUpperCamelCase(s));
50+
String result = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, toUpperCamelCase(s));
51+
return result;
5052
}
5153

5254
private static String capitalizeLettersAfterDigits(String s) {

test/integration/BUILD.bazel

+35
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package(default_visibility = ["//visibility:public"])
1818

1919
INTEGRATION_TEST_LIBRARIES = [
2020
"asset",
21+
"credentials",
2122
"logging",
2223
"redis",
2324
"library", # No gRPC service config.
@@ -180,3 +181,37 @@ java_gapic_assembly_gradle_pkg(
180181
"@com_google_googleapis//google/example/library/v1:library_proto",
181182
],
182183
)
184+
185+
# IAMCredentials.
186+
# Check that the capital name edge case is handled.
187+
java_gapic_library(
188+
name = "credentials_java_gapic",
189+
srcs = ["@com_google_googleapis//google/iam/credentials/v1:credentials_proto_with_info"],
190+
grpc_service_config = "@com_google_googleapis//google/iam/credentials/v1:iamcredentials_grpc_service_config.json",
191+
package = "google.iam.credentials.v1",
192+
test_deps = [
193+
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_grpc",
194+
],
195+
deps = [
196+
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_proto",
197+
],
198+
)
199+
200+
java_gapic_test(
201+
name = "credentials_java_gapic_test_suite",
202+
test_classes = [
203+
"com.google.cloud.iam.credentials.v1.IamCredentialsClientTest",
204+
],
205+
runtime_deps = [":credentials_java_gapic_test"],
206+
)
207+
208+
# Open Source Packages
209+
java_gapic_assembly_gradle_pkg(
210+
name = "google-cloud-iam-credentials-v1-java",
211+
deps = [
212+
":credentials_java_gapic",
213+
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_grpc",
214+
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_proto",
215+
"@com_google_googleapis//google/iam/credentials/v1:credentials_proto",
216+
],
217+
)
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,113 @@
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.cloud.iam.credentials.v1.stub;
18+
19+
import com.google.api.gax.grpc.GrpcCallSettings;
20+
import com.google.api.gax.grpc.GrpcCallableFactory;
21+
import com.google.api.gax.grpc.GrpcStubCallableFactory;
22+
import com.google.api.gax.rpc.BatchingCallSettings;
23+
import com.google.api.gax.rpc.BidiStreamingCallable;
24+
import com.google.api.gax.rpc.ClientContext;
25+
import com.google.api.gax.rpc.ClientStreamingCallable;
26+
import com.google.api.gax.rpc.OperationCallSettings;
27+
import com.google.api.gax.rpc.OperationCallable;
28+
import com.google.api.gax.rpc.PagedCallSettings;
29+
import com.google.api.gax.rpc.ServerStreamingCallSettings;
30+
import com.google.api.gax.rpc.ServerStreamingCallable;
31+
import com.google.api.gax.rpc.StreamingCallSettings;
32+
import com.google.api.gax.rpc.UnaryCallSettings;
33+
import com.google.api.gax.rpc.UnaryCallable;
34+
import com.google.longrunning.Operation;
35+
import com.google.longrunning.stub.OperationsStub;
36+
import javax.annotation.Generated;
37+
38+
// AUTO-GENERATED DOCUMENTATION AND CLASS.
39+
/**
40+
* gRPC callable factory implementation for the IAMCredentials service API.
41+
*
42+
* <p>This class is for advanced usage.
43+
*/
44+
@Generated("by gapic-generator")
45+
public class GrpcIamCredentialsCallableFactory implements GrpcStubCallableFactory {
46+
47+
@Override
48+
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable(
49+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
50+
UnaryCallSettings<RequestT, ResponseT> callSettings,
51+
ClientContext clientContext) {
52+
return GrpcCallableFactory.createUnaryCallable(grpcCallSettings, callSettings, clientContext);
53+
}
54+
55+
@Override
56+
public <RequestT, ResponseT, PagedListResponseT>
57+
UnaryCallable<RequestT, PagedListResponseT> createPagedCallable(
58+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
59+
PagedCallSettings<RequestT, ResponseT, PagedListResponseT> callSettings,
60+
ClientContext clientContext) {
61+
return GrpcCallableFactory.createPagedCallable(grpcCallSettings, callSettings, clientContext);
62+
}
63+
64+
@Override
65+
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBatchingCallable(
66+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
67+
BatchingCallSettings<RequestT, ResponseT> callSettings,
68+
ClientContext clientContext) {
69+
return GrpcCallableFactory.createBatchingCallable(
70+
grpcCallSettings, callSettings, clientContext);
71+
}
72+
73+
@Override
74+
public <RequestT, ResponseT, MetadataT>
75+
OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
76+
GrpcCallSettings<RequestT, Operation> grpcCallSettings,
77+
OperationCallSettings<RequestT, ResponseT, MetadataT> callSettings,
78+
ClientContext clientContext,
79+
OperationsStub operationsStub) {
80+
return GrpcCallableFactory.createOperationCallable(
81+
grpcCallSettings, callSettings, clientContext, operationsStub);
82+
}
83+
84+
@Override
85+
public <RequestT, ResponseT>
86+
BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
87+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
88+
StreamingCallSettings<RequestT, ResponseT> callSettings,
89+
ClientContext clientContext) {
90+
return GrpcCallableFactory.createBidiStreamingCallable(
91+
grpcCallSettings, callSettings, clientContext);
92+
}
93+
94+
@Override
95+
public <RequestT, ResponseT>
96+
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
97+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
98+
ServerStreamingCallSettings<RequestT, ResponseT> callSettings,
99+
ClientContext clientContext) {
100+
return GrpcCallableFactory.createServerStreamingCallable(
101+
grpcCallSettings, callSettings, clientContext);
102+
}
103+
104+
@Override
105+
public <RequestT, ResponseT>
106+
ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
107+
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
108+
StreamingCallSettings<RequestT, ResponseT> callSettings,
109+
ClientContext clientContext) {
110+
return GrpcCallableFactory.createClientStreamingCallable(
111+
grpcCallSettings, callSettings, clientContext);
112+
}
113+
}

0 commit comments

Comments
 (0)