FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ggj][codegen] feat: add splitException, count{Elements,Bytes} ServiceStubSettings batching descriptor by miraleung · Pull Request #265 · googleapis/sdk-platform-java · GitHub

This repository was archived by the owner on May 14, 2026. It is now read-only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.Expr;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.ForStatement;
import com.google.api.generator.engine.ast.IfStatement;
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
Expand All @@ -39,6 +40,7 @@
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -58,13 +60,18 @@ public class BatchingDescriptorComposer {

private static final String ADD_ALL_METHOD_PATTERN = "addAll%s";
private static final String GET_LIST_METHOD_PATTERN = "get%sList";
private static final String GET_COUNT_METHOD_PATTERN = "get%sCount";

public static Expr createBatchingDescriptorFieldDeclExpr(
Method method, GapicBatchingSettings batchingSettings, Map<String, Message> messageTypes) {
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(createGetBatchPartitionKeyMethod(method, batchingSettings, messageTypes));
javaMethods.add(createGetRequestBuilderMethod(method, batchingSettings));

javaMethods.add(createSplitExceptionMethod(method));
javaMethods.add(createCountElementsMethod(method, batchingSettings));
javaMethods.add(createCountByteSMethod(method));

TypeNode batchingDescriptorType =
toType(BATCHING_DESCRIPTOR_REF, method.inputType(), method.outputType());
AnonymousClassExpr batchingDescriptorClassExpr =
Expand Down Expand Up @@ -229,6 +236,99 @@ private static MethodDefinition createGetRequestBuilderMethod(
.build();
}

private static MethodDefinition createSplitExceptionMethod(Method method) {
VariableExpr throwableVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(toType(Throwable.class)).setName("throwable").build());

TypeNode batchedRequestIssuerType = toType(BATCHED_REQUEST_ISSUER_REF, method.outputType());
TypeNode batchVarType =
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(Collection.class)
.setGenerics(
Arrays.asList(
ConcreteReference.wildcardWithUpperBound(
batchedRequestIssuerType.reference())))
.build());
VariableExpr batchVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(batchVarType).setName("batch").build());
VariableExpr responderVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(batchedRequestIssuerType).setName("responder").build());

ForStatement forStatement =
ForStatement.builder()
.setLocalVariableExpr(responderVarExpr.toBuilder().setIsDecl(true).build())
.setCollectionExpr(batchVarExpr)
.setBody(
Arrays.asList(
ExprStatement.withExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(responderVarExpr)
.setMethodName("setException")
.setArguments(throwableVarExpr)
.build())))
.build();

return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.VOID)
.setName("splitException")
.setArguments(
Arrays.asList(throwableVarExpr, batchVarExpr).stream()
.map(v -> v.toBuilder().setIsDecl(true).build())
.collect(Collectors.toList()))
.setBody(Arrays.asList(forStatement))
.build();
}

private static MethodDefinition createCountElementsMethod(
Method method, GapicBatchingSettings batchingSettings) {
String getFooCountMethodName =
String.format(
GET_COUNT_METHOD_PATTERN,
JavaStyle.toUpperCamelCase(batchingSettings.batchedFieldName()));
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(method.inputType()).setName("request").build());

return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.LONG)
.setName("countElements")
.setArguments(requestVarExpr.toBuilder().setIsDecl(true).build())
.setReturnExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(requestVarExpr)
.setMethodName(getFooCountMethodName)
.setReturnType(TypeNode.LONG)
.build())
.build();
}

private static MethodDefinition createCountByteSMethod(Method method) {
VariableExpr requestVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(method.inputType()).setName("request").build());
return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.LONG)
.setName("countBytes")
.setArguments(requestVarExpr.toBuilder().setIsDecl(true).build())
.setReturnExpr(
MethodInvocationExpr.builder()
.setExprReferenceExpr(requestVarExpr)
.setMethodName("getSerializedSize")
.setReturnType(TypeNode.LONG)
.build())
.build();
}

