Skip to content

Commit a1fe20f

Browse files
authored
chore: do minor code simplification (#871)
1 parent d1bb7bd commit a1fe20f

4 files changed

Lines changed: 24 additions & 96 deletions

File tree

rules_java_gapic/java_gapic.bzl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_gapic//:gapic.bzl", "proto_custom_library", "unzipped_srcjar")
15+
load("@rules_gapic//:gapic.bzl", "proto_custom_library")
1616

1717
NO_GRPC_CONFIG_ALLOWLIST = ["library"]
1818

@@ -129,7 +129,6 @@ def _java_gapic_srcjar(
129129
# Can be used to provide a java_library with a customized generator,
130130
# like the one which dumps descriptor to a file for future debugging.
131131
java_generator_name = "java_gapic",
132-
output_suffix = ".srcjar",
133132
**kwargs):
134133
file_args_dict = {}
135134

@@ -146,7 +145,6 @@ def _java_gapic_srcjar(
146145
if service_yaml:
147146
file_args_dict[service_yaml] = "api-service-config"
148147

149-
output_suffix = ".srcjar"
150148
opt_args = []
151149

152150
if transport:
@@ -164,7 +162,7 @@ def _java_gapic_srcjar(
164162
plugin_file_args = {},
165163
opt_file_args = file_args_dict,
166164
output_type = java_generator_name,
167-
output_suffix = output_suffix,
165+
output_suffix = ".srcjar",
168166
opt_args = opt_args,
169167
**kwargs
170168
)

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@
2020
import com.google.api.gax.rpc.BidiStreamingCallable;
2121
import com.google.api.gax.rpc.ClientStreamingCallable;
2222
import com.google.api.gax.rpc.InvalidArgumentException;
23-
import com.google.api.gax.rpc.PagedCallSettings;
24-
import com.google.api.gax.rpc.ServerStreamingCallSettings;
2523
import com.google.api.gax.rpc.ServerStreamingCallable;
2624
import com.google.api.gax.rpc.StatusCode;
27-
import com.google.api.gax.rpc.StreamingCallSettings;
28-
import com.google.api.gax.rpc.UnaryCallSettings;
2925
import com.google.api.generator.engine.ast.AnnotationNode;
3026
import com.google.api.generator.engine.ast.AssignmentExpr;
3127
import com.google.api.generator.engine.ast.ClassDefinition;
@@ -58,7 +54,6 @@
5854
import com.google.api.generator.gapic.model.Message;
5955
import com.google.api.generator.gapic.model.Method;
6056
import com.google.api.generator.gapic.model.MethodArgument;
61-
import com.google.api.generator.gapic.model.OperationResponse;
6257
import com.google.api.generator.gapic.model.ResourceName;
6358
import com.google.api.generator.gapic.model.Service;
6459
import com.google.api.generator.gapic.utils.JavaStyle;
@@ -913,46 +908,6 @@ private void addDynamicTypes(GapicContext context, Service service, TypeStore ty
913908
}
914909
}
915910

916-
private static TypeNode getCallSettingsTypeHelper(
917-
Method protoMethod, TypeStore typeStore, boolean isBuilder) {
918-
Class callSettingsClazz = isBuilder ? UnaryCallSettings.Builder.class : UnaryCallSettings.class;
919-
if (protoMethod.isPaged()) {
920-
callSettingsClazz = isBuilder ? PagedCallSettings.Builder.class : PagedCallSettings.class;
921-
} else {
922-
switch (protoMethod.stream()) {
923-
case CLIENT:
924-
// Fall through.
925-
case BIDI:
926-
callSettingsClazz =
927-
isBuilder ? StreamingCallSettings.Builder.class : StreamingCallSettings.class;
928-
break;
929-
case SERVER:
930-
callSettingsClazz =
931-
isBuilder
932-
? ServerStreamingCallSettings.Builder.class
933-
: ServerStreamingCallSettings.class;
934-
break;
935-
case NONE:
936-
// Fall through
937-
default:
938-
// Fall through
939-
}
940-
}
941-
942-
List<Reference> generics = new ArrayList<>();
943-
generics.add(protoMethod.inputType().reference());
944-
generics.add(protoMethod.outputType().reference());
945-
if (protoMethod.isPaged()) {
946-
generics.add(
947-
typeStore
948-
.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, protoMethod.name()))
949-
.reference());
950-
}
951-
952-
return TypeNode.withReference(
953-
ConcreteReference.builder().setClazz(callSettingsClazz).setGenerics(generics).build());
954-
}
955-
956911
protected static TypeNode getCallableType(Method protoMethod) {
957912
Preconditions.checkState(
958913
!protoMethod.stream().equals(Method.Stream.NONE),

src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubClassComposer.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@
2929
import com.google.api.generator.engine.ast.MethodDefinition;
3030
import com.google.api.generator.engine.ast.Reference;
3131
import com.google.api.generator.engine.ast.ScopeNode;
32-
import com.google.api.generator.engine.ast.Statement;
3332
import com.google.api.generator.engine.ast.ThrowExpr;
3433
import com.google.api.generator.engine.ast.TypeNode;
35-
import com.google.api.generator.engine.ast.Variable;
36-
import com.google.api.generator.engine.ast.VariableExpr;
3734
import com.google.api.generator.gapic.composer.comment.StubCommentComposer;
3835
import com.google.api.generator.gapic.composer.store.TypeStore;
3936
import com.google.api.generator.gapic.composer.utils.ClassNames;
@@ -57,7 +54,6 @@
5754
import javax.annotation.Generated;
5855

5956
public abstract class AbstractServiceStubClassComposer implements ClassComposer {
60-
private static final String DOT = ".";
6157
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";
6258

6359
private final TransportContext transportContext;
@@ -126,7 +122,7 @@ private List<MethodDefinition> createClassMethods(
126122

127123
if (service.operationPollingMethod() != null) {
128124
methods.addAll(createLongRunningClientGetters(typeStore));
129-
}
125+
}
130126
methods.addAll(createCallableGetters(service, messageTypes, typeStore));
131127
methods.addAll(createBackgroundResourceMethodOverrides());
132128
return methods;
@@ -149,8 +145,7 @@ private List<MethodDefinition> createCallableGetters(
149145
return javaMethods;
150146
}
151147

152-
private MethodDefinition createOperationCallableGetter(
153-
Method method, TypeStore typeStore) {
148+
private MethodDefinition createOperationCallableGetter(Method method, TypeStore typeStore) {
154149
return createCallableGetterHelper(method, typeStore, true, false);
155150
}
156151

@@ -210,7 +205,8 @@ private MethodDefinition createCallableGetterHelper(
210205
return createCallableGetterMethodDefinition(returnType, methodName, annotations, typeStore);
211206
}
212207

213-
private List<MethodDefinition> createOperationsStubGetters(TypeStore typeStore, TypeNode operationsStubType) {
208+
private List<MethodDefinition> createOperationsStubGetters(
209+
TypeStore typeStore, TypeNode operationsStubType) {
214210
List<MethodDefinition> getters = new ArrayList<>();
215211

216212
Iterator<String> operationStubNameIt =
@@ -220,24 +216,27 @@ private List<MethodDefinition> createOperationsStubGetters(TypeStore typeStore,
220216
while (operationStubNameIt.hasNext() && operationStubTypeIt.hasNext()) {
221217
String methodName =
222218
String.format("get%s", JavaStyle.toUpperCamelCase(operationStubNameIt.next()));
223-
//TODO: refactor this
219+
// TODO: refactor this
224220
TypeNode actualOperationsStubType = operationStubTypeIt.next();
225221
if (operationsStubType != null) {
226222
actualOperationsStubType = operationsStubType;
227223
}
228224

229-
getters.add(createOperationsStubGetterMethodDefinition(actualOperationsStubType, methodName, typeStore));
225+
getters.add(
226+
createOperationsStubGetterMethodDefinition(
227+
actualOperationsStubType, methodName, typeStore));
230228
}
231229

232230
return getters;
233231
}
234232

235233
private List<MethodDefinition> createLongRunningClientGetters(TypeStore typeStore) {
236-
return ImmutableList.of(createCallableGetterMethodDefinition(
237-
TypeNode.withReference(ConcreteReference.withClazz(LongRunningClient.class)),
238-
"longRunningClient",
239-
ImmutableList.of(AnnotationNode.withType(typeStore.get("BetaApi"))),
240-
typeStore));
234+
return ImmutableList.of(
235+
createCallableGetterMethodDefinition(
236+
TypeNode.withReference(ConcreteReference.withClazz(LongRunningClient.class)),
237+
"longRunningClient",
238+
ImmutableList.of(AnnotationNode.withType(typeStore.get("BetaApi"))),
239+
typeStore));
241240
}
242241

243242
private static List<MethodDefinition> createBackgroundResourceMethodOverrides() {
@@ -283,7 +282,10 @@ private static TypeStore createTypes(Service service, Map<String, Message> messa
283282
}
284283

285284
protected MethodDefinition createCallableGetterMethodDefinition(
286-
TypeNode returnType, String methodName, List<AnnotationNode> annotations, TypeStore typeStore) {
285+
TypeNode returnType,
286+
String methodName,
287+
List<AnnotationNode> annotations,
288+
TypeStore typeStore) {
287289
return MethodDefinition.builder()
288290
.setScope(ScopeNode.PUBLIC)
289291
.setAnnotations(annotations)
@@ -314,8 +316,4 @@ protected MethodDefinition createOperationsStubGetterMethodDefinition(
314316
.build())))
315317
.build();
316318
}
317-
318-
private static String getClientClassName(Service service) {
319-
return String.format("%sClient", service.overriddenName());
320-
}
321319
}

src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
import java.util.HashMap;
6868
import java.util.List;
6969
import java.util.Map;
70-
import java.util.Optional;
7170
import java.util.Set;
7271
import java.util.function.BiFunction;
7372
import java.util.function.Function;
@@ -534,7 +533,7 @@ private List<Expr> setOperationSnapshotFactoryExpr(
534533
if (!protoMethod.isOperationPollingMethod()) {
535534
// TODO: Change to ordered map
536535
Map<String, String> requestFields = inputOperationMessage.operationRequestFields();
537-
List<String> fieldAnnotationNames = new ArrayList<String>(requestFields.keySet());
536+
List<String> fieldAnnotationNames = new ArrayList<>(requestFields.keySet());
538537
Collections.sort(fieldAnnotationNames);
539538
for (String fieldName : fieldAnnotationNames) {
540539
createBody.add(appendField(opNameVarExpr, requestVarExpr, requestFields.get(fieldName)));
@@ -644,7 +643,7 @@ private List<Expr> setPollingRequestFactoryExpr(
644643
Message inputOperationMessage =
645644
messageTypes.get(protoMethod.inputType().reference().fullName());
646645

647-
List<Statement> createBody = new ArrayList<Statement>(1);
646+
List<Statement> createBody = new ArrayList<>(1);
648647

649648
// Generate input variables for create
650649
VariableExpr compoundOperationIdVarExpr =
@@ -699,11 +698,11 @@ private List<Expr> setPollingRequestFactoryExpr(
699698
.setMethodName("newBuilder")
700699
.build();
701700
BiMap<String, String> responseFieldsMap = inputOperationMessage.operationResponseFields();
702-
List<String> responseFieldAnnotationNames = new ArrayList<String>(responseFieldsMap.keySet());
701+
List<String> responseFieldAnnotationNames = new ArrayList<>(responseFieldsMap.keySet());
703702
Collections.sort(responseFieldAnnotationNames);
704703
Set<String> responseFieldsNames = responseFieldsMap.inverse().keySet();
705704
Set<String> allFieldsNames = inputOperationMessage.fieldMap().keySet();
706-
ArrayList<String> nonResponseFieldsNames = new ArrayList<String>();
705+
ArrayList<String> nonResponseFieldsNames = new ArrayList<>();
707706
for (String fieldName : allFieldsNames) {
708707
if (!responseFieldsNames.contains(fieldName)) {
709708
nonResponseFieldsNames.add(fieldName);
@@ -1092,28 +1091,6 @@ protected VariableExpr declareLongRunningClient() {
10921091
.build());
10931092
}
10941093

1095-
protected Optional<String> getCallableCreatorMethodName(TypeNode callableVarExprType) {
1096-
final String typeName = callableVarExprType.reference().name();
1097-
String streamName = "Unary";
1098-
1099-
// Special handling for pagination methods.
1100-
if (callableVarExprType.reference().generics().size() == 2
1101-
&& callableVarExprType.reference().generics().get(1).name().endsWith("PagedResponse")) {
1102-
streamName = "Paged";
1103-
} else {
1104-
if (typeName.startsWith("Client")) {
1105-
return Optional.empty(); // not supported in REST transport
1106-
} else if (typeName.startsWith("Server")) {
1107-
return Optional.empty(); // not supported in REST transport (for now)
1108-
} else if (typeName.startsWith("Bidi")) {
1109-
return Optional.empty(); // not supported in REST transport
1110-
} else if (typeName.startsWith("Operation")) {
1111-
streamName = "Operation";
1112-
}
1113-
}
1114-
return Optional.of(String.format("create%sCallable", streamName));
1115-
}
1116-
11171094
@Override
11181095
protected List<Statement> createTypeRegistry(Service service) {
11191096
TypeNode typeRegistryType = FIXED_REST_TYPESTORE.get(TypeRegistry.class.getSimpleName());

0 commit comments

Comments
 (0)