Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2cb0d9f
draft version
summer-ji-eng Nov 10, 2020
2bf804e
Add unit test for long line
summer-ji-eng Nov 10, 2020
b0c6b47
Implement formatter for code snippet
summer-ji-eng Nov 10, 2020
94bd9ed
Add license and comment
summer-ji-eng Nov 10, 2020
209ec9d
Merge branch 'master' into sample_code_formatter
summer-ji-eng Nov 10, 2020
7176a85
Use string as input parameters
summer-ji-eng Nov 10, 2020
62da37b
remove unused dep
summer-ji-eng Nov 10, 2020
c9213eb
Merge branch 'master' into sample_code_formatter
summer-ji-eng Nov 11, 2020
07d9d21
draft version
summer-ji-eng Nov 10, 2020
55c7604
Add unit test for long line
summer-ji-eng Nov 10, 2020
9f1b49d
Implement formatter for code snippet
summer-ji-eng Nov 10, 2020
3126e5f
Add license and comment
summer-ji-eng Nov 10, 2020
308e6cf
Use string as input parameters
summer-ji-eng Nov 10, 2020
e2f99ab
remove unused dep
summer-ji-eng Nov 10, 2020
e2d2a40
move utils to composer/samplecode
summer-ji-eng Nov 11, 2020
875794f
Merge branch 'sample_code_formatter' of github.com:googleapis/gapic-g…
summer-ji-eng Nov 11, 2020
40977cf
remove utils files
summer-ji-eng Nov 11, 2020
ad842eb
revert BUILD in test
summer-ji-eng Nov 11, 2020
4426dd2
Merge branch 'master' into sample_code_formatter
summer-ji-eng Nov 12, 2020
2cfa86f
Merge branch 'master' into sample_code_formatter
summer-ji-eng Nov 12, 2020
222c910
Add comment and rephrase static string
summer-ji-eng Nov 13, 2020
7ca32d8
simplify the wrapper
summer-ji-eng Nov 13, 2020
4f91c12
Merge branch 'master' into sample_code_formatter
summer-ji-eng Nov 13, 2020
ef1a81d
Merge branch 'master' into sample_code_formatter
summer-ji-eng Nov 13, 2020
67a66e5
compose sample code for settings and stub settings
summer-ji-eng Nov 11, 2020
dadd3b8
Pick first pure unary method
summer-ji-eng Nov 11, 2020
219fca1
resolve merge master
summer-ji-eng Nov 13, 2020
efcd9d8
Add license for new file
summer-ji-eng Nov 13, 2020
dd1c881
pick first unary rpc for setting class
summer-ji-eng Nov 13, 2020
cbeabad
Add comment for method pick
summer-ji-eng Nov 13, 2020
0773a0c
add endpoint sample code
summer-ji-eng Nov 12, 2020
7a8f247
Add client variable
summer-ji-eng Nov 13, 2020
c3d81ef
Complete implementing credentials sample in Service Client Class comm…
summer-ji-eng Nov 13, 2020
8fd0e67
remove a method not has been implemented
summer-ji-eng Nov 13, 2020
006de4e
Implement service client class header credentials sample code
summer-ji-eng Nov 13, 2020
d271aa2
Add license in new files
summer-ji-eng Nov 13, 2020
8221e78
add static for helper functions
summer-ji-eng Nov 13, 2020
619bad5
rewrite a helper function
summer-ji-eng Nov 13, 2020
3d8e931
Implement sample code of set endpoint in service class comment header
summer-ji-eng Nov 13, 2020
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 @@ -15,8 +15,10 @@ java_library(
deps = [
"//:service_config_java_proto",
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/engine/writer",
"//src/main/java/com/google/api/generator/gapic:status_java_proto",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/composer/samplecode",
"//src/main/java/com/google/api/generator/gapic/utils",
"@com_google_api_api_common//jar",
"@com_google_api_gax_java//gax",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public GapicClass generate(Service service, Map<String, Message> messageTypes) {
ClassDefinition classDef =
ClassDefinition.builder()
.setHeaderCommentStatements(
ServiceClientCommentComposer.createClassHeaderComments(service))
ServiceClientCommentComposer.createClassHeaderComments(service, types))
.setPackageString(pakkage)
.setAnnotations(createClassAnnotations(types))
.setScope(ScopeNode.PUBLIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.JavaDocComment;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.gapic.composer.samplecode.ServiceClientCommentSampleCodeComposer;
import com.google.api.generator.gapic.model.Method;
import com.google.api.generator.gapic.model.MethodArgument;
import com.google.api.generator.gapic.model.Service;
Expand All @@ -25,6 +26,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -103,7 +105,8 @@ class ServiceClientCommentComposer {
"Returns the OperationsClient that can be used to query the status of a long-running"
+ " operation returned by another API method call.");

static List<CommentStatement> createClassHeaderComments(Service service) {
static List<CommentStatement> createClassHeaderComments(
Service service, Map<String, TypeNode> types) {
JavaDocComment.Builder classHeaderJavadocBuilder = JavaDocComment.builder();
if (service.hasDescription()) {
classHeaderJavadocBuilder =
Expand Down Expand Up @@ -134,9 +137,13 @@ static List<CommentStatement> createClassHeaderComments(Service service) {
SERVICE_DESCRIPTION_CUSTOMIZE_SUMMARY_PATTERN,
String.format("%sSettings", JavaStyle.toUpperCamelCase(service.name()))));
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_CREDENTIALS_SUMMARY_STRING);
// TODO(summerji): Add credentials' customization sample code here.
classHeaderJavadocBuilder.addSampleCode(
ServiceClientCommentSampleCodeComposer.composeClassHeaderCredentialsSampleCode(
service, types));
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_ENDPOINT_SUMMARY_STRING);
// TODO(summerji): Add endpoint customization sample code here.
classHeaderJavadocBuilder.addSampleCode(
ServiceClientCommentSampleCodeComposer.composeClassHeaderEndpointSampleCode(service, types)
);

return Arrays.asList(
CommentComposer.AUTO_GENERATED_CLASS_COMMENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.google.api.generator.gapic.model.GapicClass.Kind;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.Method;
import com.google.api.generator.gapic.model.Method.Stream;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -101,7 +102,7 @@ public GapicClass generate(Service service, Map<String, Message> ignore) {
ClassDefinition classDef =
ClassDefinition.builder()
.setPackageString(pakkage)
.setHeaderCommentStatements(createClassHeaderComments(service))
.setHeaderCommentStatements(createClassHeaderComments(service, types.get(className)))
.setAnnotations(createClassAnnotations())
.setScope(ScopeNode.PUBLIC)
.setName(className)
Expand All @@ -119,11 +120,21 @@ public GapicClass generate(Service service, Map<String, Message> ignore) {
return GapicClass.create(kind, classDef);
}

private static List<CommentStatement> createClassHeaderComments(Service service) {
private static List<CommentStatement> createClassHeaderComments(
Service service, TypeNode classType) {
// Pick the first pure unary rpc method, if no such method exist, then pick the first in the
// list.
Optional<Method> methodOpt =
service.methods().isEmpty() ? Optional.empty() : Optional.of(service.methods().get(0));
service.methods().isEmpty()
? Optional.empty()
: Optional.of(
service.methods().stream()
.reduce(
(m1, m2) ->
(m1.stream() == Stream.NONE && !m1.hasLro() && !m1.isPaged()) ? m1 : m2)
.get());
return SettingsCommentComposer.createClassHeaderComments(
getClientClassName(service.name()), service.defaultHost(), methodOpt);
getClientClassName(service.name()), service.defaultHost(), methodOpt, classType);
}

private static List<AnnotationNode> createClassAnnotations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import com.google.api.generator.gapic.model.GapicServiceConfig;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.model.Method;
import com.google.api.generator.gapic.model.Method.Stream;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -163,7 +164,7 @@ public GapicClass generate(
ClassDefinition classDef =
ClassDefinition.builder()
.setPackageString(pakkage)
.setHeaderCommentStatements(createClassHeaderComments(service))
.setHeaderCommentStatements(createClassHeaderComments(service, types.get(className)))
.setAnnotations(createClassAnnotations())
.setScope(ScopeNode.PUBLIC)
.setName(className)
Expand All @@ -187,11 +188,21 @@ private static List<AnnotationNode> createClassAnnotations() {
.build());
}

private static List<CommentStatement> createClassHeaderComments(Service service) {
private static List<CommentStatement> createClassHeaderComments(
Service service, TypeNode classType) {
// Pick the first pure unary rpc method, if no such method exist, then pick the first in the
// list.
Optional<Method> methodOpt =
service.methods().isEmpty() ? Optional.empty() : Optional.of(service.methods().get(0));
service.methods().isEmpty()
? Optional.empty()
: Optional.of(
service.methods().stream()
.reduce(
(m1, m2) ->
(m1.stream() == Stream.NONE && !m1.hasLro() && !m1.isPaged()) ? m1 : m2)
.get());
return SettingsCommentComposer.createClassHeaderComments(
String.format(STUB_PATTERN, service.name()), service.defaultHost(), methodOpt);
String.format(STUB_PATTERN, service.name()), service.defaultHost(), methodOpt, classType);
}

private static TypeNode createExtendsType(Service service, Map<String, TypeNode> types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.JavaDocComment;
import com.google.api.generator.engine.ast.LineComment;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.gapic.composer.samplecode.SettingsCommentSampleCodeComposer;
import com.google.api.generator.gapic.model.Method;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.common.base.Preconditions;
Expand All @@ -28,6 +30,8 @@
class SettingsCommentComposer {
private static final String COLON = ":";

private static final String CLIENT_CLASS_NAME_PATTERN = "%sClient";
private static final String STUB_PATTERN = "%sStub";
private static final String BUILDER_CLASS_DOC_PATTERN = "Builder for %s.";
private static final String CALL_SETTINGS_METHOD_DOC_PATTERN =
"Returns the object with the settings used for calls to %s.";
Expand Down Expand Up @@ -102,7 +106,10 @@ static CommentStatement createCallSettingsBuilderGetterComment(String javaMethod
}

static List<CommentStatement> createClassHeaderComments(
String configuredClassName, String defaultHost, Optional<Method> methodOpt) {
String configuredClassName,
String defaultHost,
Optional<Method> methodOpt,
TypeNode classType) {
// Split default address and port.
int colonIndex = defaultHost.indexOf(COLON);
Preconditions.checkState(
Expand All @@ -127,12 +134,16 @@ static List<CommentStatement> createClassHeaderComments(
.addParagraph(CLASS_HEADER_BUILDER_DESCRIPTION);

if (methodOpt.isPresent()) {
String sampleCode =
SettingsCommentSampleCodeComposer.composeSettingClassHeaderSampleCode(
methodOpt.get(), classType);
javaDocCommentBuilder =
javaDocCommentBuilder.addParagraph(
String.format(
CLASS_HEADER_SAMPLE_CODE_PATTERN,
JavaStyle.toLowerCamelCase(methodOpt.get().name())));
// TODO(summerji): Add sample code here.
javaDocCommentBuilder
.addParagraph(
String.format(
CLASS_HEADER_SAMPLE_CODE_PATTERN,
JavaStyle.toLowerCamelCase(methodOpt.get().name())))
.addSampleCode(sampleCode);
}

return Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ java_library(
":samplecode_files",
],
deps = [
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/engine/writer",
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/utils",
"@google_java_format_all_deps//jar",
"@com_google_api_gax_java//gax",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.StringObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.engine.writer.JavaWriterVisitor;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

public class ServiceClientCommentSampleCodeComposer {

private static final String SETTINGS_NAME_PATTERN = "%sSettings";
private static final String CLASS_NAME_PATTERN = "%sClient";

public static String composeClassHeaderCredentialsSampleCode(Service service, Map<String, TypeNode> types) {
String settingsVarName = JavaStyle.toLowerCamelCase(getSettingsName(service.name()));
TypeNode settingsVarType = types.get(getSettingsName(service.name()));
VariableExpr settingsVarExpr = VariableExpr.withVariable(
Variable.builder()
.setName(settingsVarName)
.setType(settingsVarType)
.build());
MethodInvocationExpr newBuilderMethodExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(settingsVarType)
.setMethodName("newBuilder")
.build();
TypeNode fixedCredentialProvideType = TypeNode.withReference(
ConcreteReference.withClazz(FixedCredentialsProvider.class)
);
MethodInvocationExpr credentialArgExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(fixedCredentialProvideType)
.setArguments(ValueExpr.withValue(StringObjectValue.withValue("myCredentials")))
.setMethodName("create")
.build();
MethodInvocationExpr credentialsMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(newBuilderMethodExpr)
.setArguments(credentialArgExpr)
.setMethodName("setCredentialsProvider")
.build();
MethodInvocationExpr buildMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(credentialsMethodExpr)
.setReturnType(settingsVarType)
.setMethodName("build")
.build();

Expr initSettingsVarExpr =
AssignmentExpr.builder()
.setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(buildMethodExpr)
.build();

String className = JavaStyle.toLowerCamelCase(getClientClassName(service.name()));
TypeNode classType = types.get(getClientClassName(service.name()));
VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(className).setType(classType).build());
MethodInvocationExpr createMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(classType).setArguments(settingsVarExpr).setMethodName("create").setReturnType(classType).build();
Expr initClientVarExpr = AssignmentExpr.builder()
.setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(createMethodExpr)
.build();

return writeSampleCode(Arrays.asList(initSettingsVarExpr, initClientVarExpr));
}

public static String composeClassHeaderEndpointSampleCode(Service service, Map<String, TypeNode> types) {
String settingsVarName = JavaStyle.toLowerCamelCase(getSettingsName(service.name()));
TypeNode settingsVarType = types.get(getSettingsName(service.name()));
VariableExpr settingsVarExpr = VariableExpr.withVariable(
Variable.builder()
.setName(settingsVarName)
.setType(settingsVarType)
.build());
MethodInvocationExpr newBuilderMethodExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(settingsVarType)
.setMethodName("newBuilder")
.build();
MethodInvocationExpr credentialsMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(newBuilderMethodExpr)
.setArguments(ValueExpr.withValue(StringObjectValue.withValue("myEndpoint")))
.setMethodName("setEndpoint")
.build();
MethodInvocationExpr buildMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(credentialsMethodExpr)
.setReturnType(settingsVarType)
.setMethodName("build")
.build();

Expr initSettingsVarExpr =
AssignmentExpr.builder()
.setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(buildMethodExpr)
.build();

String className = JavaStyle.toLowerCamelCase(getClientClassName(service.name()));
TypeNode classType = types.get(getClientClassName(service.name()));
VariableExpr clientVarExpr = VariableExpr.withVariable(Variable.builder().setName(className).setType(classType).build());
MethodInvocationExpr createMethodExpr = MethodInvocationExpr.builder().setStaticReferenceType(classType).setArguments(settingsVarExpr).setMethodName("create").setReturnType(classType).build();
Expr initClientVarExpr = AssignmentExpr.builder()
.setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(createMethodExpr)
.build();

return writeSampleCode(Arrays.asList(
initSettingsVarExpr, initClientVarExpr
));
}

private static String getClientClassName(String serviceName) {
return String.format(CLASS_NAME_PATTERN, serviceName);
}

private static String getSettingsName(String serviceName) {
return String.format(SETTINGS_NAME_PATTERN, serviceName);
}

private static String writeSampleCode(List<Expr> exprs) {
List<Statement> statements =
exprs
.stream()
.map(e -> ExprStatement.withExpr(e))
.collect(Collectors.toList());
JavaWriterVisitor visitor = new JavaWriterVisitor();
for (Statement statement : statements) {
statement.accept(visitor);
}
return SampleCodeJavaFormatter.format(visitor.write());
}
}
Loading