Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit e456a80

Browse files
authored
[ggj][codegen] feat: add Batching init to ServiceStubSettings.Builder ctor (#253)
* 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 * feat: add createDefault() to ServiceStubSettings * feat: add ServiceStubSettings.applyToAllUnaryMethods method * feat: add ServiceStubSettings.unaryMethodSettingsBuilders() * feat: add ServiceStubSettings.build() * feat: add settingsBuilder getters in ServiceStubSettings * feat: add gapic.yaml batching parsing * feat: integrate batching with retry settings parsing * fix: remove unused test proto imports * fix: handle singleton resname patterns, name_field, add logging test * fix: pass in logging grpc service config * fix: pass in logging grpc service config * test: add pubsub proto serviceStubSettings test * feat: add BatchingCallSettings to ServiceStubSettings fields * feat: add Batching init to ServiceStubSettings.Builder ctor
1 parent dba91a1 commit e456a80

2 files changed

Lines changed: 75 additions & 22 deletions

File tree

src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108

109109
// TODO(miraleung): Refactor ClassComposer's interface.
110110
public class ServiceStubSettingsClassComposer {
111+
private static final String BATCHING_DESC_PATTERN = "%s_BATCHING_DESC";
111112
private static final String CLASS_NAME_PATTERN = "%sStubSettings";
112113
private static final String GRPC_SERVICE_STUB_PATTERN = "Grpc%sStub";
113114
private static final String PAGE_STR_DESC_PATTERN = "%s_PAGE_STR_DESC";
@@ -1172,7 +1173,8 @@ private static List<MethodDefinition> createNestedClassMethods(
11721173
Map<String, TypeNode> types) {
11731174
List<MethodDefinition> nestedClassMethods = new ArrayList<>();
11741175
nestedClassMethods.addAll(
1175-
createNestedClassConstructorMethods(service, nestedMethodSettingsMemberVarExprs, types));
1176+
createNestedClassConstructorMethods(
1177+
service, serviceConfig, nestedMethodSettingsMemberVarExprs, types));
11761178
nestedClassMethods.add(createNestedClassCreateDefaultMethod(types));
11771179
nestedClassMethods.add(createNestedClassInitDefaultsMethod(service, serviceConfig, types));
11781180
nestedClassMethods.add(createNestedClassApplyToAllUnaryMethodsMethod(superType, types));
@@ -1234,6 +1236,7 @@ private static MethodDefinition createNestedClassInitDefaultsMethod(
12341236

12351237
private static List<MethodDefinition> createNestedClassConstructorMethods(
12361238
Service service,
1239+
GapicServiceConfig serviceConfig,
12371240
Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs,
12381241
Map<String, TypeNode> types) {
12391242
TypeNode builderType = types.get(NESTED_BUILDER_CLASS_NAME);
@@ -1298,38 +1301,80 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
12981301
nestedMethodSettingsMemberVarExprs.entrySet().stream()
12991302
.map(
13001303
e -> {
1304+
// TODO(miraleung): Extract this into another method.
13011305
// Name is fooBarSettings.
13021306
VariableExpr varExpr = e.getValue();
13031307
TypeNode varType = varExpr.type();
1308+
String methodName = e.getKey();
1309+
Preconditions.checkState(
1310+
methodName.endsWith(SETTINGS_LITERAL),
1311+
String.format("%s expected to end with \"Settings\"", methodName));
1312+
methodName =
1313+
methodName.substring(0, methodName.length() - SETTINGS_LITERAL.length());
1314+
13041315
if (!isPagedCallSettingsBuilderFn.apply(varType)) {
1305-
boolean isUnaryCallSettings = isUnaryCallSettingsBuilderFn.apply(varType);
1316+
if (!isBatchingCallSettingsBuilderFn.apply(varType)) {
1317+
boolean isUnaryCallSettings = isUnaryCallSettingsBuilderFn.apply(varType);
1318+
return AssignmentExpr.builder()
1319+
.setVariableExpr(varExpr)
1320+
.setValueExpr(
1321+
MethodInvocationExpr.builder()
1322+
.setStaticReferenceType(
1323+
builderToCallSettingsFn.apply(varExpr.type()))
1324+
.setMethodName(
1325+
isUnaryCallSettings
1326+
? "newUnaryCallSettingsBuilder"
1327+
: "newBuilder")
1328+
.setReturnType(varExpr.type())
1329+
.build())
1330+
.build();
1331+
}
1332+
Expr newBatchingSettingsExpr =
1333+
MethodInvocationExpr.builder()
1334+
.setStaticReferenceType(STATIC_TYPES.get("BatchingSettings"))
1335+
.setMethodName("newBuilder")
1336+
.build();
1337+
newBatchingSettingsExpr =
1338+
MethodInvocationExpr.builder()
1339+
.setExprReferenceExpr(newBatchingSettingsExpr)
1340+
.setMethodName("build")
1341+
.build();
1342+
1343+
String batchingDescVarName =
1344+
String.format(
1345+
BATCHING_DESC_PATTERN, JavaStyle.toUpperSnakeCase(methodName));
1346+
Expr batchingSettingsBuilderExpr =
1347+
MethodInvocationExpr.builder()
1348+
.setStaticReferenceType(builderToCallSettingsFn.apply(varType))
1349+
.setMethodName("newBuilder")
1350+
.setArguments(
1351+
VariableExpr.withVariable(
1352+
Variable.builder()
1353+
.setType(STATIC_TYPES.get("BatchingDescriptor"))
1354+
.setName(batchingDescVarName)
1355+
.build()))
1356+
.build();
1357+
batchingSettingsBuilderExpr =
1358+
MethodInvocationExpr.builder()
1359+
.setExprReferenceExpr(batchingSettingsBuilderExpr)
1360+
.setMethodName("setBatchingSettings")
1361+
.setArguments(newBatchingSettingsExpr)
1362+
.setReturnType(varType)
1363+
.build();
1364+
13061365
return AssignmentExpr.builder()
13071366
.setVariableExpr(varExpr)
1308-
.setValueExpr(
1309-
MethodInvocationExpr.builder()
1310-
.setStaticReferenceType(
1311-
builderToCallSettingsFn.apply(varExpr.type()))
1312-
.setMethodName(
1313-
isUnaryCallSettings
1314-
? "newUnaryCallSettingsBuilder"
1315-
: "newBuilder")
1316-
.setReturnType(varExpr.type())
1317-
.build())
1367+
.setValueExpr(batchingSettingsBuilderExpr)
13181368
.build();
13191369
}
1320-
String varName = e.getKey();
1321-
Preconditions.checkState(
1322-
varName.endsWith(SETTINGS_LITERAL),
1323-
String.format("%s expected to end with \"Settings\"", varName));
1324-
varName = varName.substring(0, varName.length() - SETTINGS_LITERAL.length());
1325-
varName =
1370+
String memberVarName =
13261371
String.format(
1327-
PAGED_RESPONSE_FACTORY_PATTERN, JavaStyle.toUpperSnakeCase(varName));
1372+
PAGED_RESPONSE_FACTORY_PATTERN, JavaStyle.toUpperSnakeCase(methodName));
13281373
VariableExpr argVar =
13291374
VariableExpr.withVariable(
13301375
Variable.builder()
13311376
.setType(STATIC_TYPES.get("PagedListResponseFactory"))
1332-
.setName(varName)
1377+
.setName(memberVarName)
13331378
.build());
13341379
return AssignmentExpr.builder()
13351380
.setVariableExpr(varExpr)

src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ private static List<Service> parseServices(
681681
+ "import com.google.api.core.ApiFunction;\n"
682682
+ "import com.google.api.core.ApiFuture;\n"
683683
+ "import com.google.api.core.BetaApi;\n"
684+
+ "import com.google.api.gax.batching.BatchingSettings;\n"
684685
+ "import com.google.api.gax.core.GaxProperties;\n"
685686
+ "import com.google.api.gax.core.GoogleCredentialsProvider;\n"
686687
+ "import com.google.api.gax.core.InstantiatingExecutorProvider;\n"
@@ -691,6 +692,7 @@ private static List<Service> parseServices(
691692
+ "import com.google.api.gax.rpc.ApiCallContext;\n"
692693
+ "import com.google.api.gax.rpc.ApiClientHeaderProvider;\n"
693694
+ "import com.google.api.gax.rpc.BatchingCallSettings;\n"
695+
+ "import com.google.api.gax.rpc.BatchingDescriptor;\n"
694696
+ "import com.google.api.gax.rpc.ClientContext;\n"
695697
+ "import com.google.api.gax.rpc.PageContext;\n"
696698
+ "import com.google.api.gax.rpc.PagedCallSettings;\n"
@@ -1131,7 +1133,9 @@ private static List<Service> parseServices(
11311133
+ " protected Builder(ClientContext clientContext) {\n"
11321134
+ " super(clientContext);\n"
11331135
+ " deleteLogSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
1134-
+ " writeLogEntriesSettings = BatchingCallSettings.newBuilder();\n"
1136+
+ " writeLogEntriesSettings =\n"
1137+
+ " BatchingCallSettings.newBuilder(WRITE_LOG_ENTRIES_BATCHING_DESC)\n"
1138+
+ " .setBatchingSettings(BatchingSettings.newBuilder().build());\n"
11351139
+ " listLogEntriesSettings ="
11361140
+ " PagedCallSettings.newBuilder(LIST_LOG_ENTRIES_PAGE_STR_FACT);\n"
11371141
+ " listMonitoredResourceDescriptorsSettings =\n"
@@ -1270,6 +1274,7 @@ private static List<Service> parseServices(
12701274
+ "import com.google.api.core.ApiFunction;\n"
12711275
+ "import com.google.api.core.ApiFuture;\n"
12721276
+ "import com.google.api.core.BetaApi;\n"
1277+
+ "import com.google.api.gax.batching.BatchingSettings;\n"
12731278
+ "import com.google.api.gax.core.GaxProperties;\n"
12741279
+ "import com.google.api.gax.core.GoogleCredentialsProvider;\n"
12751280
+ "import com.google.api.gax.core.InstantiatingExecutorProvider;\n"
@@ -1280,6 +1285,7 @@ private static List<Service> parseServices(
12801285
+ "import com.google.api.gax.rpc.ApiCallContext;\n"
12811286
+ "import com.google.api.gax.rpc.ApiClientHeaderProvider;\n"
12821287
+ "import com.google.api.gax.rpc.BatchingCallSettings;\n"
1288+
+ "import com.google.api.gax.rpc.BatchingDescriptor;\n"
12831289
+ "import com.google.api.gax.rpc.ClientContext;\n"
12841290
+ "import com.google.api.gax.rpc.PageContext;\n"
12851291
+ "import com.google.api.gax.rpc.PagedCallSettings;\n"
@@ -1783,7 +1789,9 @@ private static List<Service> parseServices(
17831789
+ " super(clientContext);\n"
17841790
+ " createTopicSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
17851791
+ " updateTopicSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
1786-
+ " publishSettings = BatchingCallSettings.newBuilder();\n"
1792+
+ " publishSettings =\n"
1793+
+ " BatchingCallSettings.newBuilder(PUBLISH_BATCHING_DESC)\n"
1794+
+ " .setBatchingSettings(BatchingSettings.newBuilder().build());\n"
17871795
+ " getTopicSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
17881796
+ " listTopicsSettings = PagedCallSettings.newBuilder(LIST_TOPICS_PAGE_STR_FACT);\n"
17891797
+ " listTopicSubscriptionsSettings =\n"

0 commit comments

Comments
 (0)