Skip to content

Commit 8fe3a2d

Browse files
authored
feat: add full RetrySettings sample code to Settings classes (#3056)
Fixes #2596 ☕️
1 parent 7f0ccc6 commit 8fe3a2d

File tree

120 files changed

+2634
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+2634
-176
lines changed

gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/comment/SettingsCommentComposer.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ public class SettingsCommentComposer {
3939
private static final String CLASS_HEADER_DEFAULT_ADDRESS_PORT_PATTERN =
4040
"The default service address (%s) and default port (%d) are used.";
4141
private static final String CLASS_HEADER_SAMPLE_CODE_PATTERN =
42-
"For example, to set the total timeout of %s to 30 seconds:";
42+
"For example, to set the [RetrySettings](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.retrying.RetrySettings) of %s:";
43+
44+
private static final String CLASS_HEADER_LRO_SAMPLE_CODE_PATTERN =
45+
"To configure the RetrySettings of a Long Running Operation method, create an OperationTimedPollAlgorithm object and update the RPC's polling algorithm. For example, to configure the RetrySettings for %s:";
46+
47+
private static final String CLASS_HEADER_SAMPLE_CODE_SUFFIX =
48+
"Please refer to the [Client Side Retry Guide](https://github.com/googleapis/google-cloud-java/blob/main/docs/client_retries.md) for additional support in setting retries.";
4349

4450
private static final String CLASS_HEADER_BUILDER_DESCRIPTION =
4551
"The builder of this class is recursive, so contained classes are themselves builders. When"
@@ -139,6 +145,8 @@ public static List<CommentStatement> createClassHeaderComments(
139145
boolean isDeprecated,
140146
Optional<String> methodNameOpt,
141147
Optional<String> sampleCodeOpt,
148+
Optional<String> lroMethodNameOpt,
149+
Optional<String> lroSampleCodeOpt,
142150
TypeNode classType) {
143151
// Split default address and port.
144152
int colonIndex = defaultHost.indexOf(COLON);
@@ -170,7 +178,18 @@ public static List<CommentStatement> createClassHeaderComments(
170178
String.format(
171179
CLASS_HEADER_SAMPLE_CODE_PATTERN,
172180
JavaStyle.toLowerCamelCase(methodNameOpt.get())))
173-
.addSampleCode(sampleCodeOpt.get());
181+
.addSampleCode(sampleCodeOpt.get())
182+
.addComment(CLASS_HEADER_SAMPLE_CODE_SUFFIX);
183+
}
184+
185+
if (lroMethodNameOpt.isPresent() && lroSampleCodeOpt.isPresent()) {
186+
javaDocCommentBuilder =
187+
javaDocCommentBuilder
188+
.addParagraph(
189+
String.format(
190+
CLASS_HEADER_LRO_SAMPLE_CODE_PATTERN,
191+
JavaStyle.toLowerCamelCase(lroMethodNameOpt.get())))
192+
.addSampleCode(lroSampleCodeOpt.get());
174193
}
175194

176195
if (isDeprecated) {

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,39 @@ private static List<CommentStatement> createClassHeaderComments(
148148
Optional<Sample> sampleCode =
149149
SettingsSampleComposer.composeSettingsSample(
150150
methodNameOpt, ClassNames.getServiceSettingsClassName(service), classType);
151-
152151
Optional<String> docSampleCode = Optional.empty();
153152
if (sampleCode.isPresent()) {
154153
samples.add(sampleCode.get());
155154
docSampleCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get().body()));
156155
}
156+
// Create a sample for a LRO method using LRO-specific RetrySettings, if one exists in the
157+
// service.
158+
Optional<Method> lroMethodOpt =
159+
service.methods().isEmpty()
160+
? Optional.empty()
161+
: service.methods().stream()
162+
.filter(m -> m.stream() == Stream.NONE && m.hasLro())
163+
.findFirst();
164+
Optional<String> lroMethodNameOpt =
165+
lroMethodOpt.isPresent() ? Optional.of(lroMethodOpt.get().name()) : Optional.empty();
166+
Optional<Sample> lroSampleCode =
167+
SettingsSampleComposer.composeLroSettingsSample(
168+
lroMethodNameOpt, ClassNames.getServiceSettingsClassName(service), classType);
169+
Optional<String> lroDocSampleCode = Optional.empty();
170+
if (lroSampleCode.isPresent()) {
171+
samples.add(lroSampleCode.get());
172+
lroDocSampleCode =
173+
Optional.of(SampleCodeWriter.writeInlineSample(lroSampleCode.get().body()));
174+
}
157175

158176
return SettingsCommentComposer.createClassHeaderComments(
159177
ClassNames.getServiceClientClassName(service),
160178
service.defaultHost(),
161179
service.isDeprecated(),
162180
methodNameOpt,
163181
docSampleCode,
182+
lroMethodNameOpt,
183+
lroDocSampleCode,
164184
classType);
165185
}
166186

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -423,22 +423,43 @@ private static List<CommentStatement> createClassHeaderComments(
423423
.findFirst()
424424
.orElse(service.methods().get(0)));
425425
Optional<String> methodNameOpt = methodOpt.map(Method::name);
426+
426427
Optional<Sample> sampleCode =
427428
SettingsSampleComposer.composeSettingsSample(
428429
methodNameOpt, ClassNames.getServiceSettingsClassName(service), classType);
429-
430430
Optional<String> docSampleCode = Optional.empty();
431431
if (sampleCode.isPresent()) {
432432
samples.add(sampleCode.get());
433433
docSampleCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get().body()));
434434
}
435+
// Create a sample for a LRO method using LRO-specific RetrySettings, if one exists in the
436+
// service.
437+
Optional<Method> lroMethodOpt =
438+
service.methods().isEmpty()
439+
? Optional.empty()
440+
: service.methods().stream()
441+
.filter(m -> m.stream() == Stream.NONE && m.hasLro())
442+
.findFirst();
443+
Optional<String> lroMethodNameOpt =
444+
lroMethodOpt.isPresent() ? Optional.of(lroMethodOpt.get().name()) : Optional.empty();
445+
Optional<Sample> lroSampleCode =
446+
SettingsSampleComposer.composeLroSettingsSample(
447+
lroMethodNameOpt, ClassNames.getServiceSettingsClassName(service), classType);
448+
Optional<String> lroDocSampleCode = Optional.empty();
449+
if (lroSampleCode.isPresent()) {
450+
samples.add(lroSampleCode.get());
451+
lroDocSampleCode =
452+
Optional.of(SampleCodeWriter.writeInlineSample(lroSampleCode.get().body()));
453+
}
435454

436455
return SettingsCommentComposer.createClassHeaderComments(
437456
ClassNames.getServiceStubClassName(service),
438457
service.defaultHost(),
439458
service.isDeprecated(),
440459
methodNameOpt,
441460
docSampleCode,
461+
lroMethodNameOpt,
462+
lroDocSampleCode,
442463
classType);
443464
}
444465

0 commit comments

Comments
 (0)