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 a log message for unable to convert string enum to enum shape #1372

Merged
merged 1 commit into from
Aug 30, 2022
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 @@ -173,13 +173,13 @@ public static boolean canConvertToEnum(StringShape shape, boolean synthesizeEnum
}

EnumTrait trait = shape.expectTrait(EnumTrait.class);
if (!synthesizeEnumNames && trait.getValues().iterator().next().getName().isPresent()) {
if (!trait.hasNames() && !synthesizeEnumNames) {
LOGGER.info(String.format(
"Unable to convert string shape `%s` to enum shape because it doesn't define names. The "
+ "`synthesizeNames` option may be able to synthesize the names for you.",
shape.getId()
));
return true;
return false;
}

for (EnumDefinition definition : trait.getValues()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,50 @@ public void getEnumValues() {
Map<String, String> expected = MapUtils.of("BAR", "bar");
assertEquals(expected, shape.getEnumValues());
}

@Test
public void canConvertToEnumWithNamedEnumTrait() {
EnumTrait trait = EnumTrait.builder()
.addEnum(EnumDefinition.builder()
.value("foo&bar") // even if this value has invalid characters for a synthesized name
.name("BAR")
.build())
.build();
StringShape string = StringShape.builder()
.id("ns.foo#bar")
.addTrait(trait)
.build();
assertTrue(EnumShape.canConvertToEnum(string, false));
assertTrue(EnumShape.canConvertToEnum(string, true));
}

@Test
public void canConvertToEnumWithNamelessEnumTrait() {
EnumTrait trait = EnumTrait.builder()
.addEnum(EnumDefinition.builder()
.value("bar")
.build())
.build();
StringShape string = StringShape.builder()
.id("ns.foo#bar")
.addTrait(trait)
.build();
assertFalse(EnumShape.canConvertToEnum(string, false));
assertTrue(EnumShape.canConvertToEnum(string, true));
}

@Test
public void canConvertToEnumWithNonConvertableNamelessEnumTrait() {
EnumTrait trait = EnumTrait.builder()
.addEnum(EnumDefinition.builder()
.value("foo&bar")
.build())
.build();
StringShape string = StringShape.builder()
.id("ns.foo#bar")
.addTrait(trait)
.build();
assertFalse(EnumShape.canConvertToEnum(string, false));
assertFalse(EnumShape.canConvertToEnum(string, true));
}
}