Skip to content

Commit

Permalink
[ggj][codegen] feat: add BatchingCallSettings to ServiceStubSettings …
Browse files Browse the repository at this point in the history
…fields (#252)

* 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
  • Loading branch information
miraleung committed Aug 29, 2020
1 parent 14cd797 commit dba91a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ public GapicClass generate(
String pakkage = String.format("%s.stub", service.pakkage());
Map<String, TypeNode> types = createDynamicTypes(service, pakkage);
Map<String, VariableExpr> methodSettingsMemberVarExprs =
createMethodSettingsClassMemberVarExprs(service, types, /* isNestedClass= */ false);
createMethodSettingsClassMemberVarExprs(
service, serviceConfig, types, /* isNestedClass= */ false);
String className = getThisClassName(service.name());

ClassDefinition classDef =
Expand Down Expand Up @@ -189,14 +190,19 @@ private static TypeNode createExtendsType(Service service, Map<String, TypeNode>
}

private static Map<String, VariableExpr> createMethodSettingsClassMemberVarExprs(
Service service, Map<String, TypeNode> types, boolean isNestedClass) {
Service service,
GapicServiceConfig serviceConfig,
Map<String, TypeNode> types,
boolean isNestedClass) {
// Maintain insertion order.
Map<String, VariableExpr> varExprs = new LinkedHashMap<>();

// Creates class variables <method>Settings, e.g. echoSettings.
// TODO(miraleung): Handle batching here.
for (Method method : service.methods()) {
TypeNode settingsType = getCallSettingsType(method, types, isNestedClass);
boolean hasBatchingSettings = serviceConfig.hasBatchingSetting(service, method);
TypeNode settingsType =
getCallSettingsType(method, types, hasBatchingSettings, isNestedClass);
String varName = JavaStyle.toLowerCamelCase(String.format("%sSettings", method.name()));
varExprs.put(
varName,
Expand Down Expand Up @@ -1090,7 +1096,8 @@ private static ClassDefinition createNestedBuilderClass(
.build());

Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs =
createMethodSettingsClassMemberVarExprs(service, types, /* isNestedClass= */ true);
createMethodSettingsClassMemberVarExprs(
service, serviceConfig, types, /* isNestedClass= */ true);

// TODO(miraleung): Fill this out.
return ClassDefinition.builder()
Expand Down Expand Up @@ -1260,6 +1267,8 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
.build());
Reference pagedSettingsBuilderRef =
ConcreteReference.withClazz(PagedCallSettings.Builder.class);
Reference batchingSettingsBuilderRef =
ConcreteReference.withClazz(BatchingCallSettings.Builder.class);
Reference unaryCallSettingsBuilderRef =
ConcreteReference.withClazz(UnaryCallSettings.Builder.class);
Function<TypeNode, Boolean> isUnaryCallSettingsBuilderFn =
Expand All @@ -1269,6 +1278,9 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
.equals(unaryCallSettingsBuilderRef);
Function<TypeNode, Boolean> isPagedCallSettingsBuilderFn =
t -> t.reference().copyAndSetGenerics(ImmutableList.of()).equals(pagedSettingsBuilderRef);
Function<TypeNode, Boolean> isBatchingCallSettingsBuilderFn =
t ->
t.reference().copyAndSetGenerics(ImmutableList.of()).equals(batchingSettingsBuilderRef);
Function<TypeNode, TypeNode> builderToCallSettingsFn =
t ->
TypeNode.withReference(
Expand Down Expand Up @@ -1349,7 +1361,8 @@ private static List<MethodDefinition> createNestedClassConstructorMethods(
.filter(
v ->
isUnaryCallSettingsBuilderFn.apply(v.type())
|| isPagedCallSettingsBuilderFn.apply(v.type()))
|| isPagedCallSettingsBuilderFn.apply(v.type())
|| isBatchingCallSettingsBuilderFn.apply(v.type()))
.collect(Collectors.toList()))
.setReturnType(NESTED_UNARY_METHOD_SETTINGS_BUILDERS_VAR_EXPR.type())
.build())
Expand Down Expand Up @@ -1809,7 +1822,10 @@ private static String getGrpcServiceStubTypeName(String serviceName) {
}

private static TypeNode getCallSettingsType(
Method method, Map<String, TypeNode> types, final boolean isSettingsBuilder) {
Method method,
Map<String, TypeNode> types,
boolean isBatchingSettings,
final boolean isSettingsBuilder) {
Function<Class, TypeNode> typeMakerFn =
clz -> TypeNode.withReference(ConcreteReference.withClazz(clz));
// Default: No streaming.
Expand All @@ -1819,6 +1835,11 @@ private static TypeNode getCallSettingsType(
isSettingsBuilder ? PagedCallSettings.Builder.class : PagedCallSettings.class)
: typeMakerFn.apply(
isSettingsBuilder ? UnaryCallSettings.Builder.class : UnaryCallSettings.class);
if (isBatchingSettings) {
callSettingsType =
typeMakerFn.apply(
isSettingsBuilder ? BatchingCallSettings.Builder.class : BatchingCallSettings.class);
}

// Streaming takes precendence over paging, as per the monolith's existing behavior.
switch (method.stream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ private static List<Service> parseServices(
+ "import com.google.api.gax.retrying.RetrySettings;\n"
+ "import com.google.api.gax.rpc.ApiCallContext;\n"
+ "import com.google.api.gax.rpc.ApiClientHeaderProvider;\n"
+ "import com.google.api.gax.rpc.BatchingCallSettings;\n"
+ "import com.google.api.gax.rpc.ClientContext;\n"
+ "import com.google.api.gax.rpc.PageContext;\n"
+ "import com.google.api.gax.rpc.PagedCallSettings;\n"
Expand Down Expand Up @@ -734,7 +735,8 @@ private static List<Service> parseServices(
+ " .add(\"https://www.googleapis.com/auth/logging.write\")\n"
+ " .build();\n"
+ " private final UnaryCallSettings<DeleteLogRequest, Empty> deleteLogSettings;\n"
+ " private final UnaryCallSettings<WriteLogEntriesRequest, WriteLogEntriesResponse>\n"
+ " private final BatchingCallSettings<WriteLogEntriesRequest,"
+ " WriteLogEntriesResponse>\n"
+ " writeLogEntriesSettings;\n"
+ " private final PagedCallSettings<\n"
+ " ListLogEntriesRequest, ListLogEntriesResponse,"
Expand Down Expand Up @@ -959,7 +961,7 @@ private static List<Service> parseServices(
+ " return deleteLogSettings;\n"
+ " }\n"
+ "\n"
+ " public UnaryCallSettings<WriteLogEntriesRequest, WriteLogEntriesResponse>\n"
+ " public BatchingCallSettings<WriteLogEntriesRequest, WriteLogEntriesResponse>\n"
+ " writeLogEntriesSettings() {\n"
+ " return writeLogEntriesSettings;\n"
+ " }\n"
Expand Down Expand Up @@ -1070,7 +1072,7 @@ private static List<Service> parseServices(
+ " unaryMethodSettingsBuilders;\n"
+ " private final UnaryCallSettings.Builder<DeleteLogRequest, Empty>"
+ " deleteLogSettings;\n"
+ " private final UnaryCallSettings.Builder<WriteLogEntriesRequest,"
+ " private final BatchingCallSettings.Builder<WriteLogEntriesRequest,"
+ " WriteLogEntriesResponse>\n"
+ " writeLogEntriesSettings;\n"
+ " private final PagedCallSettings.Builder<\n"
Expand Down Expand Up @@ -1129,7 +1131,7 @@ private static List<Service> parseServices(
+ " protected Builder(ClientContext clientContext) {\n"
+ " super(clientContext);\n"
+ " deleteLogSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
+ " writeLogEntriesSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
+ " writeLogEntriesSettings = BatchingCallSettings.newBuilder();\n"
+ " listLogEntriesSettings ="
+ " PagedCallSettings.newBuilder(LIST_LOG_ENTRIES_PAGE_STR_FACT);\n"
+ " listMonitoredResourceDescriptorsSettings =\n"
Expand Down Expand Up @@ -1223,7 +1225,7 @@ private static List<Service> parseServices(
+ " return deleteLogSettings;\n"
+ " }\n"
+ "\n"
+ " public UnaryCallSettings.Builder<WriteLogEntriesRequest,"
+ " public BatchingCallSettings.Builder<WriteLogEntriesRequest,"
+ " WriteLogEntriesResponse>\n"
+ " writeLogEntriesSettings() {\n"
+ " return writeLogEntriesSettings;\n"
Expand Down Expand Up @@ -1277,6 +1279,7 @@ private static List<Service> parseServices(
+ "import com.google.api.gax.retrying.RetrySettings;\n"
+ "import com.google.api.gax.rpc.ApiCallContext;\n"
+ "import com.google.api.gax.rpc.ApiClientHeaderProvider;\n"
+ "import com.google.api.gax.rpc.BatchingCallSettings;\n"
+ "import com.google.api.gax.rpc.ClientContext;\n"
+ "import com.google.api.gax.rpc.PageContext;\n"
+ "import com.google.api.gax.rpc.PagedCallSettings;\n"
Expand Down Expand Up @@ -1322,7 +1325,8 @@ private static List<Service> parseServices(
+ " .build();\n"
+ " private final UnaryCallSettings<Topic, Topic> createTopicSettings;\n"
+ " private final UnaryCallSettings<UpdateTopicRequest, Topic> updateTopicSettings;\n"
+ " private final UnaryCallSettings<PublishRequest, PublishResponse> publishSettings;\n"
+ " private final BatchingCallSettings<PublishRequest, PublishResponse>"
+ " publishSettings;\n"
+ " private final UnaryCallSettings<GetTopicRequest, Topic> getTopicSettings;\n"
+ " private final PagedCallSettings<ListTopicsRequest, ListTopicsResponse,"
+ " ListTopicsPagedResponse>\n"
Expand Down Expand Up @@ -1549,7 +1553,7 @@ private static List<Service> parseServices(
+ " return updateTopicSettings;\n"
+ " }\n"
+ "\n"
+ " public UnaryCallSettings<PublishRequest, PublishResponse> publishSettings() {\n"
+ " public BatchingCallSettings<PublishRequest, PublishResponse> publishSettings() {\n"
+ " return publishSettings;\n"
+ " }\n"
+ "\n"
Expand Down Expand Up @@ -1677,7 +1681,7 @@ private static List<Service> parseServices(
+ " private final UnaryCallSettings.Builder<Topic, Topic> createTopicSettings;\n"
+ " private final UnaryCallSettings.Builder<UpdateTopicRequest, Topic>"
+ " updateTopicSettings;\n"
+ " private final UnaryCallSettings.Builder<PublishRequest, PublishResponse>"
+ " private final BatchingCallSettings.Builder<PublishRequest, PublishResponse>"
+ " publishSettings;\n"
+ " private final UnaryCallSettings.Builder<GetTopicRequest, Topic>"
+ " getTopicSettings;\n"
Expand Down Expand Up @@ -1779,7 +1783,7 @@ private static List<Service> parseServices(
+ " super(clientContext);\n"
+ " createTopicSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
+ " updateTopicSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
+ " publishSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
+ " publishSettings = BatchingCallSettings.newBuilder();\n"
+ " getTopicSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();\n"
+ " listTopicsSettings = PagedCallSettings.newBuilder(LIST_TOPICS_PAGE_STR_FACT);\n"
+ " listTopicSubscriptionsSettings =\n"
Expand Down Expand Up @@ -1916,7 +1920,7 @@ private static List<Service> parseServices(
+ " return updateTopicSettings;\n"
+ " }\n"
+ "\n"
+ " public UnaryCallSettings.Builder<PublishRequest, PublishResponse>"
+ " public BatchingCallSettings.Builder<PublishRequest, PublishResponse>"
+ " publishSettings() {\n"
+ " return publishSettings;\n"
+ " }\n"
Expand Down

0 comments on commit dba91a1

Please sign in to comment.