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

Support internal trait when building synthetic enum trait #2106

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -18,6 +18,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
Expand All @@ -31,6 +32,7 @@
import software.amazon.smithy.model.traits.EnumTrait;
import software.amazon.smithy.model.traits.EnumValueTrait;
import software.amazon.smithy.model.traits.InternalTrait;
import software.amazon.smithy.model.traits.StringListTrait;
import software.amazon.smithy.model.traits.TagsTrait;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
Expand Down Expand Up @@ -286,8 +288,13 @@ static EnumDefinition enumDefinitionFromMember(MemberShape member) {
.orElseThrow(() -> new IllegalStateException("Enum definitions can only be made for string enums."));
builder.value(traitValue);
member.getTrait(DocumentationTrait.class).ifPresent(docTrait -> builder.documentation(docTrait.getValue()));
member.getTrait(TagsTrait.class).ifPresent(tagsTrait -> builder.tags(tagsTrait.getValues()));
member.getTrait(DeprecatedTrait.class).ifPresent(deprecatedTrait -> builder.deprecated(true));

Optional<List<String>> tags = member.getTrait(TagsTrait.class).map(StringListTrait::getValues);
JordonPhillips marked this conversation as resolved.
Show resolved Hide resolved
tags.ifPresent(builder::tags);
if (member.hasTrait(InternalTrait.class) && !tags.map(t -> t.contains("internal")).orElse(false)) {
builder.addTag("internal");
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,62 @@ public void canConvertToEnumWithNonConvertableNamelessEnumTrait() {
assertFalse(EnumShape.canConvertToEnum(string, false));
assertFalse(EnumShape.canConvertToEnum(string, true));
}

@Test
public void syntheticEnumSupportsDocumentation() {
EnumShape shape = EnumShape.builder()
.id("com.example#DocumentedEnum")
.addMember("documented", "foo", builder -> {
builder.addTrait(new DocumentationTrait("bar"));
})
.build();

SyntheticEnumTrait trait = shape.expectTrait(SyntheticEnumTrait.class);
assertEquals("bar", trait.getValues().get(0).getDocumentation().get());
}

@Test
public void syntheticEnumSupportsDeprecated() {
EnumShape shape = EnumShape.builder()
.id("com.example#DeprecatedEnum")
.addMember("deprecated", "foo", builder -> {
builder.addTrait(DeprecatedTrait.builder().build());
})
.build();

SyntheticEnumTrait trait = shape.expectTrait(SyntheticEnumTrait.class);
assertTrue(trait.getValues().get(0).isDeprecated());
}

@Test
public void syntheticEnumSupportsTags() {
EnumShape shape = EnumShape.builder()
.id("com.example#TaggedEnum")
.addMember("tagged", "foo", builder -> {
builder.addTrait(TagsTrait.builder().addValue("bar").build());
})
.build();

SyntheticEnumTrait trait = shape.expectTrait(SyntheticEnumTrait.class);
assertEquals(ListUtils.of("bar"), trait.getValues().get(0).getTags());
}

@Test
public void syntheticEnumSupportsInternal() {
EnumShape shape = EnumShape.builder()
.id("com.example#InternalEnum")
.addMember("withoutTag", "foo", builder -> {
builder.addTrait(new InternalTrait());
})
.addMember("withTag", "bar", builder -> {
builder.addTrait(new InternalTrait());
builder.addTrait(TagsTrait.builder().addValue("internal").build());
})
.build();

SyntheticEnumTrait trait = shape.expectTrait(SyntheticEnumTrait.class);
for (EnumDefinition definition : trait.getValues()) {
assertEquals(ListUtils.of("internal"), definition.getTags());
}
}
}