Skip to content

Commit

Permalink
Add application protocol client defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
JordonPhillips committed May 13, 2020
1 parent 5415803 commit f0aaeae
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public Void serviceShape(ServiceShape shape) {

writers.useShapeWriter(shape, serviceWriter -> {
new ServiceGenerator(settings, model, symbolProvider, serviceWriter, shape, integrations,
runtimePlugins).run();
runtimePlugins, applicationProtocol).run();

// Generate each operation for the service. We do this here instead of via the operation visitor method to
// limit it to the operations bound to the service.
Expand All @@ -227,7 +227,7 @@ public Void serviceShape(ServiceShape shape) {
Symbol operationSymbol = symbolProvider.toSymbol(operation);
writers.useShapeWriter(operation, operationWriter -> new OperationGenerator(
settings, model, symbolProvider, operationWriter, service, operation,
operationSymbol).run());
operationSymbol, applicationProtocol).run());
}
});
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum GoDependency implements SymbolDependencyContainer {
TIME("stdlib", "", "time", null, "1.14"),
FMT("stdlib", "", "fmt", null, "1.14"),
CONTEXT("stdlib", "", "context", null, "1.14"),
HTTP("stdlib", "", "net/http", null, "1.14"),

SMITHY("dependency", "github.com/awslabs/smithy-go", "github.com/awslabs/smithy-go",
"smithy", "v0.0.1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class OperationGenerator implements Runnable {
private final ServiceShape service;
private final OperationShape operation;
private final Symbol operationSymbol;
private final ApplicationProtocol applicationProtocol;

OperationGenerator(
GoSettings settings,
Expand All @@ -44,7 +45,8 @@ final class OperationGenerator implements Runnable {
GoWriter writer,
ServiceShape service,
OperationShape operation,
Symbol operationSymbol
Symbol operationSymbol,
ApplicationProtocol applicationProtocol
) {
this.settings = settings;
this.model = model;
Expand All @@ -53,6 +55,7 @@ final class OperationGenerator implements Runnable {
this.service = service;
this.operation = operation;
this.operationSymbol = operationSymbol;
this.applicationProtocol = applicationProtocol;
}

@Override
Expand All @@ -79,16 +82,19 @@ public void run() {
Symbol contextSymbol = SymbolUtils.createValueSymbolBuilder("Context", GoDependency.CONTEXT).build();
writer.openBlock("func (c $P) $T(ctx $T, params $P, opts ...func(*Options)) ($P, error) {", "}",
serviceSymbol, operationSymbol, contextSymbol, inputSymbol, outputSymbol, () -> {
// TODO: create middleware stack
constructStack();

writer.write("options := c.options.Copy()");
writer.openBlock("for _, fn := range optFns {", "}", () -> {
writer.write("fn(&options)");
});
writer.openBlock("for _, fn := range options.APIOptions {", "}", () -> {
writer.write("if err := fn(stack); err != nil { return nil, err }");
});
// TODO: resolve middleware stack
writer.write("return nil, nil");

writer.write("result, err := handler.Handle(ctx, params)");
writer.write("if err != nil { return nil, err }");
writer.write("return result.($P), nil", outputSymbol);
}).write("");

// Write out the input and output structures. These are written out here to prevent naming conflicts with other
Expand All @@ -104,4 +110,14 @@ public void run() {
writer.write("ResultMetadata $T", metadataSymbol);
});
}

private void constructStack() {
if (!applicationProtocol.isHttpProtocol()) {
throw new UnsupportedOperationException(
"Protocols other than HTTP are not yet implemented: " + applicationProtocol);
}
writer.addUseImports(GoDependency.SMITHY_MIDDLEWARE);
writer.addUseImports(GoDependency.SMITHY_HTTP_TRANSPORT);
writer.write("stack := middleware.NewStack($S, smithyhttp.NewStackRequest)", operationSymbol.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ final class ServiceGenerator implements Runnable {
private final ServiceShape service;
private final List<GoIntegration> integrations;
private final List<RuntimeClientPlugin> runtimePlugins;
private final ApplicationProtocol applicationProtocol;

ServiceGenerator(
GoSettings settings,
Expand All @@ -48,7 +49,8 @@ final class ServiceGenerator implements Runnable {
GoWriter writer,
ServiceShape service,
List<GoIntegration> integrations,
List<RuntimeClientPlugin> runtimePlugins
List<RuntimeClientPlugin> runtimePlugins,
ApplicationProtocol applicationProtocol
) {
this.settings = settings;
this.model = model;
Expand All @@ -57,6 +59,7 @@ final class ServiceGenerator implements Runnable {
this.service = service;
this.integrations = integrations;
this.runtimePlugins = runtimePlugins;
this.applicationProtocol = applicationProtocol;
}

@Override
Expand Down Expand Up @@ -127,9 +130,11 @@ private void generateConfig() {
integration.addConfigFields(settings, model, symbolProvider, writer);
}

// TODO: add application protocol defaults
generateApplicationProtocolConfig();
}).write("");

generateApplicationProtocolTypes();

writer.writeDocs("Copy creates a clone where the APIOptions list is deep copied.");
writer.openBlock("func (o $L) Copy() $L {", "}", CONFIG_NAME, CONFIG_NAME, () -> {
writer.write("to := o");
Expand All @@ -138,4 +143,26 @@ private void generateConfig() {
writer.write("return to");
});
}

private void generateApplicationProtocolConfig() {
ensureSupportedProtocol();
writer.writeDocs(
"The HTTP client to invoke API calls with. Defaults to client's default HTTP implementation if nil.");
writer.write("HTTPClient HTTPClient").write("");
}

private void generateApplicationProtocolTypes() {
ensureSupportedProtocol();
writer.openBlock("type HTTPClient interface {", "}", () -> {
writer.write("Do($P) ($P, error)", applicationProtocol.getRequestType(),
applicationProtocol.getResponseType());
}).write("");
}

private void ensureSupportedProtocol() {
if (!applicationProtocol.isHttpProtocol()) {
throw new UnsupportedOperationException(
"Protocols other than HTTP are not yet implemented: " + applicationProtocol);
}
}
}

0 comments on commit f0aaeae

Please sign in to comment.