Skip to content

Commit

Permalink
Merge pull request #289 from aws/smithy-17
Browse files Browse the repository at this point in the history
Migrate to Smithy 1.7.0
  • Loading branch information
skotambkar authored May 5, 2021
2 parents 2b2cada + 40794d1 commit 7d9ab12
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.AbstractShapeBuilder;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
Expand All @@ -45,6 +46,7 @@ private AddOperationShapes() {
*/
public static Model execute(Model model, ShapeId serviceShapeId) {
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
ServiceShape service = model.expectShape(serviceShapeId, ServiceShape.class);
TreeSet<OperationShape> operations = new TreeSet<>(topDownIndex.getContainedOperations(
model.expectShape(serviceShapeId)));

Expand All @@ -55,14 +57,14 @@ public static Model execute(Model model, ShapeId serviceShapeId) {
LOGGER.info(() -> "building unique input/output shapes for " + operationId);

StructureShape newInputShape = operation.getInput()
.map(shapeId -> cloneOperationShape(operationId, (StructureShape) model.expectShape(shapeId),
"Input"))
.orElseGet(() -> emptyOperationStructure(operationId, "Input"));
.map(shapeId -> cloneOperationShape(
service, operationId, (StructureShape) model.expectShape(shapeId), "Input"))
.orElseGet(() -> emptyOperationStructure(service, operationId, "Input"));

StructureShape newOutputShape = operation.getOutput()
.map(shapeId -> cloneOperationShape(operationId, (StructureShape) model.expectShape(shapeId),
"Output"))
.orElseGet(() -> emptyOperationStructure(operationId, "Output"));
.map(shapeId -> cloneOperationShape(
service, operationId, (StructureShape) model.expectShape(shapeId), "Output"))
.orElseGet(() -> emptyOperationStructure(service, operationId, "Output"));

// Add new input/output to model
modelBuilder.addShape(newInputShape);
Expand All @@ -78,19 +80,20 @@ public static Model execute(Model model, ShapeId serviceShapeId) {
return modelBuilder.build();
}

private static StructureShape emptyOperationStructure(ShapeId opShapeId, String suffix) {
private static StructureShape emptyOperationStructure(ServiceShape service, ShapeId opShapeId, String suffix) {
return StructureShape.builder()
.id(ShapeId.fromParts(CodegenUtils.getSyntheticTypeNamespace(), opShapeId.getName() + suffix))
.id(ShapeId.fromParts(CodegenUtils.getSyntheticTypeNamespace(), opShapeId.getName(service) + suffix))
.addTrait(SyntheticClone.builder().build())
.build();
}

private static StructureShape cloneOperationShape(
ServiceShape service,
ShapeId operationShapeId,
StructureShape structureShape,
String suffix
) {
return (StructureShape) cloneShape(structureShape, operationShapeId.getName() + suffix);
return (StructureShape) cloneShape(structureShape, operationShapeId.getName(service) + suffix);
}

private static Shape cloneShape(Shape shape, String cloneShapeName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.smithy.go.codegen.knowledge.GoPointableIndex;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.CollectionShape;
import software.amazon.smithy.model.shapes.MapShape;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
Expand Down Expand Up @@ -313,6 +314,21 @@ public static CollectionShape expectCollectionShape(Shape shape) {
throw new CodegenException("expect shape " + shape.getId() + " to be Collection, was " + shape.getType());
}

/**
* Returns the shape unpacked as a MapShape. Throws and exception if the passed in
* shape is not a map.
*
* @param shape the map shape.
* @return The unpacked MapShape.
*/
public static MapShape expectMapShape(Shape shape) {
if (shape instanceof MapShape) {
return (MapShape) (shape);
}

throw new CodegenException("expect shape " + shape.getId() + " to be Map, was " + shape.getType());
}

/**
* Comparator to sort ShapeMember lists alphabetically, with required members first followed by optional members.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ final class CodegenVisitor extends ShapeVisitor.Default<Void> {
service = settings.getService(model);
LOGGER.info(() -> "Generating Go client for service " + service.getId());

SymbolProvider resolvedProvider = GoCodegenPlugin.createSymbolProvider(model, settings.getModuleName());
SymbolProvider resolvedProvider = GoCodegenPlugin.createSymbolProvider(model, settings);
for (GoIntegration integration : integrations) {
resolvedProvider = integration.decorateSymbolProvider(settings, model, resolvedProvider);
}
Expand Down Expand Up @@ -237,7 +237,7 @@ public Void structureShape(StructureShape shape) {
}
Symbol symbol = symbolProvider.toSymbol(shape);
writers.useShapeWriter(shape, writer -> new StructureGenerator(
model, symbolProvider, writer, shape, symbol).run());
model, symbolProvider, writer, service, shape, symbol).run());
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public void execute(PluginContext context) {
/**
* Creates a Go symbol provider.
*
* @param model The model to generate symbols for.
* @param rootModuleName The name of the package root.
* @param model The model to generate symbols for.
* @param settings The Gosettings to use to create symbol provider
* @return Returns the created provider.
*/
public static SymbolProvider createSymbolProvider(Model model, String rootModuleName) {
return new SymbolVisitor(model, rootModuleName);
public static SymbolProvider createSymbolProvider(Model model, GoSettings settings) {
return new SymbolVisitor(model, settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,21 @@ public void run() {

// Write out the input and output structures. These are written out here to prevent naming conflicts with other
// shapes in the model.
new StructureGenerator(model, symbolProvider, writer, inputShape, inputSymbol)
new StructureGenerator(model, symbolProvider, writer, service, inputShape, inputSymbol)
.renderStructure(() -> {
}, true);

// The output structure gets a metadata member added.
Symbol metadataSymbol = SymbolUtils.createValueSymbolBuilder("Metadata", SmithyGoDependency.SMITHY_MIDDLEWARE)
.build();
new StructureGenerator(model, symbolProvider, writer, outputShape, outputSymbol).renderStructure(() -> {
if (outputShape.getMemberNames().size() != 0) {
writer.write("");
}
writer.writeDocs("Metadata pertaining to the operation's result.");
writer.write("ResultMetadata $T", metadataSymbol);
});
new StructureGenerator(model, symbolProvider, writer, service, outputShape, outputSymbol)
.renderStructure(() -> {
if (outputShape.getMemberNames().size() != 0) {
writer.write("");
}
writer.writeDocs("Metadata pertaining to the operation's result.");
writer.write("ResultMetadata $T", metadataSymbol);
});

// Generate operation protocol middleware helper function
generateAddOperationMiddleware();
Expand Down Expand Up @@ -212,15 +213,13 @@ private void generateOperationProtocolMiddlewareAdders() {

// Add request serializer middleware
String serializerMiddlewareName = ProtocolGenerator.getSerializeMiddlewareName(
operation.getId(),
protocolGenerator.getProtocolName());
operation.getId(), service, protocolGenerator.getProtocolName());
writer.write("err = stack.Serialize.Add(&$L{}, middleware.After)", serializerMiddlewareName);
writer.write("if err != nil { return err }");

// Adds response deserializer middleware
String deserializerMiddlewareName = ProtocolGenerator.getDeserializeMiddlewareName(
operation.getId(),
protocolGenerator.getProtocolName());
operation.getId(), service, protocolGenerator.getProtocolName());
writer.write("err = stack.Deserialize.Add(&$L{}, middleware.After)", deserializerMiddlewareName);
writer.write("if err != nil { return err }");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static GoDependency relativePackage(
private static final class Versions {
private static final String GO_STDLIB = "1.15";
private static final String GO_CMP = "v0.5.4";
private static final String SMITHY_GO = "v1.3.1";
private static final String SMITHY_GO = "v1.3.2-0.20210505063801-56a3fe683671";
private static final String GO_JMESPATH = "v0.4.0";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.utils.MapUtils;
Expand All @@ -42,17 +43,20 @@ final class StructureGenerator implements Runnable {
private final GoWriter writer;
private final StructureShape shape;
private final Symbol symbol;
private final ServiceShape service;

StructureGenerator(
Model model,
SymbolProvider symbolProvider,
GoWriter writer,
ServiceShape service,
StructureShape shape,
Symbol symbol
) {
this.model = model;
this.symbolProvider = symbolProvider;
this.writer = writer;
this.service = service;
this.shape = shape;
this.symbol = symbol;
}
Expand Down Expand Up @@ -146,7 +150,7 @@ private void renderErrorStructure() {
writer.write("return *e.Message");
});
writer.write("func (e *$L) ErrorCode() string { return $S }",
structureSymbol.getName(), shape.getId().getName());
structureSymbol.getName(), shape.getId().getName(service));

String fault = "smithy.FaultUnknown";
if (errorTrait.isClientError()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ final class SymbolVisitor implements SymbolProvider, ShapeVisitor<Symbol> {
private final ReservedWordSymbolProvider.Escaper errorMemberEscaper;
private final Map<ShapeId, ReservedWordSymbolProvider.Escaper> structureSpecificMemberEscapers = new HashMap<>();
private final GoPointableIndex pointableIndex;
private final GoSettings settings;


SymbolVisitor(Model model, String rootModuleName) {
SymbolVisitor(Model model, GoSettings settings) {
this.model = model;
this.rootModuleName = rootModuleName;
this.typesPackageName = rootModuleName + "/types";
this.settings = settings;
this.rootModuleName = settings.getModuleName();
this.typesPackageName = this.rootModuleName + "/types";
this.pointableIndex = GoPointableIndex.of(model);

// Reserve the generated names for union members, including the unknown case.
Expand Down Expand Up @@ -223,7 +224,8 @@ private String formatUnionMemberName(UnionShape union, MemberShape member) {
}

private String getDefaultShapeName(Shape shape) {
return StringUtils.capitalize(removeLeadingInvalidIdentCharacters(shape.getId().getName()));
ServiceShape serviceShape = model.expectShape(settings.getService(), ServiceShape.class);
return StringUtils.capitalize(removeLeadingInvalidIdentCharacters(shape.getId().getName(serviceShape)));
}

private String getDefaultMemberName(MemberShape shape) {
Expand Down Expand Up @@ -406,7 +408,8 @@ private Symbol createBigSymbol(Shape shape, String symbolName) {

@Override
public Symbol documentShape(DocumentShape shape) {
return symbolBuilderFor(shape, "Document", SmithyGoDependency.SMITHY)
String name = getDefaultShapeName(shape);
return symbolBuilderFor(shape, name, SmithyGoDependency.SMITHY)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ protected final void generateDeserFunction(
GoWriter writer = context.getWriter();

Symbol symbol = symbolProvider.toSymbol(shape);
String functionName = ProtocolGenerator.getDocumentDeserializerFunctionName(shape, context.getProtocolName());

String functionName = ProtocolGenerator.getDocumentDeserializerFunctionName(
shape, context.getService(), context.getProtocolName());

String additionalArguments = getAdditionalArguments().entrySet().stream()
.map(entry -> String.format(", %s %s", entry.getKey(), entry.getValue()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ private void generateSerFunction(
GoWriter writer = context.getWriter();

Symbol symbol = symbolProvider.toSymbol(shape);
String functionName = ProtocolGenerator.getDocumentSerializerFunctionName(shape, context.getProtocolName());

String functionName = ProtocolGenerator.getDocumentSerializerFunctionName(
shape, context.getService(), context.getProtocolName());

String additionalArguments = getAdditionalSerArguments().entrySet().stream()
.map(entry -> String.format(", %s %s", entry.getKey(), entry.getValue()))
Expand Down
Loading

0 comments on commit 7d9ab12

Please sign in to comment.