Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -15,9 +15,11 @@ 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/utils",
"//src/main/java/com/google/api/generator/gapic/composer/samplecode",
"@com_google_api_api_common//jar",
"@com_google_api_gax_java//gax",
"@com_google_api_gax_java//gax-grpc:gax_grpc",
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 @@ -25,10 +25,15 @@
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;

class ServiceClientCommentComposer {
// Name Pattern.
private static final String SETTINGS_NAME_PATTERN = "%sSettings";
private static final String CLASS_NAME_PATTERN = "%sClient";

// Tokens.
private static final String COLON = ":";
private static final String EMPTY_STRING = "";
Expand Down Expand Up @@ -103,7 +108,12 @@ 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) {
String settingsName = JavaStyle.toLowerCamelCase(getSettingsName(service.name()));
String clientName = JavaStyle.toLowerCamelCase(getClientClassName(service.name()));
TypeNode settingsType = types.get(getSettingsName(service.name()));
TypeNode clientType = types.get(getClientClassName(service.name()));
JavaDocComment.Builder classHeaderJavadocBuilder = JavaDocComment.builder();
if (service.hasDescription()) {
classHeaderJavadocBuilder =
Expand Down Expand Up @@ -134,7 +144,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(
ServiceClientSampleCodeComposer.composeClassHeaderCredentialsSampleCode(
clientName, clientType, settingsName, settingsType));
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_ENDPOINT_SUMMARY_STRING);
// TODO(summerji): Add endpoint customization sample code here.

Expand Down Expand Up @@ -254,4 +266,12 @@ private static JavaDocComment.Builder processProtobufComment(

return commentBuilder;
}

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

private static String getClientClassName(String serviceName) {
return String.format(CLASS_NAME_PATTERN, serviceName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// 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;

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.composer.samplecode.SampleCodeJavaFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ServiceClientSampleCodeComposer {
// TODO(summerji): Add unit tests for ServiceClientSampleCodeComposer.

public static String composeClassHeaderCredentialsSampleCode(
String clientName, TypeNode clientType, String settingsName, TypeNode settingsType) {
// Initialize clientSettings with builder() method.
// e.g. EchoSettings echoSettings =
// EchoSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create("myCredentials")).build();
VariableExpr settingsVarExpr = createVariableExpr(settingsName, settingsType);
MethodInvocationExpr newBuilderMethodExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(settingsType)
.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(settingsType)
.setMethodName("build")
.build();
Expr initSettingsVarExpr =
AssignmentExpr.builder()
.setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(buildMethodExpr)
.build();

// Initialized client with create() method.
// e.g. EchoClient echoClient = EchoClient.create(echoSettings);
VariableExpr clientVarExpr = createVariableExpr(clientName, clientType);
MethodInvocationExpr createMethodExpr =
MethodInvocationExpr.builder()
.setStaticReferenceType(clientType)
.setArguments(settingsVarExpr)
.setMethodName("create")
.setReturnType(clientType)
.build();
Expr initClientVarExpr =
AssignmentExpr.builder()
.setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build())
.setValueExpr(createMethodExpr)
.build();

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

// ======================================== Helpers ==========================================//
// TODO(summerji): Use writeSampleCode method in new class once PR#499 merged.
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());
}

private static VariableExpr createVariableExpr(String variableName, TypeNode type) {
return VariableExpr.withVariable(
Variable.builder().setName(variableName).setType(type).build());
}
}
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);
* }</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"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed this - this should be some variable, not a string. Example here

* .build();
* IdentityClient identityClient = IdentityClient.create(identitySettings);
* }</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);
* }</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);
* }</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);
* }</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);
* }</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);
* }</pre>
*
* <p>To customize the endpoint:
*/
@BetaApi
Expand Down