Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
ServiceClientHeaderSampleComposer.composeClassHeaderSample(
service, clientType, context.resourceNames(), context.messages());
javaDocCommentBuilder.addSampleCode(
SampleCodeWriter.writeInlineSample(packageInfoSampleCode.body()));
SampleCodeWriter.writeInlineSample(packageInfoSampleCode));
}

return CommentStatement.withComment(javaDocCommentBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.api.generator.engine.ast.LineComment;
import com.google.api.generator.engine.ast.Statement;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class CommentComposer {
CommentStatement.withComment(
LineComment.withComment(AUTO_GENERATED_METHOD_DISCLAIMER_STRING));

public static final List<Statement> AUTO_GENERATED_SAMPLE_COMMENT =
public static final List<Statement> AUTO_GENERATED_SAMPLE_COMMENT_BASE =
Arrays.asList(
CommentStatement.withComment(
LineComment.withComment(
Expand All @@ -80,6 +81,22 @@ public class CommentComposer {
LineComment.withComment(
"https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library")));

public static final List<Statement> CLIENT_LIFECYCLE_COMMENT =
Arrays.asList(
CommentStatement.withComment(
LineComment.withComment(
"- The samples create a client that can be re-used for multiple requests\n"
+ " and are expected to be long-lived.\n"
+ " The clients should be resource managed properly and eventually closed.\n"
+ "For more information on client lifecycle, see\n"
+ "https://docs.cloud.google.com/java/docs/client-lifecycle")));

public static final List<Statement> AUTO_GENERATED_SAMPLE_COMMENT =
ImmutableList.<Statement>builder()
.addAll(AUTO_GENERATED_SAMPLE_COMMENT_BASE)
.addAll(CLIENT_LIFECYCLE_COMMENT)
.build();

// This environment variable is mainly used to override the current year to a fixed year in tests
// so we don't have to update golden tests every year.
// This does not work for golden integration tests that are generated from Bazel rules. Because
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ protected List<CommentStatement> createClassHeaderComments(
return ServiceClientCommentComposer.createClassHeaderComments(
methodVariantsForClientHeader,
service,
SampleCodeWriter.writeInlineSample(classMethodSampleCode.body()),
SampleCodeWriter.writeInlineSample(credentialsSampleCode.body()),
SampleCodeWriter.writeInlineSample(endpointSampleCode.body()),
SampleCodeWriter.writeInlineSample(classMethodSampleCode),
SampleCodeWriter.writeInlineSample(credentialsSampleCode),
SampleCodeWriter.writeInlineSample(endpointSampleCode),
null,
null,
null);
Expand Down Expand Up @@ -797,7 +797,7 @@ private static List<MethodDefinition> createMethodVariants(
if (methodSample.isPresent()) {
samples.add(methodSample.get());
methodDocSample =
Optional.of(SampleCodeWriter.writeInlineSample(methodSample.get().body()));
Optional.of(SampleCodeWriter.writeInlineSample(methodSample.get()));
}

MethodDefinition.Builder methodVariantBuilder =
Expand Down Expand Up @@ -876,7 +876,7 @@ private static MethodDefinition createMethodDefaultMethod(
if (defaultMethodSample.isPresent()) {
samples.add(defaultMethodSample.get());
defaultMethodDocSample =
Optional.of(SampleCodeWriter.writeInlineSample(defaultMethodSample.get().body()));
Optional.of(SampleCodeWriter.writeInlineSample(defaultMethodSample.get()));
}

MethodInvocationExpr callableMethodExpr =
Expand Down Expand Up @@ -1043,7 +1043,7 @@ private static MethodDefinition createCallableMethod(
Optional<String> sampleDocCode = Optional.empty();
if (sampleCode.isPresent()) {
samples.add(sampleCode.get());
sampleDocCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get().body()));
sampleDocCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get()));
}

MethodDefinition.Builder methodDefBuilder = MethodDefinition.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.api.gax.rpc.StubSettings;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.generator.gapic.composer.comment.CommentComposer;
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
Expand Down Expand Up @@ -169,7 +170,7 @@ private static List<CommentStatement> createClassHeaderComments(
Optional<String> docSampleCode = Optional.empty();
if (sampleCode.isPresent()) {
samples.add(sampleCode.get());
docSampleCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get().body()));
docSampleCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get(), CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT_BASE));
}
// Create a sample for a LRO method using LRO-specific RetrySettings, if one exists in the
// service.
Expand All @@ -188,7 +189,7 @@ private static List<CommentStatement> createClassHeaderComments(
if (lroSampleCode.isPresent()) {
samples.add(lroSampleCode.get());
lroDocSampleCode =
Optional.of(SampleCodeWriter.writeInlineSample(lroSampleCode.get().body()));
Optional.of(SampleCodeWriter.writeInlineSample(lroSampleCode.get(), CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT_BASE));
}

return SettingsCommentComposer.createClassHeaderComments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.api.generator.gapic.composer.common;

import com.google.api.generator.gapic.composer.comment.CommentComposer;
import com.google.api.MonitoredResourceDescriptor;
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
Expand Down Expand Up @@ -459,7 +460,7 @@ private static List<CommentStatement> createClassHeaderComments(
Optional<String> docSampleCode = Optional.empty();
if (sampleCode.isPresent()) {
samples.add(sampleCode.get());
docSampleCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get().body()));
docSampleCode = Optional.of(SampleCodeWriter.writeInlineSample(sampleCode.get(), CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT_BASE));
}
// Create a sample for a LRO method using LRO-specific RetrySettings, if one exists in the
// service.
Expand All @@ -478,7 +479,7 @@ private static List<CommentStatement> createClassHeaderComments(
if (lroSampleCode.isPresent()) {
samples.add(lroSampleCode.get());
lroDocSampleCode =
Optional.of(SampleCodeWriter.writeInlineSample(lroSampleCode.get().body()));
Optional.of(SampleCodeWriter.writeInlineSample(lroSampleCode.get(), CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT_BASE));
}

return SettingsCommentComposer.createClassHeaderComments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ protected List<CommentStatement> createClassHeaderComments(
return ServiceClientCommentComposer.createClassHeaderComments(
grpcRpcsToJavaMethodNames,
service,
SampleCodeWriter.writeInlineSample(classMethodSampleCode.body()),
SampleCodeWriter.writeInlineSample(credentialsSampleCode.body()),
SampleCodeWriter.writeInlineSample(endpointSampleCode.body()),
SampleCodeWriter.writeInlineSample(transportSampleCode.body()),
SampleCodeWriter.writeInlineSample(classMethodSampleCode),
SampleCodeWriter.writeInlineSample(credentialsSampleCode),
SampleCodeWriter.writeInlineSample(endpointSampleCode),
SampleCodeWriter.writeInlineSample(transportSampleCode),
"gRPC",
"REST (HTTP1.1/JSON)");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,22 @@ public static String writeInlineSample(List<Statement> statements) {
return write(SampleComposer.composeInlineSample(statements));
}

public static String writeInlineSample(Sample sample) {
return write(SampleComposer.composeInlineSample(sample));
}

public static String writeInlineSample(Sample sample, List<Statement> sampleComment) {
return write(SampleComposer.composeInlineSample(sample, sampleComment));
}

public static String writeExecutableSample(Sample sample, String packkage) {
return write(SampleComposer.composeExecutableSample(sample, packkage));
}

public static String writeExecutableSample(Sample sample, String packkage, List<Statement> sampleComment) {
return write(SampleComposer.composeExecutableSample(sample, packkage, sampleComment));
}

@VisibleForTesting
public static String write(Statement... statement) {
return write(Arrays.asList(statement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,27 @@ static List<Statement> composeInlineSample(List<Statement> sampleBody) {
return bodyWithComment(CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT, sampleBody);
}

static List<Statement> composeInlineSample(Sample sample) {
return composeInlineSample(sample, CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT);
}

static List<Statement> composeInlineSample(Sample sample, List<Statement> sampleComment) {
return bodyWithComment(sampleComment, sample.body());
}

// "Executable" meaning it includes the necessary code to execute java code,
// still may require additional configuration to actually execute generated sample code
static ClassDefinition composeExecutableSample(Sample sample, String pakkage) {
return composeExecutableSample(sample, pakkage, CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT);
}

static ClassDefinition composeExecutableSample(Sample sample, String pakkage, List<Statement> sampleComment) {
return createExecutableSample(
sample.fileHeader(),
pakkage,
sample.name(),
sample.variableAssignments(),
bodyWithComment(CommentComposer.AUTO_GENERATED_SAMPLE_COMMENT, sample.body()),
bodyWithComment(sampleComment, sample.body()),
sample.regionTag());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,11 @@ public static Sample composeLroCallableMethod(
.setIsAsynchronous(true)
.setOverloadDisambiguation("LRO")
.build();
List<Statement> body =
Arrays.asList(
TryCatchStatement.builder()
.setTryResourceExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build());
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);
Comment on lines +405 to +409
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for creating the sample body is duplicated in several places across ServiceClientCallableMethodSampleComposer, ServiceClientHeaderSampleComposer, and ServiceClientMethodSampleComposer.

To improve maintainability and reduce code duplication, consider extracting this logic into a helper method in a utility class. This aligns with the practice of moving shared code to separate helper/utility classes to avoid duplication and maintain clean layering.

References
  1. If code is duplicated and needs to be shared, move it to a separate helper/utility class to improve maintainability and avoid layering issues.

return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}

Expand Down Expand Up @@ -524,14 +521,11 @@ public static Sample composePagedCallableMethod(
.setBody(Arrays.asList(lineCommentStatement))
.build();
bodyStatements.add(repeatedResponseForStatement);
List<Statement> body =
Arrays.asList(
TryCatchStatement.builder()
.setTryResourceExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build());
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);

RegionTag regionTag =
RegionTag.builder()
Expand Down Expand Up @@ -591,14 +585,11 @@ public static Sample composeRegularCallableMethod(
regionTag = unaryOrLroCallable.regionTag();
}

List<Statement> body =
Arrays.asList(
TryCatchStatement.builder()
.setTryResourceExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build());
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);
return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}

Expand Down Expand Up @@ -653,14 +644,11 @@ public static Sample composeStreamCallableMethod(
regionTag = streamClient.regionTag();
}

List<Statement> body =
Arrays.asList(
TryCatchStatement.builder()
.setTryResourceExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build());
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);
return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,11 @@ public static Sample composeShowcaseMethodSample(
regionTag = unaryRpc.regionTag();
}

List<Statement> body =
Arrays.asList(
TryCatchStatement.builder()
.setTryResourceExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build());
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);
return Sample.builder().setBody(body).setRegionTag(regionTag).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ public static Sample composeEmptyServiceSample(TypeNode clientType, Service serv
RegionTag regionTag =
RegionTag.builder().setServiceName(service.name()).setRpcName("emtpy").build();

List<Statement> body =
Arrays.asList(
TryCatchStatement.builder()
.setTryResourceExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build());
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);
return Sample.builder().setBody(body).setRegionTag(regionTag).setIsCanonical(true).build();
}

Expand Down Expand Up @@ -127,14 +124,11 @@ public static Sample composeCanonicalSample(
regionTag = unaryRpc.regionTag();
}

List<Statement> body =
Arrays.asList(
TryCatchStatement.builder()
.setTryResourceExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr))
.setTryBody(bodyStatements)
.setIsSampleCode(true)
.build());
List<Statement> body = new ArrayList<>();
body.add(
ExprStatement.withExpr(
SampleComposerUtil.assignClientVariableWithCreateMethodExpr(clientVarExpr)));
body.addAll(bodyStatements);
return Sample.builder().setBody(body).setRegionTag(regionTag).setIsCanonical(true).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,12 @@ public static Optional<Sample> composeSettingsSample(
.setServiceName(classType.reference().name())
.setRpcName(methodNameOpt.get())
.build();
return Optional.of(Sample.builder().setBody(statements).setRegionTag(regionTag).build());
return Optional.of(
Sample.builder()
.setBody(statements)
.setRegionTag(regionTag)

.build());
}

public static Optional<Sample> composeLroSettingsSample(
Expand Down Expand Up @@ -434,6 +439,11 @@ public static Optional<Sample> composeLroSettingsSample(
.setServiceName(classType.reference().name())
.setRpcName(methodNameOpt.get())
.build();
return Optional.of(Sample.builder().setBody(statements).setRegionTag(regionTag).build());
return Optional.of(
Sample.builder()
.setBody(statements)
.setRegionTag(regionTag)

.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public abstract class Sample {

public abstract boolean isCanonical();



public static Builder builder() {
return new AutoValue_Sample.Builder()
.setBody(ImmutableList.of())
Expand Down Expand Up @@ -88,6 +90,8 @@ public abstract static class Builder {

public abstract Builder setIsCanonical(boolean isCanonical);



abstract Builder setName(String name);

abstract Sample autoBuild();
Expand Down
Loading
Loading