Skip to content
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 @@ -20,15 +20,20 @@
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PROTECTED;
import static javax.lang.model.element.Modifier.PUBLIC;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
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.model.config.customization.UtilitiesMethod;
Expand Down Expand Up @@ -88,14 +93,9 @@ protected void addFields(TypeSpec.Builder type) {

@Override
protected void addAdditionalMethods(TypeSpec.Builder type) {
MethodSpec delegate = MethodSpec.methodBuilder("delegate")
.addModifiers(PUBLIC)
.addStatement("return this.delegate")
.returns(SdkClient.class)
.build();

type.addMethod(nameMethod())
.addMethod(delegate);
.addMethod(delegateMethod())
.addMethod(invokeMethod());
}

private MethodSpec nameMethod() {
Expand All @@ -107,6 +107,34 @@ private MethodSpec nameMethod() {
.build();
}

private MethodSpec delegateMethod() {
return MethodSpec.methodBuilder("delegate")
.addModifiers(PUBLIC)
.addStatement("return this.delegate")
.returns(SdkClient.class)
.build();
}

private MethodSpec invokeMethod() {
TypeVariableName requestTypeVariableName =
TypeVariableName.get("T", poetExtensions.getModelClass(model.getSdkRequestBaseClassName()));

TypeVariableName responseTypeVariableName = STREAMING_TYPE_VARIABLE;

ParameterizedTypeName functionTypeName = ParameterizedTypeName
.get(ClassName.get(Function.class), requestTypeVariableName, responseTypeVariableName);

return MethodSpec.methodBuilder("invokeOperation")
.addModifiers(PROTECTED)
.addParameter(requestTypeVariableName, "request")
.addParameter(functionTypeName, "operation")
.addTypeVariable(requestTypeVariableName)
.addTypeVariable(responseTypeVariableName)
.returns(responseTypeVariableName)
.addStatement("return operation.apply(request)")
.build();
}

@Override
protected MethodSpec serviceClientConfigMethod() {
return MethodSpec.methodBuilder("serviceClientConfiguration")
Expand Down Expand Up @@ -164,9 +192,19 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
builder.addModifiers(PUBLIC)
.addAnnotation(Override.class);

builder.addStatement("return delegate.$N($L)",
if (builder.parameters.size() < 1) {
throw new IllegalStateException("All client methods must have an argument");
}

List<ParameterSpec> parameters = new ArrayList<>(builder.parameters);
String requestParameter = parameters.remove(0).name;
String additionalParameters = String.format(", %s", parameters.stream().map(p -> p.name).collect(joining(", ")));

builder.addStatement("return invokeOperation($N, request -> delegate.$N(request$N))",
requestParameter,
opModel.getMethodName(),
builder.parameters.stream().map(p -> p.name).collect(joining(", ")));
parameters.isEmpty() ? "" : additionalParameters);

return builder;
}

Expand All @@ -175,7 +213,9 @@ protected MethodSpec.Builder paginatedMethodBody(MethodSpec.Builder builder, Ope
String methodName = PaginatorUtils.getPaginatedMethodName(opModel.getMethodName());
return builder.addModifiers(PUBLIC)
.addAnnotation(Override.class)
.addStatement("return delegate.$N($N)", methodName, opModel.getInput().getVariableName());
.addStatement("return invokeOperation($N, request -> delegate.$N(request))",
opModel.getInput().getVariableName(),
methodName);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
import static javax.lang.model.element.Modifier.ABSTRACT;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PROTECTED;
import static javax.lang.model.element.Modifier.PUBLIC;
import static software.amazon.awssdk.codegen.poet.client.AsyncClientInterface.STREAMING_TYPE_VARIABLE;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.codegen.docs.SimpleMethodOverload;
import software.amazon.awssdk.codegen.model.config.customization.UtilitiesMethod;
Expand Down Expand Up @@ -89,14 +96,9 @@ protected void addConsumerMethod(List<MethodSpec> specs, MethodSpec spec, Simple

@Override
protected void addAdditionalMethods(TypeSpec.Builder type) {
MethodSpec delegate = MethodSpec.methodBuilder("delegate")
.addModifiers(PUBLIC)
.addStatement("return this.delegate")
.returns(SdkClient.class)
.build();

type.addMethod(nameMethod())
.addMethod(delegate);
.addMethod(delegateMethod())
.addMethod(invokeMethod());
}

@Override
Expand Down Expand Up @@ -125,9 +127,19 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
builder.addModifiers(PUBLIC)
.addAnnotation(Override.class);

builder.addStatement("return delegate.$N($L)",
if (builder.parameters.size() < 1) {
throw new IllegalStateException("All client methods must have an argument");
}

List<ParameterSpec> operationParameters = new ArrayList<>(builder.parameters);
String requestParameter = operationParameters.remove(0).name;
String additionalParameters = String.format(", %s", operationParameters.stream().map(p -> p.name).collect(joining(", ")));

builder.addStatement("return invokeOperation($N, request -> delegate.$N(request$N))",
requestParameter,
opModel.getMethodName(),
builder.parameters.stream().map(p -> p.name).collect(joining(", ")));
operationParameters.isEmpty() ? "" : additionalParameters);

return builder;
}

Expand All @@ -136,7 +148,9 @@ protected MethodSpec.Builder paginatedMethodBody(MethodSpec.Builder builder, Ope
String methodName = PaginatorUtils.getPaginatedMethodName(opModel.getMethodName());
return builder.addModifiers(PUBLIC)
.addAnnotation(Override.class)
.addStatement("return delegate.$N($N)", methodName, opModel.getInput().getVariableName());
.addStatement("return invokeOperation($N, request -> delegate.$N(request))",
opModel.getInput().getVariableName(),
methodName);
}

@Override
Expand Down Expand Up @@ -167,6 +181,34 @@ private MethodSpec nameMethod() {
.build();
}

private MethodSpec delegateMethod() {
return MethodSpec.methodBuilder("delegate")
.addModifiers(PUBLIC)
.addStatement("return this.delegate")
.returns(SdkClient.class)
.build();
}

private MethodSpec invokeMethod() {
TypeVariableName requestTypeVariableName =
TypeVariableName.get("T", poetExtensions.getModelClass(model.getSdkRequestBaseClassName()));

TypeVariableName responseTypeVariableName = STREAMING_TYPE_VARIABLE;

ParameterizedTypeName functionTypeName = ParameterizedTypeName
.get(ClassName.get(Function.class), requestTypeVariableName, responseTypeVariableName);

return MethodSpec.methodBuilder("invokeOperation")
.addModifiers(PROTECTED)
.addParameter(requestTypeVariableName, "request")
.addParameter(functionTypeName, "operation")
.addTypeVariable(requestTypeVariableName)
.addTypeVariable(responseTypeVariableName)
.returns(responseTypeVariableName)
.addStatement("return operation.apply(request)")
.build();
}

@Override
protected MethodSpec serviceClientConfigMethod() {
return MethodSpec.methodBuilder("serviceClientConfiguration")
Expand Down
Loading