From 0773a0cb4921bd04462da60db3039a42eccfd0ec Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 15:57:22 -0800 Subject: [PATCH 1/8] add endpoint sample code --- .../composer/ServiceClientClassComposer.java | 2 +- .../ServiceClientCommentComposer.java | 6 +- .../gapic/composer/samplecode/BUILD.bazel | 1 + ...erviceClientCommentSampleCodeComposer.java | 99 +++++++++++++++++++ .../SettingsCommentSampleCodeComposer.java | 2 +- 5 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java index 02ff960025..8ed7331724 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java @@ -119,7 +119,7 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() .setHeaderCommentStatements( - ServiceClientCommentComposer.createClassHeaderComments(service)) + ServiceClientCommentComposer.createClassHeaderComments(service, types)) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java index 1a0f8f2b0f..275af3672e 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java @@ -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; @@ -27,6 +28,7 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.Map; class ServiceClientCommentComposer { // Tokens. @@ -103,7 +105,7 @@ 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 createClassHeaderComments(Service service) { + static List createClassHeaderComments(Service service, Map types) { JavaDocComment.Builder classHeaderJavadocBuilder = JavaDocComment.builder(); if (service.hasDescription()) { classHeaderJavadocBuilder = @@ -134,7 +136,7 @@ static List 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. diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/BUILD.bazel b/src/main/java/com/google/api/generator/gapic/composer/samplecode/BUILD.bazel index 54f203fff4..9dcaf7ab63 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/BUILD.bazel +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/BUILD.bazel @@ -18,5 +18,6 @@ java_library( "//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", ], ) diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java new file mode 100644 index 0000000000..0c2737e97d --- /dev/null +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java @@ -0,0 +1,99 @@ +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.Method; +import com.google.api.generator.gapic.model.Service; +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 composeClassHeaderMethodSampleCode() { + // TODO(summerji): implement class header rpc sample code. + return "string holder"; + } + + public static String composeClassHeaderCredentialsSampleCode(Service service, Map types) { + String settingsVarName = getSettingsName(service.name()); + TypeNode settingsVarType = types.get(settingsVarName); + 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 initLocalSettingsVarExpr = + AssignmentExpr.builder() + .setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()) + .setValueExpr(buildMethodExpr) + .build(); + + List statements = + Arrays.asList( + initLocalSettingsVarExpr + ) + .stream() + .map(e -> ExprStatement.withExpr(e)) + .collect(Collectors.toList()); + return SampleCodeJavaFormatter.format(writeStatements(statements)); + } + + 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 writeStatements(List statements) { + JavaWriterVisitor visitor = new JavaWriterVisitor(); + for (Statement statement : statements) { + statement.accept(visitor); + } + return visitor.write(); + } +} diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/SettingsCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/SettingsCommentSampleCodeComposer.java index 6c27ec9fd8..f8fdc4b787 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/SettingsCommentSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/SettingsCommentSampleCodeComposer.java @@ -34,7 +34,7 @@ import java.util.List; import java.util.stream.Collectors; -public final class SettingsCommentSampleCodeComposer { +public class SettingsCommentSampleCodeComposer { private static final String BUILDER_NAME_PATTERN = "%sBuilder"; private static final String STUB = "Stub"; From 7a8f247139a4674249937b3198b42aa24820655f Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 17:01:10 -0800 Subject: [PATCH 2/8] Add client variable --- .../composer/ServiceClientCommentComposer.java | 9 ++++++--- .../ServiceClientCommentSampleCodeComposer.java | 13 ++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java index 275af3672e..9aec8b8184 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientCommentComposer.java @@ -26,9 +26,9 @@ 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; -import java.util.Map; class ServiceClientCommentComposer { // Tokens. @@ -105,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 createClassHeaderComments(Service service, Map types) { + static List createClassHeaderComments( + Service service, Map types) { JavaDocComment.Builder classHeaderJavadocBuilder = JavaDocComment.builder(); if (service.hasDescription()) { classHeaderJavadocBuilder = @@ -136,7 +137,9 @@ static List createClassHeaderComments(Service service, Map statements = Arrays.asList( - initLocalSettingsVarExpr + initLocalSettingsVarExpr, + initClientVarExpr ) .stream() .map(e -> ExprStatement.withExpr(e)) From c3d81ef65a9a0a8cc60d2058fc360194f32bedbc Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 20:18:28 -0800 Subject: [PATCH 3/8] Complete implementing credentials sample in Service Client Class comment header --- .../ServiceClientCommentSampleCodeComposer.java | 10 +++++----- .../generator/gapic/composer/goldens/EchoClient.golden | 8 ++++++++ .../gapic/composer/goldens/IdentityClient.golden | 8 ++++++++ test/integration/goldens/asset/AssetServiceClient.java | 8 ++++++++ .../goldens/logging/ConfigServiceV2Client.java | 9 +++++++++ .../goldens/logging/LoggingServiceV2Client.java | 9 +++++++++ .../goldens/logging/MetricsServiceV2Client.java | 9 +++++++++ test/integration/goldens/redis/CloudRedisClient.java | 8 ++++++++ 8 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java index b9ee2a0a4f..e9d5e0a234 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java @@ -28,12 +28,12 @@ public class ServiceClientCommentSampleCodeComposer { public static String composeClassHeaderMethodSampleCode() { // TODO(summerji): implement class header rpc sample code. - return "string holder"; + return ""; } public static String composeClassHeaderCredentialsSampleCode(Service service, Map types) { - String settingsVarName = getSettingsName(service.name()); - TypeNode settingsVarType = types.get(settingsVarName); + String settingsVarName = JavaStyle.toLowerCamelCase(getSettingsName(service.name())); + TypeNode settingsVarType = types.get(getSettingsName(service.name())); VariableExpr settingsVarExpr = VariableExpr.withVariable( Variable.builder() .setName(settingsVarName) @@ -72,8 +72,8 @@ public static String composeClassHeaderCredentialsSampleCode(Service service, Ma .setValueExpr(buildMethodExpr) .build(); - String className = getClientClassName(service.name()); - TypeNode classType = types.get(className); + 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() diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden index 20a193853e..71d187905d 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/EchoClient.golden @@ -63,6 +63,14 @@ import javax.annotation.Generated; * *

To customize credentials: * + *


+ * EchoSettings echoSettings =
+ *     EchoSettings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
+ *         .build();
+ * EchoClient echoClient = EchoClient.create(echoSettings);
+ * 
+ * *

To customize the endpoint: */ @BetaApi diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden index 0f48757f2b..d238ecc2dd 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/IdentityClient.golden @@ -53,6 +53,14 @@ import javax.annotation.Generated; * *

To customize credentials: * + *


+ * IdentitySettings identitySettings =
+ *     IdentitySettings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
+ *         .build();
+ * IdentityClient identityClient = IdentityClient.create(identitySettings);
+ * 
+ * *

To customize the endpoint: */ @BetaApi diff --git a/test/integration/goldens/asset/AssetServiceClient.java b/test/integration/goldens/asset/AssetServiceClient.java index 103843b065..d2fe918308 100644 --- a/test/integration/goldens/asset/AssetServiceClient.java +++ b/test/integration/goldens/asset/AssetServiceClient.java @@ -75,6 +75,14 @@ * *

To customize credentials: * + *


+ * AssetServiceSettings assetServiceSettings =
+ *     AssetServiceSettings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
+ *         .build();
+ * AssetServiceClient assetServiceClient = AssetServiceClient.create(assetServiceSettings);
+ * 
+ * *

To customize the endpoint: */ @BetaApi diff --git a/test/integration/goldens/logging/ConfigServiceV2Client.java b/test/integration/goldens/logging/ConfigServiceV2Client.java index b6516172fc..a10e504f57 100644 --- a/test/integration/goldens/logging/ConfigServiceV2Client.java +++ b/test/integration/goldens/logging/ConfigServiceV2Client.java @@ -73,6 +73,15 @@ * *

To customize credentials: * + *


+ * ConfigServiceV2Settings configServiceV2Settings =
+ *     ConfigServiceV2Settings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
+ *         .build();
+ * ConfigServiceV2Client configServiceV2Client =
+ *     ConfigServiceV2Client.create(configServiceV2Settings);
+ * 
+ * *

To customize the endpoint: */ @BetaApi diff --git a/test/integration/goldens/logging/LoggingServiceV2Client.java b/test/integration/goldens/logging/LoggingServiceV2Client.java index d90cbfadf6..baa466e94b 100644 --- a/test/integration/goldens/logging/LoggingServiceV2Client.java +++ b/test/integration/goldens/logging/LoggingServiceV2Client.java @@ -75,6 +75,15 @@ * *

To customize credentials: * + *


+ * LoggingServiceV2Settings loggingServiceV2Settings =
+ *     LoggingServiceV2Settings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
+ *         .build();
+ * LoggingServiceV2Client loggingServiceV2Client =
+ *     LoggingServiceV2Client.create(loggingServiceV2Settings);
+ * 
+ * *

To customize the endpoint: */ @BetaApi diff --git a/test/integration/goldens/logging/MetricsServiceV2Client.java b/test/integration/goldens/logging/MetricsServiceV2Client.java index 19f6aef98d..a92299773e 100644 --- a/test/integration/goldens/logging/MetricsServiceV2Client.java +++ b/test/integration/goldens/logging/MetricsServiceV2Client.java @@ -72,6 +72,15 @@ * *

To customize credentials: * + *


+ * MetricsServiceV2Settings metricsServiceV2Settings =
+ *     MetricsServiceV2Settings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
+ *         .build();
+ * MetricsServiceV2Client metricsServiceV2Client =
+ *     MetricsServiceV2Client.create(metricsServiceV2Settings);
+ * 
+ * *

To customize the endpoint: */ @BetaApi diff --git a/test/integration/goldens/redis/CloudRedisClient.java b/test/integration/goldens/redis/CloudRedisClient.java index c807b6dd8a..01af3ad018 100644 --- a/test/integration/goldens/redis/CloudRedisClient.java +++ b/test/integration/goldens/redis/CloudRedisClient.java @@ -95,6 +95,14 @@ * *

To customize credentials: * + *


+ * CloudRedisSettings cloudRedisSettings =
+ *     CloudRedisSettings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
+ *         .build();
+ * CloudRedisClient cloudRedisClient = CloudRedisClient.create(cloudRedisSettings);
+ * 
+ * *

To customize the endpoint: */ @BetaApi From 8fd0e67094677e7b9b52b17c871391fff08d013f Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 20:54:59 -0800 Subject: [PATCH 4/8] remove a method not has been implemented --- .../samplecode/ServiceClientCommentSampleCodeComposer.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java index e9d5e0a234..3391f4114f 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java @@ -26,11 +26,6 @@ public class ServiceClientCommentSampleCodeComposer { private static final String SETTINGS_NAME_PATTERN = "%sSettings"; private static final String CLASS_NAME_PATTERN = "%sClient"; - public static String composeClassHeaderMethodSampleCode() { - // TODO(summerji): implement class header rpc sample code. - return ""; - } - public static String composeClassHeaderCredentialsSampleCode(Service service, Map types) { String settingsVarName = JavaStyle.toLowerCamelCase(getSettingsName(service.name())); TypeNode settingsVarType = types.get(getSettingsName(service.name())); From 006de4e81a58f460eb745278d35410cb2478065a Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 22:50:55 -0800 Subject: [PATCH 5/8] Implement service client class header credentials sample code --- .../samplecode/ServiceClientCommentSampleCodeComposer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java index 3391f4114f..b958a82c1e 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java @@ -87,15 +87,15 @@ public static String composeClassHeaderCredentialsSampleCode(Service service, Ma return SampleCodeJavaFormatter.format(writeStatements(statements)); } - private static String getClientClassName(String serviceName) { + private String getClientClassName(String serviceName) { return String.format(CLASS_NAME_PATTERN, serviceName); } - private static String getSettingsName(String serviceName) { + private String getSettingsName(String serviceName) { return String.format(SETTINGS_NAME_PATTERN, serviceName); } - private static String writeStatements(List statements) { + private String writeStatements(List statements) { JavaWriterVisitor visitor = new JavaWriterVisitor(); for (Statement statement : statements) { statement.accept(visitor); From d271aa27a87958a007b7dc30c71a401d907665c1 Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 22:55:47 -0800 Subject: [PATCH 6/8] Add license in new files --- .../ServiceClientCommentSampleCodeComposer.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java index b958a82c1e..fb601c6a22 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java @@ -1,3 +1,17 @@ +// 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; From 8221e788689600054ef713c122609f4c757ab38d Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 23:09:32 -0800 Subject: [PATCH 7/8] add static for helper functions --- .../samplecode/ServiceClientCommentSampleCodeComposer.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java index fb601c6a22..39aacba5f7 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java @@ -101,15 +101,15 @@ public static String composeClassHeaderCredentialsSampleCode(Service service, Ma return SampleCodeJavaFormatter.format(writeStatements(statements)); } - private String getClientClassName(String serviceName) { + private static String getClientClassName(String serviceName) { return String.format(CLASS_NAME_PATTERN, serviceName); } - private String getSettingsName(String serviceName) { + private static String getSettingsName(String serviceName) { return String.format(SETTINGS_NAME_PATTERN, serviceName); } - private String writeStatements(List statements) { + private static String writeStatements(List statements) { JavaWriterVisitor visitor = new JavaWriterVisitor(); for (Statement statement : statements) { statement.accept(visitor); From 619bad557221317d4e67e633c9a9256529d1807a Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 12 Nov 2020 23:27:12 -0800 Subject: [PATCH 8/8] rewrite a helper function --- ...erviceClientCommentSampleCodeComposer.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java index 39aacba5f7..57407e1c17 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCommentSampleCodeComposer.java @@ -27,7 +27,6 @@ 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.Method; import com.google.api.generator.gapic.model.Service; import com.google.api.generator.gapic.utils.JavaStyle; import java.util.Arrays; @@ -75,7 +74,7 @@ public static String composeClassHeaderCredentialsSampleCode(Service service, Ma .setMethodName("build") .build(); - Expr initLocalSettingsVarExpr = + Expr initSettingsVarExpr = AssignmentExpr.builder() .setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()) .setValueExpr(buildMethodExpr) @@ -90,15 +89,7 @@ public static String composeClassHeaderCredentialsSampleCode(Service service, Ma .setValueExpr(createMethodExpr) .build(); - List statements = - Arrays.asList( - initLocalSettingsVarExpr, - initClientVarExpr - ) - .stream() - .map(e -> ExprStatement.withExpr(e)) - .collect(Collectors.toList()); - return SampleCodeJavaFormatter.format(writeStatements(statements)); + return writeSampleCode(Arrays.asList(initSettingsVarExpr, initClientVarExpr)); } private static String getClientClassName(String serviceName) { @@ -109,11 +100,16 @@ private static String getSettingsName(String serviceName) { return String.format(SETTINGS_NAME_PATTERN, serviceName); } - private static String writeStatements(List statements) { + private static String writeSampleCode(List exprs) { + List statements = + exprs + .stream() + .map(e -> ExprStatement.withExpr(e)) + .collect(Collectors.toList()); JavaWriterVisitor visitor = new JavaWriterVisitor(); for (Statement statement : statements) { statement.accept(visitor); } - return visitor.write(); + return SampleCodeJavaFormatter.format(visitor.write()); } }