Skip to content

Commit

Permalink
[ggj][codegen] feat: add third ServiceStubSettings.Builder(settings) …
Browse files Browse the repository at this point in the history
…ctor (#241)

* feat: add factory var decl in ServiceStubSettings codegen

* fix: prevent duplicate MethodDefinition annotations

* feat: add descriptor fields to ServiceStubSettings codegen

* feat: add starter Builder to ServiceStubSettings codegen

* feat: add settings.builder decls to ServiceStubSettings codegen

* feat: add first nested ctors to ServiceStubSettings codegen

* feat: add GapicServiceConfig DS and processing

* feat: integrate GapicServiceConfig into GapicContext, Parser, Composer

* feat: initial param block, RetrySettingsComposer, test

* fix!: refactor GapicRetrySettings

* fix: recognize 1. or .1 double patterns

* feat: support BlockStatement in ClassDef stmts

* feat: add params block to ServiceStubSettings codegen

* feat: add codes def to ServiceStubSettings codegen

* feat: add initDefaults() to ServiceStubSettings codegen

* feat: add LRO to ServiceStubSettings.Builder.initDefaults

* feat: add third ServiceStubSettings.Builder(settings) ctor
  • Loading branch information
miraleung authored Aug 29, 2020
1 parent 15da1d0 commit bf0bbaa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ private static Map<String, TypeNode> createStaticTypes() {
RetrySettings.class,
StatusCode.class,
UnaryCallSettings.class);

return concreteClazzes.stream()
.collect(
Collectors.toMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ private static List<MethodDefinition> createNestedClassMethods(
Map<String, TypeNode> types) {
List<MethodDefinition> nestedClassMethods = new ArrayList<>();
nestedClassMethods.addAll(
createNestedClassConstructorMethods(nestedMethodSettingsMemberVarExprs, types));
createNestedClassConstructorMethods(service, nestedMethodSettingsMemberVarExprs, types));
nestedClassMethods.add(createNestedClassInitDefaultsMethod(service, serviceConfig, types));

// TODO(miraleung): More methods.
Expand Down Expand Up @@ -1220,7 +1220,10 @@ private static MethodDefinition createNestedClassInitDefaultsMethod(
}

private static List<MethodDefinition> createNestedClassConstructorMethods(
Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs, Map<String, TypeNode> types) {
Service service,
Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs,
Map<String, TypeNode> types) {

TypeNode builderType = types.get(NESTED_BUILDER_CLASS_NAME);

List<MethodDefinition> ctorMethods = new ArrayList<>();
Expand Down Expand Up @@ -1324,7 +1327,7 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
})
.collect(Collectors.toList()));

ctorBodyExprs.add(
Expr unaryMethodSettingsBuildersAssignExpr =
AssignmentExpr.builder()
.setVariableExpr(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR)
.setValueExpr(
Expand All @@ -1345,7 +1348,8 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
.collect(Collectors.toList()))
.setReturnType(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR.type())
.build())
.build());
.build();
ctorBodyExprs.add(unaryMethodSettingsBuildersAssignExpr);

ctorBodyExprs.add(
MethodInvocationExpr.builder()
Expand All @@ -1364,6 +1368,50 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
.collect(Collectors.toList()))
.build());

// Third constructor that takes a ServivceStubSettings.
TypeNode outerSettingsType = types.get(getThisClassName(service.name()));
VariableExpr settingsVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(outerSettingsType).setName("settings").build());
ctorBodyExprs = new ArrayList<>();
ctorBodyExprs.add(
ReferenceConstructorExpr.superBuilder()
.setType(builderType)
.setArguments(settingsVarExpr)
.build());
// TODO(cleanup): Technically this should actually use the outer class's <method>Settings
// members to avoid decoupling variable names.
ctorBodyExprs.addAll(
nestedMethodSettingsMemberVarExprs.values().stream()
.map(
v ->
AssignmentExpr.builder()
.setVariableExpr(v)
.setValueExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(
VariableExpr.builder()
.setExprReferenceExpr(settingsVarExpr)
.setVariable(v.variable())
.build())
.setMethodName("toBuilder")
.setReturnType(v.type())
.build())
.build())
.collect(Collectors.toList()));
ctorBodyExprs.add(unaryMethodSettingsBuildersAssignExpr);

ctorMethods.add(
MethodDefinition.constructorBuilder()
.setScope(ScopeNode.PROTECTED)
.setReturnType(builderType)
.setArguments(settingsVarExpr.toBuilder().setIsDecl(true).build())
.setBody(
ctorBodyExprs.stream()
.map(e -> ExprStatement.withExpr(e))
.collect(Collectors.toList()))
.build());

return ctorMethods;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,22 @@ public void generateServiceClasses() {
+ " initDefaults(this);\n"
+ " }\n"
+ "\n"
+ " protected Builder(EchoStubSettings settings) {\n"
+ " super(settings);\n"
+ " echoSettings = settings.echoSettings.toBuilder();\n"
+ " expandSettings = settings.expandSettings.toBuilder();\n"
+ " collectSettings = settings.collectSettings.toBuilder();\n"
+ " chatSettings = settings.chatSettings.toBuilder();\n"
+ " chatAgainSettings = settings.chatAgainSettings.toBuilder();\n"
+ " pagedExpandSettings = settings.pagedExpandSettings.toBuilder();\n"
+ " waitSettings = settings.waitSettings.toBuilder();\n"
+ " waitOperationSettings = settings.waitOperationSettings.toBuilder();\n"
+ " blockSettings = settings.blockSettings.toBuilder();\n"
+ " unaryMethodSettingsBuilders =\n"
+ " ImmutableList.<UnaryCallSettings.Builder<?, ?>>of(\n"
+ " echoSettings, pagedExpandSettings, waitSettings, blockSettings);\n"
+ " }\n"
+ "\n"
+ " private static Builder initDefaults(Builder builder) {\n"
+ " builder\n"
+ " .echoSettings()\n"
Expand Down

0 comments on commit bf0bbaa

Please sign in to comment.