Skip to content

Commit

Permalink
Make operation implementations singletons (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmellema authored Jan 6, 2025
1 parent c2d8307 commit e5157ae
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
public class EffectiveAuthSchemeTest {
@Test
void generatedOperationHaveExpectedSchemes() {
assertEquals(new NoAuth().effectiveAuthSchemes(), List.of(NoAuthTrait.ID));
assertEquals(NoAuth.instance().effectiveAuthSchemes(), List.of(NoAuthTrait.ID));
assertEquals(
new AllAuth().effectiveAuthSchemes(),
AllAuth.instance().effectiveAuthSchemes(),
List.of(HttpApiKeyAuthTrait.ID, HttpBasicAuthTrait.ID));
assertEquals(new ScopedAuth().effectiveAuthSchemes(), List.of(HttpBasicAuthTrait.ID));
assertEquals(ScopedAuth.instance().effectiveAuthSchemes(), List.of(HttpBasicAuthTrait.ID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void accept(GenerateOperationDirective<CodeGenerationContext, JavaCodegen
public final class ${shape:T} implements ${operationType:C} {
${id:C|}
private static final ${shape:T} $$INSTANCE = new ${shape:T}();
private ${schema:C|}
${typeRegistrySection:C|}
Expand All @@ -66,6 +68,12 @@ public final class ${shape:T} implements ${operationType:C} {
${?inputStreamMember}private static final ${sdkSchema:T} INPUT_STREAM_MEMBER = ${inputType:T}.$$SCHEMA.member(${inputStreamMember:S});${/inputStreamMember}
${?outputStreamMember}private static final ${sdkSchema:T} OUTPUT_STREAM_MEMBER = ${outputType:T}.$$SCHEMA.member(${outputStreamMember:S});${/outputStreamMember}
public static ${shape:T} instance() {
return $$INSTANCE;
}
private ${shape:T}() {}
@Override
public ${sdkShapeBuilder:N}<${inputType:T}> inputBuilder() {
return ${inputType:T}.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void run() {
@Override
public ${?async}${future:T}<${/async}${output:T}${?async}>${/async} ${name:L}(${input:T} input, ${overrideConfig:T} overrideConfig) {${^async}
try {
${/async}return call(input, new ${operation:T}(), overrideConfig)${^async}.join()${/async};${^async}
${/async}return call(input, ${operation:T}.instance(), overrideConfig)${^async}.join()${/async};${^async}
} catch (${completionException:T} e) {
throw unwrapAndThrow(e);
}${/async}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public void run() {
* @return Paginator that can be used to retrieval paginated results.
*/
default ${paginator:T}<${output:T}> ${name:L}Paginator(${input:T} input) {
return ${paginator:T}.paginate(input, new ${operation:T}(), this::${name:L});
return ${paginator:T}.paginate(input, ${operation:T}.instance(), this::${name:L});
}
""";
writer.pushState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ private void generateStages(JavaWriter writer, List<String> stages) {
"""
@Override
public ${nextStage:L} add${operation:T}Operation(${asyncOperationType:T} operation) {
this.${operationFieldName:L} = s -> ${operationClass:T}.ofAsync("${operation:T}", operation::${operationFieldName:L}, new ${apiOperationClass:T}(), s);
this.${operationFieldName:L} = s -> ${operationClass:T}.ofAsync("${operation:T}", operation::${operationFieldName:L}, ${apiOperationClass:T}.instance(), s);
return this;
}
@Override
public ${nextStage:L} add${operation:T}Operation(${syncOperationType:T} operation) {
this.${operationFieldName:L} = s -> ${operationClass:T}.of("${operation:T}", operation::${operationFieldName:L}, new ${apiOperationClass:T}(), s);
this.${operationFieldName:L} = s -> ${operationClass:T}.of("${operation:T}", operation::${operationFieldName:L}, ${apiOperationClass:T}.instance(), s);
return this;
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static class PutItemState {
@Setup
public void setup() throws URISyntaxException {
endpoint = new URI("https://dynamodb.us-east-1.amazonaws.com");
operation = new PutItem();
operation = PutItem.instance();
protocol = new AwsJson1Protocol(ShapeId.from("com.amazonaws.dynamodb#DynamoDB_20120810"));
req = PutItemInput.builder().tableName("a").item(testItem.getValue()).build();
}
Expand All @@ -97,7 +97,7 @@ public void setup() throws URISyntaxException {
// This isn't actually used, but needed for the protocol implementation.
endpoint = new URI("https://dynamodb.us-east-1.amazonaws.com");
req = HttpRequest.builder().method("POST").uri(endpoint).build();
operation = new GetItem();
operation = GetItem.instance();
protocol = new AwsJson1Protocol(ShapeId.from("com.amazonaws.dynamodb#DynamoDB_20120810"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ public PersonDirectoryClientWithDefaults(PersonDirectoryClientWithDefaults.Build

@Override
public GetPersonImageOutput getPersonImage(GetPersonImageInput input, RequestOverrideConfig overrideConfig) {
return call(input, new GetPersonImage(), overrideConfig).join();
return call(input, GetPersonImage.instance(), overrideConfig).join();
}

@Override
public PutPersonOutput putPerson(PutPersonInput input, RequestOverrideConfig overrideConfig) {
return call(input, new PutPerson(), overrideConfig).join();
return call(input, PutPerson.instance(), overrideConfig).join();
}

@Override
public PutPersonImageOutput putPersonImage(PutPersonImageInput input, RequestOverrideConfig overrideConfig) {
return call(input, new PutPersonImage(), overrideConfig).join();
return call(input, PutPersonImage.instance(), overrideConfig).join();
}

static PersonDirectoryClientWithDefaults.Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,12 @@ private static List<HttpTestOperation> getTestOperations(
}

private static ApiOperation<?, ?> getApiOperation(SymbolProvider provider, Shape shape) {
var fqn = provider.toSymbol(shape).getFullName();
try {
var fqn = provider.toSymbol(shape).getFullName();
return CodegenUtils.getImplementationByName(ApiOperation.class, fqn).getDeclaredConstructor().newInstance();
} catch (InvocationTargetException
| InstantiationException
| IllegalAccessException
| NoSuchMethodException e) {
throw new RuntimeException(e);
var method = CodegenUtils.getClassForName(fqn).getMethod("instance");
return (ApiOperation<?, ?>) method.invoke(null);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException("Could not instantiate ApiOperation: " + fqn, e);
}
}

Expand Down

0 comments on commit e5157ae

Please sign in to comment.