Skip to content

Commit

Permalink
[ggj][engx][codegen] fix: handle back-compat casing for IAMCredential…
Browse files Browse the repository at this point in the history
…s 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
  • Loading branch information
miraleung authored Dec 16, 2020
1 parent eb26841 commit 6c06c55
Show file tree
Hide file tree
Showing 19 changed files with 2,484 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,66 @@
public class ClassNames {
// Using constants since many of these class names are used often.
private static final String MOCK_SERVICE_CLASS_NAME_PATTERN = "Mock%s";
private static final String MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN = "Mock%sImpl";
private static final String SERVICE_CLIENT_CLASS_NAME_PATTERN = "%sClient";
private static final String SERVICE_CLIENT_TEST_CLASS_NAME_PATTERN = "%sClientTest";
private static final String SERVICE_SETTINGS_CLASS_NAME_PATTERN = "%sSettings";
private static final String SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN = "%sStubSettings";
private static final String SERVICE_STUB_CLASS_NAME_PATTERN = "%sStub";
private static final String GRPC_SERVICE_STUB_CLASS_NAME_PATTERN = "Grpc%sStub";
private static final String GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN =
"Grpc%sCallableFactory";
private static final String MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN = "Mock%sImpl";

protected static String getServiceClientClassName(Service service) {
return String.format(SERVICE_CLIENT_CLASS_NAME_PATTERN, service.overriddenName());
return String.format(
SERVICE_CLIENT_CLASS_NAME_PATTERN,
monolithBackwardsCompatibleName(service.overriddenName()));
}

protected static String getServiceSettingsClassName(Service service) {
return String.format(SERVICE_SETTINGS_CLASS_NAME_PATTERN, service.overriddenName());
}

protected static String getMockServiceClassName(Service service) {
return String.format(MOCK_SERVICE_CLASS_NAME_PATTERN, service.name());
}

protected static String getMockServiceImplClassName(Service service) {
return String.format(MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN, service.name());
return String.format(
SERVICE_SETTINGS_CLASS_NAME_PATTERN,
monolithBackwardsCompatibleName(service.overriddenName()));
}

protected static String getServiceStubSettingsClassName(Service service) {
return String.format(SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN, service.name());
return String.format(
SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN, monolithBackwardsCompatibleName(service.name()));
}

protected static String getServiceStubClassName(Service service) {
return String.format(SERVICE_STUB_CLASS_NAME_PATTERN, service.name());
return String.format(
SERVICE_STUB_CLASS_NAME_PATTERN, monolithBackwardsCompatibleName(service.name()));
}

protected static String getGrpcServiceCallableFactoryClassName(Service service) {
return String.format(GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN, service.name());
return String.format(
GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN,
monolithBackwardsCompatibleName(service.name()));
}

protected static String getGrpcServiceStubClassName(Service service) {
return String.format(GRPC_SERVICE_STUB_CLASS_NAME_PATTERN, service.name());
return String.format(
GRPC_SERVICE_STUB_CLASS_NAME_PATTERN, monolithBackwardsCompatibleName(service.name()));
}

protected static String getServiceClientTestClassName(Service service) {
return String.format(
SERVICE_CLIENT_TEST_CLASS_NAME_PATTERN,
monolithBackwardsCompatibleName(service.overriddenName()));
}

protected static String getMockServiceClassName(Service service) {
return String.format(MOCK_SERVICE_CLASS_NAME_PATTERN, service.name());
}

protected static String getMockServiceImplClassName(Service service) {
return String.format(MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN, service.name());
}

private static String monolithBackwardsCompatibleName(String rawServiceName) {
return rawServiceName.startsWith("IAMCredentials")
? rawServiceName.replace("IAM", "Iam")
: rawServiceName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public GapicClass generate(Service service, Map<String, Message> ignore) {
.setAnnotations(createClassAnnotations())
.setScope(ScopeNode.PUBLIC)
.setName(className)
.setExtendsType(types.get(String.format(STUB_PATTERN, service.name())))
.setExtendsType(types.get(ClassNames.getServiceStubClassName(service)))
.setStatements(classStatements)
.setMethods(
createClassMethods(
Expand Down Expand Up @@ -500,7 +500,7 @@ private static List<MethodDefinition> createConstructorMethods(
Map<String, VariableExpr> classMemberVarExprs,
Map<String, VariableExpr> callableClassMemberVarExprs,
Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs) {
TypeNode stubSettingsType = types.get(String.format(STUB_SETTINGS_PATTERN, service.name()));
TypeNode stubSettingsType = types.get(ClassNames.getServiceStubSettingsClassName(service));
VariableExpr settingsVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("settings").setType(stubSettingsType).build());
Expand Down Expand Up @@ -549,8 +549,7 @@ private static List<MethodDefinition> createConstructorMethods(
NewObjectExpr.builder()
.setType(
types.get(
String.format(
GRPC_SERVICE_CALLABLE_FACTORY_PATTERN, service.name())))
ClassNames.getGrpcServiceCallableFactoryClassName(service)))
.build())
.build())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private static List<MethodDefinition> createConstructorMethods(
String thisClientName = ClassNames.getServiceClientClassName(service);
String settingsName = ClassNames.getServiceSettingsClassName(service);
TypeNode thisClassType = types.get(thisClientName);
TypeNode stubSettingsType = types.get(String.format("%sStubSettings", service.name()));
TypeNode stubSettingsType = types.get(ClassNames.getServiceStubSettingsClassName(service));
TypeNode operationsClientType = types.get("OperationsClient");
TypeNode exceptionType = types.get("IOException");

Expand Down Expand Up @@ -1430,26 +1430,32 @@ private static Map<String, TypeNode> createConcreteTypes() {
private static Map<String, TypeNode> createVaporTypes(Service service) {
// Client stub types.
Map<String, TypeNode> types =
Arrays.asList("%sStub", "%sStubSettings").stream()
Arrays.asList(
ClassNames.getServiceStubClassName(service),
ClassNames.getServiceStubSettingsClassName(service))
.stream()
.collect(
Collectors.toMap(
t -> String.format(t, service.name()),
t ->
n -> n,
n ->
TypeNode.withReference(
VaporReference.builder()
.setName(String.format(t, service.name()))
.setName(n)
.setPakkage(String.format("%s.stub", service.pakkage()))
.build())));
// Client ServiceClient and ServiceSettings types.
types.putAll(
Arrays.asList("%sClient", "%sSettings").stream()
Arrays.asList(
ClassNames.getServiceClientClassName(service),
ClassNames.getServiceSettingsClassName(service))
.stream()
.collect(
Collectors.toMap(
t -> String.format(t, service.overriddenName()),
t ->
n -> n,
n ->
TypeNode.withReference(
VaporReference.builder()
.setName(String.format(t, service.overriddenName()))
.setName(n)
.setPakkage(service.pakkage())
.build()))));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public GapicClass generate(
Service service, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
String pakkage = service.pakkage();
Map<String, TypeNode> types = createDynamicTypes(service);
String className = String.format("%sClientTest", service.overriddenName());
String className = ClassNames.getServiceClientTestClassName(service);
GapicClass.Kind kind = Kind.MAIN;

Map<String, VariableExpr> classMemberVarExprs = createClassMemberVarExprs(service, types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static ServiceStubClassComposer instance() {
@Override
public GapicClass generate(Service service, Map<String, Message> messageTypes) {
Map<String, TypeNode> types = createTypes(service, messageTypes);
String className = String.format("%sStub", service.name());
String className = ClassNames.getServiceStubClassName(service);
GapicClass.Kind kind = Kind.STUB;
String pakkage = String.format("%s.stub", service.pakkage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static String toLowerCamelCase(String s) {
if (s.indexOf(UNDERSCORE) >= 0) {
s = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, s);
}

return capitalizeLettersAfterDigits(
String.format("%s%s", s.substring(0, 1).toLowerCase(), s.substring(1)));
}
Expand All @@ -46,7 +47,8 @@ public static String toUpperCamelCase(String s) {
}

public static String toUpperSnakeCase(String s) {
return CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, toUpperCamelCase(s));
String result = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, toUpperCamelCase(s));
return result;
}

private static String capitalizeLettersAfterDigits(String s) {
Expand Down
35 changes: 35 additions & 0 deletions test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package(default_visibility = ["//visibility:public"])

INTEGRATION_TEST_LIBRARIES = [
"asset",
"credentials",
"logging",
"redis",
"library", # No gRPC service config.
Expand Down Expand Up @@ -180,3 +181,37 @@ java_gapic_assembly_gradle_pkg(
"@com_google_googleapis//google/example/library/v1:library_proto",
],
)

# IAMCredentials.
# Check that the capital name edge case is handled.
java_gapic_library(
name = "credentials_java_gapic",
srcs = ["@com_google_googleapis//google/iam/credentials/v1:credentials_proto_with_info"],
grpc_service_config = "@com_google_googleapis//google/iam/credentials/v1:iamcredentials_grpc_service_config.json",
package = "google.iam.credentials.v1",
test_deps = [
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_grpc",
],
deps = [
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_proto",
],
)

java_gapic_test(
name = "credentials_java_gapic_test_suite",
test_classes = [
"com.google.cloud.iam.credentials.v1.IamCredentialsClientTest",
],
runtime_deps = [":credentials_java_gapic_test"],
)

# Open Source Packages
java_gapic_assembly_gradle_pkg(
name = "google-cloud-iam-credentials-v1-java",
deps = [
":credentials_java_gapic",
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_grpc",
"@com_google_googleapis//google/iam/credentials/v1:credentials_java_proto",
"@com_google_googleapis//google/iam/credentials/v1:credentials_proto",
],
)
6 changes: 6 additions & 0 deletions test/integration/goldens/credentials/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"]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.cloud.iam.credentials.v1.stub;

import com.google.api.gax.grpc.GrpcCallSettings;
import com.google.api.gax.grpc.GrpcCallableFactory;
import com.google.api.gax.grpc.GrpcStubCallableFactory;
import com.google.api.gax.rpc.BatchingCallSettings;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.OperationCallSettings;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.PagedCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.StreamingCallSettings;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.longrunning.Operation;
import com.google.longrunning.stub.OperationsStub;
import javax.annotation.Generated;

// AUTO-GENERATED DOCUMENTATION AND CLASS.
/**
* gRPC callable factory implementation for the IAMCredentials service API.
*
* <p>This class is for advanced usage.
*/
@Generated("by gapic-generator")
public class GrpcIamCredentialsCallableFactory implements GrpcStubCallableFactory {

@Override
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
UnaryCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return GrpcCallableFactory.createUnaryCallable(grpcCallSettings, callSettings, clientContext);
}

@Override
public <RequestT, ResponseT, PagedListResponseT>
UnaryCallable<RequestT, PagedListResponseT> createPagedCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
PagedCallSettings<RequestT, ResponseT, PagedListResponseT> callSettings,
ClientContext clientContext) {
return GrpcCallableFactory.createPagedCallable(grpcCallSettings, callSettings, clientContext);
}

@Override
public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBatchingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
BatchingCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return GrpcCallableFactory.createBatchingCallable(
grpcCallSettings, callSettings, clientContext);
}

@Override
public <RequestT, ResponseT, MetadataT>
OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
GrpcCallSettings<RequestT, Operation> grpcCallSettings,
OperationCallSettings<RequestT, ResponseT, MetadataT> callSettings,
ClientContext clientContext,
OperationsStub operationsStub) {
return GrpcCallableFactory.createOperationCallable(
grpcCallSettings, callSettings, clientContext, operationsStub);
}

@Override
public <RequestT, ResponseT>
BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
StreamingCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return GrpcCallableFactory.createBidiStreamingCallable(
grpcCallSettings, callSettings, clientContext);
}

@Override
public <RequestT, ResponseT>
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
ServerStreamingCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return GrpcCallableFactory.createServerStreamingCallable(
grpcCallSettings, callSettings, clientContext);
}

@Override
public <RequestT, ResponseT>
ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
StreamingCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
return GrpcCallableFactory.createClientStreamingCallable(
grpcCallSettings, callSettings, clientContext);
}
}
Loading

0 comments on commit 6c06c55

Please sign in to comment.