Skip to content

Commit

Permalink
[ggj][codegen] feat: add comments to ServiceSettings (#275)
Browse files Browse the repository at this point in the history
* feat: parse batching descriptor fields

* feat: add Field.isMap and map-parsing test

* feat: add initial batching descriptor field to ServiceStubSettings

* feat: support '? extends Foo' wildcard bounded references

* fix: speed up precommit checks with --disk_cache

* feat: add splitException, count{Elements,Bytes} batching descriptor methods to ServiceStubSettings

* feat: add GeneralForStatement

* feat: add splitRseponse to batching desc. in ServiceStubSettings

* fix: output srcjar path

* feat: add method comments to ServiceStubSettings, fix AST formatting

* feat: add class Javadoc to ServiceStubSettings

* feat: add class Javadoc to ServiceStubSettings

* feat: add comments to ServiceSettings

* fix: file move
  • Loading branch information
miraleung authored Sep 4, 2020
1 parent bf7f2b0 commit 8f1c6f0
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ public ClassDefinition build() {

for (Statement statement : classDef.statements()) {
Preconditions.checkState(
statement instanceof ExprStatement
|| statement instanceof BlockStatement
|| statement instanceof CommentStatement,
statement instanceof CommentStatement
|| statement instanceof ExprStatement
|| statement instanceof BlockStatement,
"Class statement type must be either an expression, block, or comment statement");
if (statement instanceof ExprStatement) {
Expr expr = ((ExprStatement) statement).expression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.CastExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
Expand Down Expand Up @@ -61,9 +62,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -72,10 +73,14 @@
public class ServiceSettingsClassComposer implements ClassComposer {
private static final String BUILDER_CLASS_NAME = "Builder";
private static final String CLASS_NAME_PATTERN = "%sSettings";
private static final String CLIENT_CLASS_NAME_PATTERN = "%sClient";
private static final String CALL_SETTINGS_TYPE_NAME_PATTERN = "%sCallSettings";
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";
private static final String STUB_SETTINGS_PATTERN = "%sStubSettings";

private static final String OPERATION_SETTINGS_LITERAL = "OperationSettings";
private static final String SETTINGS_LITERAL = "Settings";

private static final ServiceSettingsClassComposer INSTANCE = new ServiceSettingsClassComposer();

private static final Map<String, TypeNode> staticTypes = createStaticTypes();
Expand All @@ -96,6 +101,7 @@ public GapicClass generate(Service service, Map<String, Message> ignore) {
ClassDefinition classDef =
ClassDefinition.builder()
.setPackageString(pakkage)
.setHeaderCommentStatements(createClassHeaderComments(service))
.setAnnotations(createClassAnnotations())
.setScope(ScopeNode.PUBLIC)
.setName(className)
Expand All @@ -113,6 +119,13 @@ public GapicClass generate(Service service, Map<String, Message> ignore) {
return GapicClass.create(kind, classDef);
}

private static List<CommentStatement> createClassHeaderComments(Service service) {
Optional<Method> methodOpt =
service.methods().isEmpty() ? Optional.empty() : Optional.of(service.methods().get(0));
return SettingsCommentComposer.createClassHeaderComments(
getClientClassName(service.name()), service.defaultHost(), methodOpt);
}

private static List<AnnotationNode> createClassAnnotations() {
return Arrays.asList(
AnnotationNode.builder()
Expand Down Expand Up @@ -162,6 +175,9 @@ private static List<MethodDefinition> createSettingsGetterMethods(
BiFunction<TypeNode, String, MethodDefinition> methodMakerFn =
(retType, methodName) ->
MethodDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsGetterComment(
getMethodNameFromSettingsVarName(methodName)))
.setScope(ScopeNode.PUBLIC)
.setReturnType(retType)
.setName(methodName)
Expand Down Expand Up @@ -242,9 +258,7 @@ private static MethodDefinition createCreatorMethod(

private static List<MethodDefinition> createDefaultGetterMethods(
Service service, Map<String, TypeNode> types) {
Map<String, TypeNode> javaMethodNameToReturnType = createDefaultMethodNamesToTypes();

BiFunction<String, TypeNode, MethodDefinition> methodMakerFn =
BiFunction<String, TypeNode, MethodDefinition.Builder> methodStarterFn =
(mName, retType) ->
MethodDefinition.builder()
.setScope(ScopeNode.PUBLIC)
Expand All @@ -256,20 +270,59 @@ private static List<MethodDefinition> createDefaultGetterMethods(
.setStaticReferenceType(types.get(getStubSettingsClassName(service.name())))
.setMethodName(mName)
.setReturnType(retType)
.build())
.build();
.build());
BiFunction<MethodDefinition.Builder, CommentStatement, MethodDefinition> methodMakerFn =
(methodDefBuilder, comment) -> methodDefBuilder.setHeaderCommentStatements(comment).build();
Function<Class, TypeNode> typeMakerFn =
c -> TypeNode.withReference(ConcreteReference.withClazz(c));

List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.addAll(
javaMethodNameToReturnType.entrySet().stream()
.map(e -> methodMakerFn.apply(e.getKey(), e.getValue()))
.collect(Collectors.toList()));
javaMethods.add(
methodMakerFn
methodMakerFn.apply(
methodStarterFn.apply(
"defaultExecutorProviderBuilder",
typeMakerFn.apply(InstantiatingExecutorProvider.Builder.class)),
SettingsCommentComposer.DEFAULT_EXECUTOR_PROVIDER_BUILDER_METHOD_COMMENT));
javaMethods.add(
methodMakerFn.apply(
methodStarterFn.apply("getDefaultEndpoint", TypeNode.STRING),
SettingsCommentComposer.DEFAULT_SERVICE_ENDPOINT_METHOD_COMMENT));
javaMethods.add(
methodMakerFn.apply(
methodStarterFn.apply(
"getDefaultServiceScopes",
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(List.class)
.setGenerics(Arrays.asList(TypeNode.STRING.reference()))
.build())),
SettingsCommentComposer.DEFAULT_SERVICE_SCOPES_METHOD_COMMENT));
javaMethods.add(
methodMakerFn.apply(
methodStarterFn.apply(
"defaultCredentialsProviderBuilder",
typeMakerFn.apply(GoogleCredentialsProvider.Builder.class)),
SettingsCommentComposer.DEFAULT_CREDENTIALS_PROVIDER_BUILDER_METHOD_COMMENT));
javaMethods.add(
methodMakerFn.apply(
methodStarterFn.apply(
"defaultGrpcTransportProviderBuilder",
typeMakerFn.apply(InstantiatingGrpcChannelProvider.Builder.class)),
SettingsCommentComposer.DEFAULT_GRPC_TRANSPORT_PROVIDER_BUILDER_METHOD_COMMENT));

javaMethods.add(
methodStarterFn
.apply(
"defaultTransportChannelProvider",
typeMakerFn.apply(TransportChannelProvider.class))
.build());

javaMethods.add(
methodStarterFn
.apply(
"defaultApiClientHeaderProviderBuilder",
TypeNode.withReference(
ConcreteReference.withClazz(ApiClientHeaderProvider.Builder.class)))
.toBuilder()
.setAnnotations(
Arrays.asList(
AnnotationNode.builder()
Expand All @@ -287,6 +340,7 @@ private static List<MethodDefinition> createBuilderHelperMethods(
TypeNode builderType = types.get(BUILDER_CLASS_NAME);
MethodDefinition newBuilderMethodOne =
MethodDefinition.builder()
.setHeaderCommentStatements(SettingsCommentComposer.NEW_BUILDER_METHOD_COMMENT)
.setScope(ScopeNode.PUBLIC)
.setIsStatic(true)
.setReturnType(builderType)
Expand All @@ -308,6 +362,7 @@ private static List<MethodDefinition> createBuilderHelperMethods(

MethodDefinition newBuilderMethodTwo =
MethodDefinition.builder()
.setHeaderCommentStatements(SettingsCommentComposer.NEW_BUILDER_METHOD_COMMENT)
.setScope(ScopeNode.PUBLIC)
.setIsStatic(true)
.setReturnType(builderType)
Expand All @@ -322,6 +377,7 @@ private static List<MethodDefinition> createBuilderHelperMethods(

MethodDefinition toBuilderMethod =
MethodDefinition.builder()
.setHeaderCommentStatements(SettingsCommentComposer.TO_BUILDER_METHOD_COMMENT)
.setScope(ScopeNode.PUBLIC)
.setReturnType(builderType)
.setName("toBuilder")
Expand All @@ -340,6 +396,8 @@ private static List<MethodDefinition> createBuilderHelperMethods(
private static ClassDefinition createNestedBuilderClass(
Service service, Map<String, TypeNode> types) {
return ClassDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.createBuilderClassComment(getThisClassName(service.name())))
.setIsNested(true)
.setScope(ScopeNode.PUBLIC)
.setIsStatic(true)
Expand Down Expand Up @@ -531,6 +589,8 @@ private static MethodDefinition createNestedBuilderApplyToAllUnaryMethod(
.build();

return MethodDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.APPLY_TO_ALL_UNARY_METHODS_METHOD_COMMENTS)
.setScope(ScopeNode.PUBLIC)
.setReturnType(builderType)
.setName(javaMethodName)
Expand All @@ -547,6 +607,9 @@ private static List<MethodDefinition> createNestedBuilderSettingsGetterMethods(
BiFunction<TypeNode, String, MethodDefinition> methodMakerFn =
(retType, methodName) ->
MethodDefinition.builder()
.setHeaderCommentStatements(
SettingsCommentComposer.createCallSettingsBuilderGetterComment(
getMethodNameFromSettingsVarName(methodName)))
.setScope(ScopeNode.PUBLIC)
.setReturnType(retType)
.setName(methodName)
Expand Down Expand Up @@ -623,32 +686,6 @@ private static Map<String, TypeNode> createStaticTypes() {
c -> TypeNode.withReference(ConcreteReference.withClazz(c))));
}

private static Map<String, TypeNode> createDefaultMethodNamesToTypes() {
Function<Class, TypeNode> typeMakerFn =
c -> TypeNode.withReference(ConcreteReference.withClazz(c));
Map<String, TypeNode> javaMethodNameToReturnType = new LinkedHashMap<>();
javaMethodNameToReturnType.put(
"defaultExecutorProviderBuilder",
typeMakerFn.apply(InstantiatingExecutorProvider.Builder.class));
javaMethodNameToReturnType.put("getDefaultEndpoint", TypeNode.STRING);
javaMethodNameToReturnType.put(
"getDefaultServiceScopes",
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(List.class)
.setGenerics(Arrays.asList(TypeNode.STRING.reference()))
.build()));
javaMethodNameToReturnType.put(
"defaultCredentialsProviderBuilder",
typeMakerFn.apply(GoogleCredentialsProvider.Builder.class));
javaMethodNameToReturnType.put(
"defaultGrpcTransportProviderBuilder",
typeMakerFn.apply(InstantiatingGrpcChannelProvider.Builder.class));
javaMethodNameToReturnType.put(
"defaultTransportChannelProvider", staticTypes.get("TransportChannelProvider"));
return javaMethodNameToReturnType;
}

private static Map<String, TypeNode> createDynamicTypes(Service service) {
Map<String, TypeNode> types = new HashMap<>();

Expand Down Expand Up @@ -692,7 +729,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service) {
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(String.format("%sClient", service.name()))
.setEnclosingClassName(getClientClassName(service.name()))
.setIsStaticImport(true)
.build()))));
return types;
Expand Down Expand Up @@ -777,6 +814,11 @@ private static String getThisClassName(String serviceName) {
return String.format(CLASS_NAME_PATTERN, serviceName);
}

private static String getClientClassName(String serviceName) {

return String.format(CLIENT_CLASS_NAME_PATTERN, serviceName);
}

private static String getStubSettingsClassName(String serviceName) {
return String.format(STUB_SETTINGS_PATTERN, serviceName);
}
Expand All @@ -789,4 +831,17 @@ private static TypeNode getStubSettingsBuilderType(Service service) {
.setEnclosingClassName(getStubSettingsClassName(service.name()))
.build());
}

/** Turns a name like "waitSettings" or "waitOperationSettings" into "wait". */
private static String getMethodNameFromSettingsVarName(String settingsVarName) {
BiFunction<String, String, String> methodNameSubstrFn =
(s, literal) -> s.substring(0, s.length() - literal.length());
if (settingsVarName.endsWith(OPERATION_SETTINGS_LITERAL)) {
return methodNameSubstrFn.apply(settingsVarName, OPERATION_SETTINGS_LITERAL);
}
if (settingsVarName.endsWith(SETTINGS_LITERAL)) {
return methodNameSubstrFn.apply(settingsVarName, SETTINGS_LITERAL);
}
return settingsVarName;
}
}
Loading

0 comments on commit 8f1c6f0

Please sign in to comment.