private static TypeNode toType(Class clazz) {
return TypeNode.withReference(ConcreteReference.withClazz(clazz));
}
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ public void batchingDescriptor_hasSubresponseField() {
"}\n",
"};\n",
"}\n",
"@Override\n",
"public void splitException(",
"Throwable throwable, ",
"Collection<? extends BatchedRequestIssuer<PublishResponse>> batch) {\n",
"for (BatchedRequestIssuer<PublishResponse> responder : batch) {\n",
"responder.setException(throwable);\n",
"}\n",
"}\n",
"@Override\n",
"public long countElements(PublishRequest request) {\n",
"return request.getMessagesCount();\n",
"}\n",
"@Override\n",
"public long countBytes(PublishRequest request) {\n",
"return request.getSerializedSize();\n",
"}\n",
"}");
assertEquals(expected, writerVisitor.write());
}
Expand Down Expand Up @@ -209,6 +225,22 @@ public void batchingDescriptor_noSubresponseField() {
"}\n",
"};\n",
"}\n",
"@Override\n",
"public void splitException(",
"Throwable throwable, ",
"Collection<? extends BatchedRequestIssuer<WriteLogEntriesResponse>> batch) {\n",
"for (BatchedRequestIssuer<WriteLogEntriesResponse> responder : batch) {\n",
"responder.setException(throwable);\n",
"}\n",
"}\n",
"@Override\n",
"public long countElements(WriteLogEntriesRequest request) {\n",
"return request.getEntriesCount();\n",
"}\n",
"@Override\n",
"public long countBytes(WriteLogEntriesRequest request) {\n",
"return request.getSerializedSize();\n",
"}\n",
"}");

assertEquals(expected, writerVisitor.write());
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,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.BatchedRequestIssuer;\n"
+ "import com.google.api.gax.rpc.BatchingCallSettings;\n"
+ "import com.google.api.gax.rpc.BatchingDescriptor;\n"
+ "import com.google.api.gax.rpc.ClientContext;\n"
Expand Down Expand Up @@ -723,6 +724,7 @@ private static List<Service> parseServices(
+ "import com.google.logging.v2.WriteLogEntriesResponse;\n"
+ "import com.google.protobuf.Empty;\n"
+ "import java.io.IOException;\n"
+ "import java.util.Collection;\n"
+ "import java.util.List;\n"
+ "import java.util.Objects;\n"
+ "import javax.annotation.Generated;\n"
Expand Down Expand Up @@ -995,6 +997,27 @@ private static List<Service> parseServices(
+ " }\n"
+ " };\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public void splitException(\n"
+ " Throwable throwable,\n"
+ " Collection<? extends BatchedRequestIssuer<WriteLogEntriesResponse>>"
+ " batch) {\n"
+ " for (BatchedRequestIssuer<WriteLogEntriesResponse> responder : batch)"
+ " {\n"
+ " responder.setException(throwable);\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public long countElements(WriteLogEntriesRequest request) {\n"
+ " return request.getEntriesCount();\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public long countBytes(WriteLogEntriesRequest request) {\n"
+ " return request.getSerializedSize();\n"
+ " }\n"
+ " };\n"
+ "\n"
+ " public UnaryCallSettings<DeleteLogRequest, Empty> deleteLogSettings() {\n"
Expand Down Expand Up @@ -1341,6 +1364,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.BatchedRequestIssuer;\n"
+ "import com.google.api.gax.rpc.BatchingCallSettings;\n"
+ "import com.google.api.gax.rpc.BatchingDescriptor;\n"
+ "import com.google.api.gax.rpc.ClientContext;\n"
Expand Down Expand Up @@ -1373,6 +1397,7 @@ private static List<Service> parseServices(
+ "import com.google.pubsub.v1.Topic;\n"
+ "import com.google.pubsub.v1.UpdateTopicRequest;\n"
+ "import java.io.IOException;\n"
+ "import java.util.Collection;\n"
+ "import java.util.List;\n"
+ "import java.util.Objects;\n"
+ "import javax.annotation.Generated;\n"
Expand Down Expand Up @@ -1635,6 +1660,25 @@ private static List<Service> parseServices(
+ " }\n"
+ " };\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public void splitException(\n"
+ " Throwable throwable,\n"
+ " Collection<? extends BatchedRequestIssuer<PublishResponse>> batch) {\n"
+ " for (BatchedRequestIssuer<PublishResponse> responder : batch) {\n"
+ " responder.setException(throwable);\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public long countElements(PublishRequest request) {\n"
+ " return request.getMessagesCount();\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public long countBytes(PublishRequest request) {\n"
+ " return request.getSerializedSize();\n"
+ " }\n"
+ " };\n"
+ "\n"
+ " public UnaryCallSettings<Topic, Topic> createTopicSettings() {\n"
Expand Down

Back | FazBrowse Home | New Git URL