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

Convert to internal trait from tag #1739

Merged
merged 3 commits into from
Apr 20, 2023
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 @@ -29,6 +29,7 @@
import software.amazon.smithy.model.traits.EnumDefinition;
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.TagsTrait;
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
Expand Down Expand Up @@ -199,6 +200,12 @@ public static boolean canConvertToEnum(StringShape shape, boolean synthesizeEnum
/**
* Converts an enum definition to the equivalent enum member shape.
*
* <p>If an enum definition is marked as deprecated, the DeprecatedTrait
* is applied to the converted enum member shape.
*
* <p>If an enum definition has an "internal" tag, the InternalTrait is
* applied to the converted enum member shape.
*
* @param parentId The {@link ShapeId} of the enum shape.
* @param synthesizeName Whether to synthesize a name if possible.
* @return An optional member shape representing the enum definition,
Expand Down Expand Up @@ -233,6 +240,9 @@ static Optional<MemberShape> memberFromEnumDefinition(
if (definition.isDeprecated()) {
builder.addTrait(DeprecatedTrait.builder().build());
}
if (definition.hasTag("internal")) {
builder.addTrait(new InternalTrait());
}

return Optional.of(builder.build());
} catch (ShapeIdSyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,10 @@ public Model changeShapeType(
*
* <p>A member will be created on the shape for each entry in the {@link EnumTrait}.
*
* <p>When the enum definition from the enum trait has been marked as deprecated, or
* tagged as "internal", the corresponding enum shape member will be marked with the
* DeprecatedTrait or InternalTrait accordingly.
*
* @param model Model to transform.
* @param synthesizeEnumNames Whether enums without names should have names synthesized if possible.
* @return Returns the transformed model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import software.amazon.smithy.model.traits.EnumDefinition;
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.TagsTrait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
import software.amazon.smithy.model.traits.synthetic.SyntheticEnumTrait;
Expand Down Expand Up @@ -125,6 +126,7 @@ public void addMemberFromEnumTrait() {
EnumDefinition enumDefinition = EnumDefinition.builder()
.name("foo")
.value("bar")
.tags(ListUtils.of("internal"))
.build();
EnumShape shape = builder.setMembersFromEnumTrait(EnumTrait.builder().addEnum(enumDefinition).build()).build();

Expand All @@ -133,6 +135,8 @@ public void addMemberFromEnumTrait() {
.id(shape.getId().withMember("foo"))
.target(UnitTypeTrait.UNIT)
.addTrait(EnumValueTrait.builder().stringValue("bar").build())
.addTrait(new InternalTrait())
.addTrait(TagsTrait.builder().addValue("internal").build())
.build());

assertTrue(shape.hasTrait(EnumTrait.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import software.amazon.smithy.model.traits.EnumDefinition;
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.TagsTrait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
import software.amazon.smithy.model.traits.synthetic.SyntheticEnumTrait;
import software.amazon.smithy.utils.ListUtils;
Expand Down Expand Up @@ -269,6 +271,7 @@ public void canConvertStringToEnum() {
.addEnum(EnumDefinition.builder()
.name("foo")
.value("bar")
.addTag("internal")
.build())
.build();
SourceLocation source = new SourceLocation("/foo", 1, 1);
Expand All @@ -280,14 +283,15 @@ public void canConvertStringToEnum() {
.build();
Model model = Model.assembler().addShape(startShape).assemble().unwrap();
Model result = ModelTransformer.create().changeShapeType(model, MapUtils.of(id, ShapeType.ENUM));

assertThat(result.expectShape(id).getType(), Matchers.is(ShapeType.ENUM));
assertThat(result.expectShape(id).getSourceLocation(), Matchers.equalTo(source));
assertThat(result.expectShape(id).members(), Matchers.hasSize(1));
assertThat(result.expectShape(id).members().iterator().next(), Matchers.equalTo(MemberShape.builder()
.id(id.withMember("foo"))
.target(UnitTypeTrait.UNIT)
.addTrait(EnumValueTrait.builder().stringValue("bar").build())
.addTrait(new InternalTrait())
.addTrait(TagsTrait.builder().addValue("internal").build())
.build()));
}

Expand Down