Skip to content

Commit

Permalink
Update union value generator
Browse files Browse the repository at this point in the history
  • Loading branch information
JordonPhillips committed Aug 24, 2020
1 parent 3dae644 commit 83a19b1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.model.shapes.SimpleShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.EnumTrait;
Expand Down Expand Up @@ -104,7 +105,7 @@ public void writeShapeValueInline(GoWriter writer, Shape shape, Node params) {
break;

default:
scalarWrapShapeValue(writer, shape, params);
writeScalarPointerInline(writer, shape, params);
}
}

Expand Down Expand Up @@ -136,15 +137,18 @@ protected void unionDeclShapeValue(GoWriter writer, UnionShape shape, ObjectNode
Optional<MemberShape> member = shape.getMember(entry.getKey().toString());
if (member.isPresent()) {
Shape target = model.expectShape(member.get().getTarget());
String exportedMemberName = symbol.getName() + symbolProvider.toMemberName(member.get());
Symbol castingFunction = SymbolUtils.createValueSymbolBuilder(
"As" + exportedMemberName,
Symbol memberSymbol = SymbolUtils.createValueSymbolBuilder(
symbol.getName() + symbolProvider.toMemberName(member.get()),
symbol.getNamespace()
).build();

writer.writeInline("$T(", castingFunction);
writeShapeValueInline(writer, target, entry.getValue());
writer.writeInline(")");
writer.writeInline("$T{Value: ", memberSymbol);
if (target instanceof SimpleShape) {
writeScalarValueInline(writer, target, entry.getValue());
} else {
writeShapeValueInline(writer, target, entry.getValue());
}
writer.writeInline("}");
}
return;
}
Expand Down Expand Up @@ -192,41 +196,25 @@ protected void mapDeclShapeValue(GoWriter writer, MapShape shape, Node params) {
* @param shape scalar shape.
* @param params parameters to fill the generated shape declaration.
*/
protected void scalarWrapShapeValue(GoWriter writer, Shape shape, Node params) {
protected void writeScalarPointerInline(GoWriter writer, Shape shape, Node params) {
boolean withPtrImport = true;
String closing = ")";
ShapeValueNodeVisitor visitor = new ShapeValueNodeVisitor(writer, this, shape);

switch (shape.getType()) {
case BOOLEAN:
writer.writeInline("ptr.Bool(");
break;

case BLOB:
if (shape.hasTrait(StreamingTrait.class)) {
writer.addUseImports(SmithyGoDependency.SMITHY_IO);
writer.addUseImports(SmithyGoDependency.BYTES);
writer.writeInline("smithyio.ReadSeekNopCloser{ReadSeeker: bytes.NewReader([]byte(");
closing += ")}";
} else {
writer.writeInline("[]byte(");
}
closing = "";
withPtrImport = false;
break;

case STRING:
// Enum are not pointers, but string alias values
if (shape.hasTrait(StreamingTrait.class)) {
writer.addUseImports(SmithyGoDependency.SMITHY_IO);
writer.addUseImports(SmithyGoDependency.STRINGS);
writer.writeInline("smithyio.ReadSeekNopCloser{ReadSeeker: strings.NewReader(");
closing += "}";

} else if (shape.hasTrait(EnumTrait.class)) {
Symbol enumSymbol = symbolProvider.toSymbol(shape);
writer.writeInline("$T(", enumSymbol);
if (shape.hasTrait(StreamingTrait.class) || shape.hasTrait(EnumTrait.class)) {
closing = "";
withPtrImport = false;

} else {
writer.writeInline("ptr.String(");
}
Expand Down Expand Up @@ -263,7 +251,7 @@ protected void scalarWrapShapeValue(GoWriter writer, Shape shape, Node params) {

case BIG_INTEGER:
case BIG_DECIMAL:
params.accept(visitor);
writeScalarValueInline(writer, shape, params);
return;

default:
Expand All @@ -274,7 +262,44 @@ protected void scalarWrapShapeValue(GoWriter writer, Shape shape, Node params) {
writer.addUseImports(SmithyGoDependency.SMITHY_PTR);
}

params.accept(visitor);
writeScalarValueInline(writer, shape, params);
writer.writeInline(closing);
}

protected void writeScalarValueInline(GoWriter writer, Shape shape, Node params) {
String closing = "";
switch (shape.getType()) {
case BLOB:
if (shape.hasTrait(StreamingTrait.class)) {
writer.addUseImports(SmithyGoDependency.SMITHY_IO);
writer.addUseImports(SmithyGoDependency.BYTES);
writer.writeInline("smithyio.ReadSeekNopCloser{ReadSeeker: bytes.NewReader([]byte(");
closing = "))}";
} else {
writer.writeInline("[]byte(");
closing = ")";
}
break;

case STRING:
// Enum are not pointers, but string alias values
if (shape.hasTrait(StreamingTrait.class)) {
writer.addUseImports(SmithyGoDependency.SMITHY_IO);
writer.addUseImports(SmithyGoDependency.STRINGS);
writer.writeInline("smithyio.ReadSeekNopCloser{ReadSeeker: strings.NewReader(");
closing = ")}";

} else if (shape.hasTrait(EnumTrait.class)) {
Symbol enumSymbol = symbolProvider.toSymbol(shape);
writer.writeInline("$T(", enumSymbol);
closing = ")";
}
break;

default:
break;
}
params.accept(new ShapeValueNodeVisitor(writer, this, shape));
writer.writeInline(closing);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.model.shapes.SimpleShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.utils.SetUtils;

/**
Expand Down Expand Up @@ -160,4 +162,15 @@ public static void writeSafeMemberAccessor(
consumer.accept(operand);
});
}

/**
* Determines whether a given shape will use a pointer when the shape is used as a union value.
*
* @param shape the shape to check
* @return true if the shape should use pointers
*/
public static boolean usesPointerWhenUnionValue(Shape shape) {
return !(shape instanceof SimpleShape) || shape.isBlobShape() || shape.hasTrait(EnumTrait.class)
|| shape.hasTrait(StreamingTrait.class);
}
}

0 comments on commit 83a19b1

Please sign in to comment.