Skip to content

Paginated methods returning publisher or iterable implement logic in … #4046

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
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 @@ -28,6 +28,6 @@ class CommonGeneratorTasks extends CompositeGeneratorTask {
new ModelClassGeneratorTasks(params),
new PackageInfoGeneratorTasks(params),
new BaseExceptionClassGeneratorTasks(params),
new ClientOptionsClassGeneratorTasks(params));
new CommonInternalGeneratorTasks(params));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,35 @@

package software.amazon.awssdk.codegen.emitters.tasks;

import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import software.amazon.awssdk.codegen.emitters.GeneratorTask;
import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams;
import software.amazon.awssdk.codegen.emitters.PoetGeneratorTask;
import software.amazon.awssdk.codegen.poet.client.SdkClientOptions;
import software.amazon.awssdk.codegen.poet.common.UserAgentUtilsSpec;

public class ClientOptionsClassGeneratorTasks extends BaseGeneratorTasks {
public class CommonInternalGeneratorTasks extends BaseGeneratorTasks {
private final GeneratorTaskParams params;

public ClientOptionsClassGeneratorTasks(GeneratorTaskParams params) {
public CommonInternalGeneratorTasks(GeneratorTaskParams params) {
super(params);
this.params = params;
}

@Override
protected List<GeneratorTask> createTasks() throws Exception {
return Collections.singletonList(
new PoetGeneratorTask(clientOptionsDir(), params.getModel().getFileHeader(), new SdkClientOptions(params.getModel()))
);
return Arrays.asList(createClientOptionTask(), createUserAgentTask());
}

private PoetGeneratorTask createClientOptionTask() {
return new PoetGeneratorTask(clientOptionsDir(), params.getModel().getFileHeader(),
new SdkClientOptions(params.getModel()));
}

private PoetGeneratorTask createUserAgentTask() {
return new PoetGeneratorTask(clientOptionsDir(), params.getModel().getFileHeader(),
new UserAgentUtilsSpec(params.getModel()));
}

private String clientOptionsDir() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public ClassName getServiceConfigClass() {
+ model.getMetadata().getServiceName() + "ServiceClientConfiguration");
}

public ClassName getUserAgentClass() {
return ClassName.get(model.getMetadata().getFullClientInternalPackageName(), "UserAgentUtils");
}

/**
* @param operationName Name of the operation
* @return A Poet {@link ClassName} for the response type of a paginated operation in the base service package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
import static software.amazon.awssdk.codegen.poet.client.SyncClientClass.getProtocolSpecs;

Expand Down Expand Up @@ -155,10 +154,6 @@ protected void addAdditionalMethods(TypeSpec.Builder type) {
.addMethod(protocolSpec.initProtocolFactory(model))
.addMethod(resolveMetricPublishersMethod());

if (model.hasPaginators()) {
type.addMethod(applyPaginatorUserAgentMethod(poetExtensions, model));
}

if (model.containsRequestSigners() || model.containsRequestEventStreams() || hasStreamingV4AuthOperations()) {
type.addMethod(applySignerOverrideMethod(poetExtensions, model));
type.addMethod(isSignerOverriddenOnClientMethod());
Expand Down Expand Up @@ -196,9 +191,6 @@ protected List<MethodSpec> operations() {
private Stream<MethodSpec> operations(OperationModel opModel) {
List<MethodSpec> methods = new ArrayList<>();
methods.add(traditionalMethod(opModel));
if (opModel.isPaginated()) {
methods.add(paginatedTraditionalMethod(opModel));
}
return methods.stream();
}

Expand Down Expand Up @@ -420,14 +412,6 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
return builder;
}

@Override
protected MethodSpec.Builder paginatedMethodBody(MethodSpec.Builder builder, OperationModel opModel) {
return builder.addModifiers(PUBLIC)
.addStatement("return new $T(this, applyPaginatorUserAgent($L))",
poetExtensions.getResponseClassForPaginatedAsyncOperation(opModel.getOperationName()),
opModel.getInput().getVariableName());
}

@Override
public ClassName className() {
return className;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,34 @@ private MethodSpec builder() {
*/
protected Iterable<MethodSpec> operations() {
return model.getOperations().values().stream()
.flatMap(this::operationsAndSimpleMethods)
.flatMap(this::operationsWithVariants)
.sorted(Comparator.comparing(m -> m.name))
.collect(toList());
}

private Stream<MethodSpec> operationsAndSimpleMethods(OperationModel operationModel) {
private Stream<MethodSpec> operationsWithVariants(OperationModel operationModel) {
List<MethodSpec> methods = new ArrayList<>();
methods.addAll(traditionalMethods(operationModel));
methods.addAll(overloadMethods(operationModel));
methods.addAll(traditionalMethodWithConsumerVariant(operationModel));
methods.addAll(overloadedMethods(operationModel));
methods.addAll(paginatedMethods(operationModel));
return methods.stream()
// Add Deprecated annotation if needed to all overloads
.map(m -> DeprecationUtils.checkDeprecated(operationModel, m));
}

/**
* Generates the traditional method for an operation (i.e. one that takes a request and returns a response).
*/
private List<MethodSpec> traditionalMethodWithConsumerVariant(OperationModel opModel) {
List<MethodSpec> methods = new ArrayList<>();
String consumerBuilderJavadoc = consumerBuilderJavadoc(opModel, SimpleMethodOverload.NORMAL);

methods.add(traditionalMethod(opModel));
methods.add(ClientClassUtils.consumerBuilderVariant(methods.get(0), consumerBuilderJavadoc));

return methods;
}

private List<MethodSpec> paginatedMethods(OperationModel opModel) {
List<MethodSpec> methods = new ArrayList<>();

Expand Down Expand Up @@ -251,7 +264,9 @@ protected MethodSpec paginatedTraditionalMethod(OperationModel opModel) {

protected MethodSpec.Builder paginatedMethodBody(MethodSpec.Builder builder, OperationModel operationModel) {
return builder.addModifiers(DEFAULT, PUBLIC)
.addStatement("throw new $T()", UnsupportedOperationException.class);
.addStatement("return new $T(this, $L)",
poetExtensions.getResponseClassForPaginatedAsyncOperation(operationModel.getOperationName()),
operationModel.getInput().getVariableName());
}

private MethodSpec paginatedSimpleMethod(OperationModel opModel) {
Expand All @@ -274,7 +289,7 @@ private MethodSpec paginatedSimpleMethod(OperationModel opModel) {
* @param opModel Operation to generate simple methods for.
* @return All simple method overloads for a given operation.
*/
private List<MethodSpec> overloadMethods(OperationModel opModel) {
private List<MethodSpec> overloadedMethods(OperationModel opModel) {
String consumerBuilderFileJavadoc = consumerBuilderJavadoc(opModel, SimpleMethodOverload.FILE);

List<MethodSpec> methodOverloads = new ArrayList<>();
Expand Down Expand Up @@ -312,20 +327,6 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
.addStatement("throw new $T()", UnsupportedOperationException.class);
}

/**
* Generates the traditional method for an operation (i.e. one that takes a request and returns a response).
*/
private List<MethodSpec> traditionalMethods(OperationModel opModel) {
List<MethodSpec> methods = new ArrayList<>();

methods.add(traditionalMethod(opModel));

String consumerBuilderJavadoc = consumerBuilderJavadoc(opModel, SimpleMethodOverload.NORMAL);
methods.add(ClientClassUtils.consumerBuilderVariant(methods.get(0), consumerBuilderJavadoc));

return methods;
}

protected MethodSpec traditionalMethod(OperationModel opModel) {
ClassName responsePojoType = getPojoResponseType(opModel);
ClassName requestType = ClassName.get(modelPackage, opModel.getInput().getVariableType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@
import software.amazon.awssdk.codegen.model.service.HostPrefixProcessor;
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;
import software.amazon.awssdk.core.util.VersionInfo;
import software.amazon.awssdk.utils.HostnameValidator;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.Validate;

final class ClientClassUtils {
private static final String PAGINATOR_USER_AGENT = "PAGINATED";

private ClientClassUtils() {
}
Expand Down Expand Up @@ -85,40 +82,6 @@ static MethodSpec consumerBuilderVariant(MethodSpec spec, String javadoc) {
return result.build();
}

static MethodSpec applyPaginatorUserAgentMethod(PoetExtension poetExtensions, IntermediateModel model) {

TypeVariableName typeVariableName =
TypeVariableName.get("T", poetExtensions.getModelClass(model.getSdkRequestBaseClassName()));

ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName
.get(ClassName.get(Consumer.class), ClassName.get(AwsRequestOverrideConfiguration.Builder.class));

CodeBlock codeBlock = CodeBlock.builder()
.addStatement("$T userAgentApplier = b -> b.addApiName($T.builder().version"
+ "($T.SDK_VERSION).name($S).build())",
parameterizedTypeName, ApiName.class,
VersionInfo.class,
PAGINATOR_USER_AGENT)
.addStatement("$T overrideConfiguration =\n"
+ " request.overrideConfiguration().map(c -> c.toBuilder()"
+ ".applyMutation"
+ "(userAgentApplier).build())\n"
+ " .orElse((AwsRequestOverrideConfiguration.builder()"
+ ".applyMutation"
+ "(userAgentApplier).build()))", AwsRequestOverrideConfiguration.class)
.addStatement("return (T) request.toBuilder().overrideConfiguration"
+ "(overrideConfiguration).build()")
.build();

return MethodSpec.methodBuilder("applyPaginatorUserAgent")
.addModifiers(Modifier.PRIVATE)
.addParameter(typeVariableName, "request")
.addTypeVariable(typeVariableName)
.addCode(codeBlock)
.returns(typeVariableName)
.build();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.utils.Validate;

Expand Down Expand Up @@ -171,9 +170,6 @@ protected List<MethodSpec> operations() {
private Stream<MethodSpec> operations(OperationModel opModel) {
List<MethodSpec> methods = new ArrayList<>();
methods.add(traditionalMethod(opModel));
if (opModel.isPaginated()) {
methods.add(paginatedTraditionalMethod(opModel));
}
return methods.stream();
}

Expand Down Expand Up @@ -212,14 +208,6 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
return builder;
}

@Override
protected MethodSpec.Builder paginatedMethodBody(MethodSpec.Builder builder, OperationModel opModel) {
String methodName = PaginatorUtils.getPaginatedMethodName(opModel.getMethodName());
return builder.addModifiers(PUBLIC)
.addAnnotation(Override.class)
.addStatement("return delegate.$N($N)", methodName, opModel.getInput().getVariableName());
}

@Override
protected MethodSpec.Builder utilitiesOperationBody(MethodSpec.Builder builder) {
return builder.addAnnotation(Override.class).addStatement("return delegate.$N()", UtilitiesMethod.METHOD_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.codegen.poet.client;

import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
Expand All @@ -31,16 +32,17 @@
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.codegen.docs.SimpleMethodOverload;
import software.amazon.awssdk.codegen.model.config.customization.UtilitiesMethod;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.utils.Validate;

Expand Down Expand Up @@ -112,6 +114,23 @@ protected void addCloseMethod(TypeSpec.Builder type) {
type.addMethod(method);
}

@Override
protected List<MethodSpec> operations() {
return model.getOperations().values().stream()
// TODO Sync not supported for event streaming yet. Revisit after sync/async merge
.filter(o -> !o.hasEventStreamInput())
.filter(o -> !o.hasEventStreamOutput())
.flatMap(this::operations)
.sorted(Comparator.comparing(m -> m.name))
.collect(toList());
}

private Stream<MethodSpec> operations(OperationModel opModel) {
List<MethodSpec> methods = new ArrayList<>();
methods.add(traditionalMethod(opModel));
return methods.stream();
}

@Override
public ClassName className() {
return className;
Expand All @@ -122,6 +141,11 @@ protected MethodSpec.Builder simpleMethodModifier(MethodSpec.Builder builder) {
return builder.addAnnotation(Override.class);
}

protected MethodSpec traditionalMethod(OperationModel opModel) {
MethodSpec.Builder builder = operationMethodSignature(model, opModel);
return operationBody(builder, opModel).build();
}

@Override
protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, OperationModel opModel) {
builder.addModifiers(PUBLIC)
Expand All @@ -143,14 +167,6 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
return builder;
}

@Override
protected MethodSpec.Builder paginatedMethodBody(MethodSpec.Builder builder, OperationModel opModel) {
String methodName = PaginatorUtils.getPaginatedMethodName(opModel.getMethodName());
return builder.addModifiers(PUBLIC)
.addAnnotation(Override.class)
.addStatement("return delegate.$N($N)", methodName, opModel.getInput().getVariableName());
}

@Override
protected MethodSpec.Builder utilitiesOperationBody(MethodSpec.Builder builder) {
return builder.addAnnotation(Override.class).addStatement("return delegate.$N()", UtilitiesMethod.METHOD_NAME);
Expand Down
Loading