Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inline disclaimer comment, use Sample/RegionTag, collect GapicClass samples (pt 2) #934

Closed
wants to merge 19 commits into from
Closed
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 @@ -14,6 +14,7 @@

package com.google.api.generator.engine.ast;

import com.google.api.generator.gapic.model.RegionTag;
import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand All @@ -26,6 +27,9 @@
public abstract class ClassDefinition implements AstNode {
// Optional.
public abstract ImmutableList<CommentStatement> fileHeader();
// Required for samples classes.
@Nullable
public abstract RegionTag regionTag();
// Required.
public abstract ScopeNode scope();
// Required.
Expand Down Expand Up @@ -92,6 +96,8 @@ public Builder setFileHeader(CommentStatement... headerComments) {

public abstract Builder setFileHeader(List<CommentStatement> fileHeader);

public abstract Builder setRegionTag(RegionTag regionTag);

public Builder setHeaderCommentStatements(CommentStatement... comments) {
return setHeaderCommentStatements(Arrays.asList(comments));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.engine.ast.WhileStatement;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -357,13 +356,7 @@ public void visit(TryCatchStatement tryCatchStatement) {
if (tryCatchStatement.tryResourceExpr() != null) {
tryCatchStatement.tryResourceExpr().accept(this);
}

statements(tryCatchStatement.tryBody());

Preconditions.checkState(
!tryCatchStatement.isSampleCode() && !tryCatchStatement.catchVariableExprs().isEmpty(),
"Import generation should not be invoked on sample code, but was found when visiting a"
+ " try-catch block");
for (int i = 0; i < tryCatchStatement.catchVariableExprs().size(); i++) {
tryCatchStatement.catchVariableExprs().get(i).accept(this);
statements(tryCatchStatement.catchBlocks().get(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.engine.ast.WhileStatement;
import com.google.api.generator.gapic.model.RegionTag;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -910,6 +911,15 @@ public void visit(ClassDefinition classDefinition) {
newline();
}

String regionTagReplace = "REPLACE_REGION_TAG";
if (classDefinition.regionTag() != null) {
statements(
Arrays.asList(
classDefinition
.regionTag()
.generateTag(RegionTag.RegionTagRegion.START, regionTagReplace)));
}

// This must go first, so that we can check for type collisions.
classDefinition.accept(importWriterVisitor);
if (!classDefinition.isNested()) {
Expand Down Expand Up @@ -974,10 +984,27 @@ public void visit(ClassDefinition classDefinition) {
classes(classDefinition.nestedClasses());

rightBrace();
if (classDefinition.regionTag() != null) {
statements(
Arrays.asList(
classDefinition
.regionTag()
.generateTag(RegionTag.RegionTagRegion.END, regionTagReplace)));
}

// We should have valid Java by now, so format it.
if (!classDefinition.isNested()) {
buffer.replace(0, buffer.length(), JavaFormatter.format(buffer.toString()));
String formattedClazz = JavaFormatter.format(buffer.toString());

// fixing region tag after formatting
// formatter splits long region tags on multiple lines and moves the end tag up - doesn't meet
// tag requirements
if (classDefinition.regionTag() != null) {
formattedClazz =
formattedClazz.replaceAll(regionTagReplace, classDefinition.regionTag().generate());
formattedClazz = formattedClazz.replaceAll("} // \\[END", "}\n// \\[END");
}
buffer.replace(0, buffer.length(), formattedClazz);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import com.google.api.generator.engine.ast.PackageInfoDefinition;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.gapic.composer.samplecode.SampleCodeWriter;
import com.google.api.generator.gapic.composer.samplecode.ServiceClientSampleCodeComposer;
import com.google.api.generator.gapic.composer.utils.ClassNames;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
import com.google.api.generator.gapic.model.Sample;
import com.google.api.generator.gapic.model.Service;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -119,10 +121,11 @@ private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
.setPakkage(service.pakkage())
.setName(ClassNames.getServiceClientClassName(service))
.build());
String packageInfoSampleCode =
Sample packageInfoSampleCode =
ServiceClientSampleCodeComposer.composeClassHeaderMethodSampleCode(
service, clientType, context.resourceNames(), context.messages());
javaDocCommentBuilder.addSampleCode(packageInfoSampleCode);
javaDocCommentBuilder.addSampleCode(
SampleCodeWriter.writeInlineSample(packageInfoSampleCode.body()));
}

return CommentStatement.withComment(javaDocCommentBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.google.api.generator.engine.ast.BlockComment;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.LineComment;
import com.google.api.generator.engine.ast.Statement;
import java.util.Arrays;
import java.util.List;

public class CommentComposer {
private static final String APACHE_LICENSE_STRING =
Expand Down Expand Up @@ -52,4 +55,13 @@ public class CommentComposer {
public static final CommentStatement AUTO_GENERATED_METHOD_COMMENT =
CommentStatement.withComment(
LineComment.withComment(AUTO_GENERATED_METHOD_DISCLAIMER_STRING));

public static final List<Statement> AUTO_GENERATED_SAMPLE_COMMENT =
Arrays.asList(
CommentStatement.withComment(
LineComment.withComment(
"This snippet has been automatically generated for illustrative purposes only.")),
CommentStatement.withComment(
LineComment.withComment(
"It may require modifications to work in your environment.")));
}
Loading