Skip to content

Commit

Permalink
Fix trait codegen to support lists of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
hpmellema committed Oct 15, 2024
1 parent 0291f02 commit cff969c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.traits.defaults.StructDefaultsTrait;
import com.example.traits.documents.DocumentTrait;
import com.example.traits.documents.StructWithNestedDocumentTrait;
import com.example.traits.enums.EnumListMemberTrait;
import com.example.traits.enums.IntEnumTrait;
import com.example.traits.enums.StringEnumTrait;
import com.example.traits.enums.SuitTrait;
Expand Down Expand Up @@ -74,6 +75,9 @@ static Stream<Arguments> createTraitTests() {
Arguments.of(StringEnumTrait.ID, Node.from("no")),
Arguments.of(IntEnumTrait.ID, Node.from(2)),
Arguments.of(SuitTrait.ID, Node.from("clubs")),
Arguments.of(EnumListMemberTrait.ID, ObjectNode.objectNodeBuilder()
.withMember("value", ArrayNode.fromStrings("some", "none", "some"))
.build()),
// Lists
Arguments.of(NumberListTrait.ID, ArrayNode.fromNodes(
Node.from(1), Node.from(2), Node.from(3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.example.traits.defaults.StructDefaultsTrait;
import com.example.traits.documents.DocumentTrait;
import com.example.traits.documents.StructWithNestedDocumentTrait;
import com.example.traits.enums.EnumListMemberTrait;
import com.example.traits.enums.IntEnumTrait;
import com.example.traits.enums.SomeEnum;
import com.example.traits.enums.StringEnumTrait;
import com.example.traits.enums.SuitTrait;
import com.example.traits.idref.IdRefListTrait;
Expand Down Expand Up @@ -95,6 +97,8 @@ static Stream<Arguments> loadsModelTests() {
MapUtils.of("getValue", 1, "getEnumValue", IntEnumTrait.IntEnum.YES)),
Arguments.of("enums/string-enum-compatibility.smithy", SuitTrait.class,
MapUtils.of("getEnumValue", SuitTrait.Suit.CLUB, "getValue", "club")),
Arguments.of("enums/enum-list-member-trait.smithy", EnumListMemberTrait.class,
MapUtils.of("getValue", Optional.of(ListUtils.of(SomeEnum.SOME, SomeEnum.NONE, SomeEnum.SOME)))),
// Id Refs
Arguments.of("idref/idref-string.smithy", IdRefStringTrait.class,
MapUtils.of("getValue", TARGET_ONE)),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$version: "2.0"

namespace test.smithy.traitcodegen

use test.smithy.traitcodegen.enums#EnumListMemberTrait

@EnumListMemberTrait(value: ["some", "none", "some"])
structure myStruct {}
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,22 @@ public Void listShape(ListShape shape) {

// Clear all
writer.openBlock("public Builder clear$U() {", "}", memberName, () -> {
writer.write("$L.get().clear();", memberName);
writer.write("this.$L.get().clear();", memberName);
writer.writeWithNoFormatting("return this;");
}).newLine();

// Set one
writer.openBlock("public Builder add$U($T value) {", "}",
memberName, symbolProvider.toSymbol(shape.getMember()), () -> {
writer.write("$L.get().add(value);", memberName);
writer.write("this.$L.get().add(value);", memberName);
writer.write("return this;");
}).newLine();

// Remove one
writer.openBlock("public Builder remove$U($T value) {", "}",
memberName, symbolProvider.toSymbol(shape.getMember()),
() -> {
writer.write("$L.get().remove(value);", memberName);
writer.write("this.$L.get().remove(value);", memberName);
writer.write("return this;");
}).newLine();
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import software.amazon.smithy.model.shapes.TimestampShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.TimestampFormatTrait;
import software.amazon.smithy.traitcodegen.SymbolProperties;
import software.amazon.smithy.traitcodegen.TraitCodegenUtils;
import software.amazon.smithy.traitcodegen.writer.TraitCodegenWriter;
import software.amazon.smithy.utils.StringUtils;
Expand Down Expand Up @@ -106,6 +107,34 @@ public Void stringShape(StringShape shape) {
return null;
}

@Override
public Void enumShape(EnumShape shape) {
// Enum traits do not need this method, only nested enums.
if (symbol.getProperty(SymbolProperties.BASE_SYMBOL).isPresent()) {
return null;
}
writeFromNodeJavaDoc();
writer.openBlock("public static $T fromNode($T node) {", "}", symbol, Node.class, () -> {
writer.write("return from(node.expectStringNode().getValue());");
});
writer.newLine();
return null;
}

@Override
public Void intEnumShape(IntEnumShape shape) {
// Enum traits do not need this method, only nested enums.
if (symbol.getProperty(SymbolProperties.BASE_SYMBOL).isPresent()) {
return null;
}
writeFromNodeJavaDoc();
writer.openBlock("public static $T fromNode($T node) {", "}", symbol, Node.class, () -> {
writer.writeWithNoFormatting("return from(node.expectNumberNode().getValue().intValue());");
});
writer.newLine();
return null;
}

@Override
protected Void numberShape(NumberShape shape) {
// Number shapes do not create a from node method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


public class TraitCodegenPluginTest {
private static final int EXPECTED_NUMBER_OF_FILES = 58;
private static final int EXPECTED_NUMBER_OF_FILES = 60;

private MockManifest manifest;
private Model model;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$version: "2.0"

namespace test.smithy.traitcodegen.enums

@trait(selector: "structure")
structure EnumListMemberTrait {
value: EnumList
}

list EnumList {
member: SomeEnum
}

enum SomeEnum {
SOME = "some"
NONE = "none"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ documents/struct-with-nested-document.smithy
enums/enum-trait.smithy
enums/int-enum-trait.smithy
enums/string-enum-compatibility.smithy
enums/enum-list-member-trait.smithy
idref/idref-list.smithy
idref/idref-map.smithy
idref/idref-string.smithy
Expand Down

0 comments on commit cff969c

Please sign in to comment.