Skip to content
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 @@ -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,7 +137,9 @@ 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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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));
}

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ import javax.annotation.Generated;
*
* <p>To customize credentials:
*
* <pre><code>
* EchoSettings echoSettings =
* EchoSettings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .build();
* EchoClient echoClient = EchoClient.create(echoSettings);
* </code></pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ import javax.annotation.Generated;
*
* <p>To customize credentials:
*
* <pre><code>
* IdentitySettings identitySettings =
* IdentitySettings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .build();
* IdentityClient identityClient = IdentityClient.create(identitySettings);
* </code></pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down
8 changes: 8 additions & 0 deletions test/integration/goldens/asset/AssetServiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
*
* <p>To customize credentials:
*
* <pre><code>
* AssetServiceSettings assetServiceSettings =
* AssetServiceSettings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .build();
* AssetServiceClient assetServiceClient = AssetServiceClient.create(assetServiceSettings);
* </code></pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down
9 changes: 9 additions & 0 deletions test/integration/goldens/logging/ConfigServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
*
* <p>To customize credentials:
*
* <pre><code>
* ConfigServiceV2Settings configServiceV2Settings =
* ConfigServiceV2Settings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .build();
* ConfigServiceV2Client configServiceV2Client =
* ConfigServiceV2Client.create(configServiceV2Settings);
* </code></pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down
9 changes: 9 additions & 0 deletions test/integration/goldens/logging/LoggingServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
*
* <p>To customize credentials:
*
* <pre><code>
* LoggingServiceV2Settings loggingServiceV2Settings =
* LoggingServiceV2Settings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .build();
* LoggingServiceV2Client loggingServiceV2Client =
* LoggingServiceV2Client.create(loggingServiceV2Settings);
* </code></pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down
9 changes: 9 additions & 0 deletions test/integration/goldens/logging/MetricsServiceV2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
*
* <p>To customize credentials:
*
* <pre><code>
* MetricsServiceV2Settings metricsServiceV2Settings =
* MetricsServiceV2Settings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .build();
* MetricsServiceV2Client metricsServiceV2Client =
* MetricsServiceV2Client.create(metricsServiceV2Settings);
* </code></pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down
8 changes: 8 additions & 0 deletions test/integration/goldens/redis/CloudRedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@
*
* <p>To customize credentials:
*
* <pre><code>
* CloudRedisSettings cloudRedisSettings =
* CloudRedisSettings.newBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials"))
* .build();
* CloudRedisClient cloudRedisClient = CloudRedisClient.create(cloudRedisSettings);
* </code></pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down