Skip to content

Codegen decorator class #3199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

package software.amazon.awssdk.codegen.docs;


import static software.amazon.awssdk.codegen.internal.Constant.ASYNC_STREAMING_INPUT_PARAM;
import static software.amazon.awssdk.codegen.internal.Constant.ASYNC_STREAMING_OUTPUT_PARAM;

import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;

Expand Down Expand Up @@ -66,10 +70,10 @@ protected void applyReturns(DocumentationBuilder docBuilder) {
protected void applyParams(DocumentationBuilder docBuilder) {
emitRequestParm(docBuilder);
if (opModel.hasStreamingInput()) {
docBuilder.param("requestBody", REQUEST_BODY_DOCS + getStreamingInputDocs());
docBuilder.param(ASYNC_STREAMING_INPUT_PARAM, REQUEST_BODY_DOCS + getStreamingInputDocs());
}
if (opModel.hasStreamingOutput()) {
docBuilder.param("asyncResponseTransformer", STREAM_RESPONSE_TRANSFORMER_DOCS + getStreamingOutputDocs());
docBuilder.param(ASYNC_STREAMING_OUTPUT_PARAM, STREAM_RESPONSE_TRANSFORMER_DOCS + getStreamingOutputDocs());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.model.service.PaginatorDefinition;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
import software.amazon.awssdk.utils.async.SequentialSubscriber;

Expand All @@ -33,13 +33,13 @@ public class PaginationDocs {
private static final String SUBSCRIBE_METHOD_NAME = "subscribe";

private final OperationModel operationModel;
private final PoetExtensions poetExtensions;
private final PoetExtension poetExtensions;
private final PaginatorDefinition paginatorDefinition;

public PaginationDocs(IntermediateModel intermediateModel, OperationModel operationModel,
PaginatorDefinition paginatorDefinition) {
this.operationModel = operationModel;
this.poetExtensions = new PoetExtensions(intermediateModel);
this.poetExtensions = new PoetExtension(intermediateModel);
this.paginatorDefinition = paginatorDefinition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static software.amazon.awssdk.codegen.internal.Constant.SYNC_CLIENT_DESTINATION_PATH_PARAM_NAME;
import static software.amazon.awssdk.codegen.internal.Constant.SYNC_CLIENT_SOURCE_PATH_PARAM_NAME;
import static software.amazon.awssdk.codegen.internal.Constant.SYNC_STREAMING_INPUT_PARAM;
import static software.amazon.awssdk.codegen.internal.Constant.SYNC_STREAMING_OUTPUT_PARAM;

import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
Expand Down Expand Up @@ -76,11 +78,11 @@ protected void applyReturns(DocumentationBuilder docBuilder) {
protected void applyParams(DocumentationBuilder docBuilder) {
emitRequestParm(docBuilder);
if (opModel.hasStreamingInput()) {
docBuilder.param("requestBody", REQUEST_BODY_DOCS + getStreamingInputDocs());
docBuilder.param(SYNC_STREAMING_INPUT_PARAM, REQUEST_BODY_DOCS + getStreamingInputDocs());

}
if (opModel.hasStreamingOutput()) {
docBuilder.param("responseTransformer", STREAM_RESPONSE_HANDLER_DOCS + getStreamingOutputDocs(),
docBuilder.param(SYNC_STREAMING_OUTPUT_PARAM, STREAM_RESPONSE_HANDLER_DOCS + getStreamingOutputDocs(),
opModel.getOutputShape().getShapeName(), getStreamingOutputDocs());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.codegen.emitters.tasks.SharedModelsTaskParamsValidator;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.PoetExtension;

/**
* Parameters for generator tasks.
Expand All @@ -30,14 +30,14 @@ public class GeneratorTaskParams {

private final IntermediateModel model;
private final GeneratorPathProvider pathProvider;
private final PoetExtensions poetExtensions;
private final PoetExtension poetExtensions;
private final Logger log = LoggerFactory.getLogger(GeneratorTaskParams.class);

private GeneratorTaskParams(IntermediateModel model,
GeneratorPathProvider pathProvider) {
this.model = model;
this.pathProvider = pathProvider;
this.poetExtensions = new PoetExtensions(model);
this.poetExtensions = new PoetExtension(model);
}

public static GeneratorTaskParams create(IntermediateModel model, String sourceDirectory, String testDirectory) {
Expand All @@ -64,7 +64,7 @@ public GeneratorPathProvider getPathProvider() {
/**
* @return Extensions and convenience methods for Java Poet.
*/
public PoetExtensions getPoetExtensions() {
public PoetExtension getPoetExtensions() {
return poetExtensions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import software.amazon.awssdk.codegen.poet.builder.AsyncClientBuilderInterface;
import software.amazon.awssdk.codegen.poet.client.AsyncClientClass;
import software.amazon.awssdk.codegen.poet.client.AsyncClientInterface;
import software.amazon.awssdk.codegen.poet.client.DelegatingAsyncClientClass;
import software.amazon.awssdk.codegen.poet.endpointdiscovery.EndpointDiscoveryAsyncCacheLoaderGenerator;

public class AsyncClientGeneratorTasks extends BaseGeneratorTasks {
Expand All @@ -46,13 +47,21 @@ protected List<GeneratorTask> createTasks() throws Exception {
generatorTasks.add(createEndpointDiscoveryCacheLoaderTask());
}

if (model.getCustomizationConfig().isDelegateAsyncClientClass()) {
generatorTasks.add(createDecoratorClientClassTask());
}

return generatorTasks;
}

private GeneratorTask createClientClassTask() throws IOException {
return createPoetGeneratorTask(new AsyncClientClass(generatorTaskParams));
}

private GeneratorTask createDecoratorClientClassTask() throws IOException {
return createPoetGeneratorTask(new DelegatingAsyncClientClass(model));
}

private GeneratorTask createClientBuilderTask() throws IOException {
return createPoetGeneratorTask(new AsyncClientBuilderClass(model));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ public final class Constant {

public static final String APPROVED_SIMPLE_METHOD_VERBS = "(get|list|describe|lookup|batchGet).*";

public static final String ASYNC_STREAMING_INPUT_PARAM = "requestBody";
public static final String ASYNC_STREAMING_OUTPUT_PARAM = "asyncResponseTransformer";
public static final String SYNC_STREAMING_INPUT_PARAM = "requestBody";
public static final String SYNC_STREAMING_OUTPUT_PARAM = "responseTransformer";
public static final String EVENT_PUBLISHER_PARAM_NAME = "requestStream";
public static final String EVENT_RESPONSE_HANDLER_PARAM_NAME = "asyncResponseHandler";

private Constant() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ public class CustomizationConfig {

private RetryMode defaultRetryMode;


/**
* Whether to generate an abstract decorator class that delegates to the async service client
*/
private boolean delegateAsyncClientClass;

private CustomizationConfig() {
}
Expand Down Expand Up @@ -502,4 +505,12 @@ public ServiceConfig getServiceConfig() {
public void setServiceConfig(ServiceConfig serviceConfig) {
this.serviceConfig = serviceConfig;
}

public boolean isDelegateAsyncClientClass() {
return delegateAsyncClientClass;
}

public void setDelegateAsyncClientClass(boolean delegateAsyncClientClass) {
this.delegateAsyncClientClass = delegateAsyncClientClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,8 @@ public String getFullWaitersPackageName() {
public String getFullWaitersInternalPackageName() {
return joinPackageNames(getFullWaitersPackageName(), "internal");
}

public String getFullInternalPackageName() {
return joinPackageNames(getFullClientPackageName(), "internal");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
/**
* Extension and convenience methods to Poet that use the intermediate model.
*/
public class PoetExtensions {
public class PoetExtension {
Copy link
Contributor Author

@zoewangg zoewangg May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkstyle suddenly failed due to plural name (guess it's because I made changes to this class)


private final IntermediateModel model;

public PoetExtensions(IntermediateModel model) {
public PoetExtension(IntermediateModel model) {
this.model = model;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.STATIC;
import static software.amazon.awssdk.codegen.internal.Constant.EVENT_PUBLISHER_PARAM_NAME;
import static software.amazon.awssdk.codegen.poet.client.ClientClassUtils.addS3ArnableFieldCode;
import static software.amazon.awssdk.codegen.poet.client.ClientClassUtils.applyPaginatorUserAgentMethod;
import static software.amazon.awssdk.codegen.poet.client.ClientClassUtils.applySignerOverrideMethod;
Expand Down Expand Up @@ -58,7 +59,7 @@
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
import software.amazon.awssdk.codegen.model.service.AuthType;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.poet.StaticImport;
import software.amazon.awssdk.codegen.poet.client.specs.ProtocolSpec;
Expand All @@ -85,7 +86,7 @@

public final class AsyncClientClass extends AsyncClientInterface {
private final IntermediateModel model;
private final PoetExtensions poetExtensions;
private final PoetExtension poetExtensions;
private final ClassName className;
private final ProtocolSpec protocolSpec;

Expand Down Expand Up @@ -420,7 +421,8 @@ private TypeName eventStreamType(ShapeModel shapeModel) {
return poetExtensions.getModelClass(shapeModel.getShapeName());
}

private MethodSpec utilitiesMethod() {
@Override
protected MethodSpec utilitiesMethod() {
UtilitiesMethod config = model.getCustomizationConfig().getUtilitiesMethod();
ClassName returnType = PoetUtils.classNameFromFqcn(config.getReturnType());
String instanceClass = config.getInstanceType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package software.amazon.awssdk.codegen.poet.client;

import static java.util.stream.Collectors.toList;
import static software.amazon.awssdk.codegen.internal.Constant.ASYNC_STREAMING_INPUT_PARAM;
import static software.amazon.awssdk.codegen.internal.Constant.EVENT_PUBLISHER_PARAM_NAME;
import static software.amazon.awssdk.codegen.internal.Constant.EVENT_RESPONSE_HANDLER_PARAM_NAME;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
Expand Down Expand Up @@ -43,7 +46,7 @@
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.poet.eventstream.EventStreamUtils;
import software.amazon.awssdk.codegen.poet.model.DeprecationUtils;
Expand All @@ -57,21 +60,20 @@
public class AsyncClientInterface implements ClassSpec {

public static final TypeVariableName STREAMING_TYPE_VARIABLE = TypeVariableName.get("ReturnT");
protected static final String EVENT_PUBLISHER_PARAM_NAME = "requestStream";

protected final IntermediateModel model;
protected final ClassName className;
protected final String clientPackageName;
private final String modelPackage;
private final PoetExtensions poetExtensions;
private final PoetExtension poetExtensions;

public AsyncClientInterface(IntermediateModel model) {
this.modelPackage = model.getMetadata().getFullModelPackageName();
this.clientPackageName = model.getMetadata().getFullClientPackageName();
this.model = model;
this.className = ClassName.get(model.getMetadata().getFullClientPackageName(),
model.getMetadata().getAsyncInterface());
this.poetExtensions = new PoetExtensions(model);
this.poetExtensions = new PoetExtension(model);
}

@Override
Expand Down Expand Up @@ -304,7 +306,7 @@ private MethodSpec traditionalMethod(OperationModel opModel) {
.addJavadoc(opModel.getDocs(model, ClientType.ASYNC));

if (opModel.hasStreamingInput()) {
builder.addParameter(ClassName.get(AsyncRequestBody.class), "requestBody");
builder.addParameter(ClassName.get(AsyncRequestBody.class), ASYNC_STREAMING_INPUT_PARAM);
} else if (opModel.hasEventStreamInput()) {
String eventStreamShapeName = EventStreamUtils.getEventStreamInRequest(opModel.getInputShape())
.getShapeName();
Expand All @@ -319,7 +321,7 @@ private MethodSpec traditionalMethod(OperationModel opModel) {
.get(ClassName.get(AsyncResponseTransformer.class), responsePojoType, STREAMING_TYPE_VARIABLE);
builder.addParameter(asyncResponseHandlerType, "asyncResponseTransformer");
} else if (opModel.hasEventStreamOutput()) {
builder.addParameter(poetExtensions.eventStreamResponseHandlerType(opModel), "asyncResponseHandler");
builder.addParameter(poetExtensions.eventStreamResponseHandlerType(opModel), EVENT_RESPONSE_HANDLER_PARAM_NAME);
}
return operationBody(builder, opModel).build();
}
Expand Down Expand Up @@ -448,26 +450,34 @@ private String consumerBuilderJavadoc(OperationModel opModel, SimpleMethodOverlo
return opModel.getDocs(model, ClientType.ASYNC, overload, new DocConfiguration().isConsumerBuilder(true));
}

private MethodSpec utilitiesMethod() {
protected MethodSpec utilitiesMethod() {
UtilitiesMethod config = model.getCustomizationConfig().getUtilitiesMethod();
ClassName returnType = PoetUtils.classNameFromFqcn(config.getReturnType());

return MethodSpec.methodBuilder(UtilitiesMethod.METHOD_NAME)
.returns(returnType)
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.DEFAULT)
.addStatement("throw new $T()", UnsupportedOperationException.class)
.addJavadoc("Creates an instance of {@link $T} object with the "
+ "configuration set on this client.", returnType)
.build();
MethodSpec.Builder builder = MethodSpec.methodBuilder(UtilitiesMethod.METHOD_NAME)
.returns(returnType)
.addModifiers(Modifier.PUBLIC)
.addJavadoc("Creates an instance of {@link $T} object with the "
+ "configuration set on this client.", returnType);
return utilitiesOperationBody(builder).build();
}

private MethodSpec waiterMethod() {
return MethodSpec.methodBuilder("waiter")
.addModifiers(Modifier.PUBLIC, Modifier.DEFAULT)
.returns(poetExtensions.getAsyncWaiterInterface())
.addStatement("throw new $T()", UnsupportedOperationException.class)
.addJavadoc(WaiterDocs.waiterMethodInClient(poetExtensions.getAsyncWaiterInterface()))
.build();
protected MethodSpec.Builder utilitiesOperationBody(MethodSpec.Builder builder) {
return builder.addModifiers(Modifier.DEFAULT).addStatement("throw new $T()", UnsupportedOperationException.class);
}

protected MethodSpec waiterMethod() {
MethodSpec.Builder builder = MethodSpec.methodBuilder("waiter")
.addModifiers(Modifier.PUBLIC)
.returns(poetExtensions.getAsyncWaiterInterface())
.addJavadoc(WaiterDocs.waiterMethodInClient(
poetExtensions.getAsyncWaiterInterface()));

return waiterOperationBody(builder).build();
}

protected MethodSpec.Builder waiterOperationBody(MethodSpec.Builder builder) {
return builder.addModifiers(Modifier.DEFAULT, Modifier.PUBLIC)
.addStatement("throw new $T()", UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
import software.amazon.awssdk.codegen.model.service.HostPrefixProcessor;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.core.ApiName;
import software.amazon.awssdk.core.signer.Signer;
Expand Down Expand Up @@ -85,7 +85,7 @@ static MethodSpec consumerBuilderVariant(MethodSpec spec, String javadoc) {
return result.build();
}

static MethodSpec applyPaginatorUserAgentMethod(PoetExtensions poetExtensions, IntermediateModel model) {
static MethodSpec applyPaginatorUserAgentMethod(PoetExtension poetExtensions, IntermediateModel model) {

TypeVariableName typeVariableName =
TypeVariableName.get("T", poetExtensions.getModelClass(model.getSdkRequestBaseClassName()));
Expand Down Expand Up @@ -119,7 +119,7 @@ static MethodSpec applyPaginatorUserAgentMethod(PoetExtensions poetExtensions, I
.build();
}

static MethodSpec applySignerOverrideMethod(PoetExtensions poetExtensions, IntermediateModel model) {
static MethodSpec applySignerOverrideMethod(PoetExtension poetExtensions, IntermediateModel model) {
String signerOverrideVariable = "signerOverride";

TypeVariableName typeVariableName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.regions.Region;

public class ClientSimpleMethodsIntegrationTests implements ClassSpec {

private final IntermediateModel model;
private final PoetExtensions poetExtensions;
private final PoetExtension poetExtensions;

public ClientSimpleMethodsIntegrationTests(IntermediateModel model) {
this.model = model;
this.poetExtensions = new PoetExtensions(model);
this.poetExtensions = new PoetExtension(model);
}

@Override
Expand Down
Loading