Skip to content

Commit

Permalink
Add comments in composer, add TODOs, refactor the signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
summer-ji-eng committed Nov 17, 2020
1 parent cdd6511 commit b1f948f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
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 @@ -106,6 +110,10 @@ class ServiceClientCommentComposer {

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 @@ -137,7 +145,8 @@ static List<CommentStatement> createClassHeaderComments(
String.format("%sSettings", JavaStyle.toUpperCamelCase(service.name()))));
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_CREDENTIALS_SUMMARY_STRING);
classHeaderJavadocBuilder.addSampleCode(
ServiceClientSampleCodeComposer.composeClassHeaderCredentialsSampleCode(service, types));
ServiceClientSampleCodeComposer.composeClassHeaderCredentialsSampleCode(
clientName, clientType, settingsName, settingsType));
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_ENDPOINT_SUMMARY_STRING);
classHeaderJavadocBuilder.addSampleCode(
ServiceClientSampleCodeComposer.composeClassHeaderEndpointSampleCode(service, types));
Expand Down Expand Up @@ -258,4 +267,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
Expand Up @@ -28,25 +28,22 @@
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 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.Map;
import java.util.stream.Collectors;

public class ServiceClientSampleCodeComposer {
private static final String SETTINGS_NAME_PATTERN = "%sSettings";
private static final String CLASS_NAME_PATTERN = "%sClient";
//TODO(summerji): Add unit tests for ServiceClientSampleCodeComposer.

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 = createVariableExpr(settingsVarName, settingsVarType);
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(settingsVarType)
.setStaticReferenceType(settingsType)
.setMethodName("newBuilder")
.build();
TypeNode fixedCredentialProvideType =
Expand All @@ -66,7 +63,7 @@ public static String composeClassHeaderCredentialsSampleCode(
MethodInvocationExpr buildMethodExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(credentialsMethodExpr)
.setReturnType(settingsVarType)
.setReturnType(settingsType)
.setMethodName("build")
.build();
Expr initSettingsVarExpr =
Expand All @@ -75,8 +72,8 @@ public static String composeClassHeaderCredentialsSampleCode(
.setValueExpr(buildMethodExpr)
.build();

String clientName = JavaStyle.toLowerCamelCase(getClientClassName(service.name()));
TypeNode clientType = types.get(getClientClassName(service.name()));
// Initialized client with create() method.
// e.g. EchoClient echoClient = EchoClient.create(echoSettings);
VariableExpr clientVarExpr = createVariableExpr(clientName, clientType);
MethodInvocationExpr createMethodExpr =
MethodInvocationExpr.builder()
Expand Down Expand Up @@ -143,15 +140,7 @@ public static String composeClassHeaderEndpointSampleCode(
}

// ======================================== Helpers ==========================================//

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);
}

// 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());
Expand Down

0 comments on commit b1f948f

Please sign in to comment.