Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trait codegen to support lists of enums #2420

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading