Skip to content

Commit

Permalink
Allow empty enum with non-empty mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS authored and mtdowling committed Jun 2, 2023
1 parent e08012c commit 29ee927
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ private EnumShape(Builder builder) {
}
}

private EnumShape(Builder builder, Map<String, MemberShape> members) {
super(builder);
this.members = members;
validateMemberShapeIds();
if (members.size() < 1) {
throw new SourceException("enum shapes must have at least one member", getSourceLocation());
}
}

/**
* Gets a map of enum member names to their corresponding values.
*
Expand Down Expand Up @@ -291,8 +300,11 @@ public static final class Builder extends StringShape.Builder {

@Override
public EnumShape build() {
addSyntheticEnumTrait();
return new EnumShape(this);
// Collect members from enum and mixins
Map<String, MemberShape> aggregatedMembers =
NamedMemberUtils.computeMixinMembers(getMixins(), members, getId(), getSourceLocation());
addSyntheticEnumTrait(aggregatedMembers.values());
return new EnumShape(this, aggregatedMembers);
}

/**
Expand All @@ -302,10 +314,10 @@ public EnumShape build() {
* the enum trait, without having to manually add the trait or risk that it
* gets serialized.
*/
private void addSyntheticEnumTrait() {
private void addSyntheticEnumTrait(Collection<MemberShape> memberShapes) {
SyntheticEnumTrait.Builder builder = SyntheticEnumTrait.builder();
builder.sourceLocation(getSourceLocation());
for (MemberShape member : members.get().values()) {
for (MemberShape member : memberShapes) {
try {
builder.addEnum(EnumShape.enumDefinitionFromMember(member));
} catch (IllegalStateException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[ERROR] smithy.example#EmptyEnumMixin: enum must have at least one entry | Model
[ERROR] smithy.example#EmptyEnum: Cannot apply mixin to smithy.example#EmptyEnum: smithy.example#EmptyEnumMixin not found | Model
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$version: "2.0"

namespace smithy.example

@mixin
enum EmptyEnumMixin {}

enum EmptyEnum with [EmptyEnumMixin] {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$version: "2.0"

namespace smithy.example

@mixin
enum NonEmptyEnumMixin {
VALUE = "value"
}

enum NonEmptyEnum with [NonEmptyEnumMixin] {}

0 comments on commit 29ee927

Please sign in to comment.