Skip to content

Commit 1b595d3

Browse files
committed
refactor: breakout composer classes, update sample naming
1 parent 27a548b commit 1b595d3

8 files changed

Lines changed: 1627 additions & 1687 deletions

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
import com.google.api.generator.engine.ast.VariableExpr;
5757
import com.google.api.generator.gapic.composer.comment.ServiceClientCommentComposer;
5858
import com.google.api.generator.gapic.composer.samplecode.SampleCodeWriter;
59-
import com.google.api.generator.gapic.composer.samplecode.ServiceClientSampleCodeComposer;
59+
import com.google.api.generator.gapic.composer.samplecode.ServiceClientCallableMethodSampleComposer;
60+
import com.google.api.generator.gapic.composer.samplecode.ServiceClientHeaderSampleComposer;
61+
import com.google.api.generator.gapic.composer.samplecode.ServiceClientMethodSampleComposer;
6062
import com.google.api.generator.gapic.composer.store.TypeStore;
6163
import com.google.api.generator.gapic.composer.utils.ClassNames;
6264
import com.google.api.generator.gapic.composer.utils.PackageChecker;
@@ -194,13 +196,13 @@ private static List<CommentStatement> createClassHeaderComments(
194196
TypeNode clientType = typeStore.get(ClassNames.getServiceClientClassName(service));
195197
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
196198
Sample classMethodSampleCode =
197-
ServiceClientSampleCodeComposer.composeClassHeaderMethodSampleCode(
199+
ServiceClientHeaderSampleComposer.composeClassHeaderSample(
198200
service, clientType, resourceNames, messageTypes);
199201
Sample credentialsSampleCode =
200-
ServiceClientSampleCodeComposer.composeClassHeaderCredentialsSampleCode(
202+
ServiceClientHeaderSampleComposer.composeSetCredentialsSample(
201203
clientType, settingsType);
202204
Sample endpointSampleCode =
203-
ServiceClientSampleCodeComposer.composeClassHeaderEndpointSampleCode(
205+
ServiceClientHeaderSampleComposer.composeSetEndpointSample(
204206
clientType, settingsType);
205207
samples.addAll(Arrays.asList(classMethodSampleCode, credentialsSampleCode, endpointSampleCode));
206208
return ServiceClientCommentComposer.createClassHeaderComments(
@@ -713,7 +715,7 @@ private static List<MethodDefinition> createMethodVariants(
713715

714716
Optional<Sample> methodSample =
715717
Optional.of(
716-
ServiceClientSampleCodeComposer.composeRpcMethodHeaderSampleCode(
718+
ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(
717719
method, typeStore.get(clientName), signature, resourceNames, messageTypes));
718720
Optional<String> methodDocSample = Optional.empty();
719721
if (methodSample.isPresent()) {
@@ -801,7 +803,7 @@ private static MethodDefinition createMethodDefaultMethod(
801803

802804
Optional<Sample> defaultMethodSample =
803805
Optional.of(
804-
ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode(
806+
ServiceClientMethodSampleComposer.composeCanonicalSample(
805807
method, typeStore.get(clientName), resourceNames, messageTypes));
806808
Optional<String> defaultMethodDocSample = Optional.empty();
807809
if (defaultMethodSample.isPresent()) {
@@ -940,15 +942,15 @@ private static MethodDefinition createCallableMethod(
940942
if (callableMethodKind.equals(CallableMethodKind.LRO)) {
941943
sampleCode =
942944
Optional.of(
943-
ServiceClientSampleCodeComposer.composeLroCallableMethodHeaderSampleCode(
945+
ServiceClientCallableMethodSampleComposer.composeLroCallableMethod(
944946
method,
945947
typeStore.get(ClassNames.getServiceClientClassName(service)),
946948
resourceNames,
947949
messageTypes));
948950
} else if (callableMethodKind.equals(CallableMethodKind.PAGED)) {
949951
sampleCode =
950952
Optional.of(
951-
ServiceClientSampleCodeComposer.composePagedCallableMethodHeaderSampleCode(
953+
ServiceClientCallableMethodSampleComposer.composePagedCallableMethod(
952954
method,
953955
typeStore.get(ClassNames.getServiceClientClassName(service)),
954956
resourceNames,
@@ -957,15 +959,15 @@ private static MethodDefinition createCallableMethod(
957959
if (method.stream().equals(Stream.NONE)) {
958960
sampleCode =
959961
Optional.of(
960-
ServiceClientSampleCodeComposer.composeRegularCallableMethodHeaderSampleCode(
962+
ServiceClientCallableMethodSampleComposer.composeRegularCallableMethod(
961963
method,
962964
typeStore.get(ClassNames.getServiceClientClassName(service)),
963965
resourceNames,
964966
messageTypes));
965967
} else {
966968
sampleCode =
967969
Optional.of(
968-
ServiceClientSampleCodeComposer.composeStreamCallableMethodHeaderSampleCode(
970+
ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(
969971
method,
970972
typeStore.get(ClassNames.getServiceClientClassName(service)),
971973
resourceNames,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.api.generator.gapic.composer.samplecode;
16+
17+
import com.google.api.generator.engine.ast.AssignmentExpr;
18+
import com.google.api.generator.engine.ast.MethodInvocationExpr;
19+
import com.google.api.generator.engine.ast.TypeNode;
20+
import com.google.api.generator.engine.ast.VariableExpr;
21+
import com.google.api.generator.gapic.model.MethodArgument;
22+
import com.google.api.generator.gapic.model.ResourceName;
23+
import com.google.api.generator.gapic.utils.JavaStyle;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.stream.Collectors;
27+
28+
public class SampleComposerUtil {
29+
// Assign client variable expr with create client.
30+
// e.g EchoClient echoClient = EchoClient.create()
31+
static AssignmentExpr assignClientVariableWithCreateMethodExpr(VariableExpr clientVarExpr) {
32+
return AssignmentExpr.builder()
33+
.setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build())
34+
.setValueExpr(
35+
MethodInvocationExpr.builder()
36+
.setStaticReferenceType(clientVarExpr.variable().type())
37+
.setReturnType(clientVarExpr.variable().type())
38+
.setMethodName("create")
39+
.build())
40+
.build();
41+
}
42+
43+
static boolean isStringTypedResourceName(
44+
MethodArgument arg, Map<String, ResourceName> resourceNames) {
45+
return arg.type().equals(TypeNode.STRING)
46+
&& arg.field().hasResourceReference()
47+
&& resourceNames.containsKey(arg.field().resourceReference().resourceTypeString());
48+
}
49+
50+
static boolean isProtoEmptyType(TypeNode type) {
51+
return type.reference().pakkage().equals("com.google.protobuf")
52+
&& type.reference().name().equals("Empty");
53+
}
54+
55+
static String createOverloadDisambiguation(List<VariableExpr> methodArgVarExprs) {
56+
return methodArgVarExprs.stream()
57+
.map(
58+
arg ->
59+
JavaStyle.toUpperCamelCase(
60+
arg.variable().type().reference() == null
61+
? arg.variable().type().typeKind().name().toLowerCase()
62+
: arg.variable().type().reference().name().toLowerCase()))
63+
.collect(Collectors.joining());
64+
}
65+
}

0 commit comments

Comments
 (0)