From 8b39977d8a02df0fff2576b9ef24f256c53cecbc Mon Sep 17 00:00:00 2001 From: fossand Date: Fri, 6 Jan 2023 22:12:55 -0800 Subject: [PATCH] Fix Upgrade1to2Command for Set shape --- .../cli/commands/Upgrade1to2Command.java | 30 ++++++++++++++++++ .../cli/commands/upgrade/cases/box.v2.smithy | 3 +- .../cli/commands/upgrade/cases/list.v1.smithy | 27 ++++++++++++++++ .../cli/commands/upgrade/cases/list.v2.smithy | 31 +++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v1.smithy create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v2.smithy diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/Upgrade1to2Command.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/Upgrade1to2Command.java index 2a03c4764f4..4f6a2beb21d 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/Upgrade1to2Command.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/Upgrade1to2Command.java @@ -31,6 +31,7 @@ import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.function.Function; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -45,7 +46,9 @@ import software.amazon.smithy.model.SourceLocation; import software.amazon.smithy.model.loader.ModelAssembler; import software.amazon.smithy.model.loader.Prelude; +import software.amazon.smithy.model.shapes.ListShape; import software.amazon.smithy.model.shapes.MemberShape; +import software.amazon.smithy.model.shapes.SetShape; import software.amazon.smithy.model.shapes.Shape; import software.amazon.smithy.model.shapes.ShapeType; import software.amazon.smithy.model.shapes.ShapeVisitor; @@ -321,6 +324,16 @@ public Void stringShape(StringShape shape) { return null; } + @Override + public Void listShape(ListShape shape) { + getDefault(shape); + if (shape instanceof SetShape) { + writer.erase(shape.getSourceLocation(), 3); // `set` has 3 characters + writer.insert(shape.getSourceLocation(), "@uniqueItems" + System.lineSeparator() + "list"); + } + return null; + } + private String serializeEnum(StringShape shape) { // Strip all the traits from the shape except the enum trait. // We're leaving the other traits where they are in the model @@ -425,6 +438,13 @@ private void insertLine(int lineNumber, String line) { contents = String.join(System.lineSeparator(), lines); } + private void insert(SourceLocation from, String value) { + IdlAwareSimpleParser parser = new IdlAwareSimpleParser(contents); + parser.rewind(from); + int fromPosition = parser.position(); + contents = contents.substring(0, fromPosition) + value + contents.substring(fromPosition); + } + private void eraseLine(int lineNumber) { List lines = new ArrayList<>(Arrays.asList(contents.split("\\r?\\n"))); lines.remove(lineNumber - 1); @@ -461,6 +481,16 @@ private void erase(SourceLocation from, SourceLocation to) { contents = contents.substring(0, fromPosition) + contents.substring(toPosition); } + private void erase(SourceLocation from, int size) { + IdlAwareSimpleParser parser = new IdlAwareSimpleParser(contents); + parser.rewind(from); + int fromPosition = parser.position(); + SourceLocation to = new SourceLocation(from.getFilename(), from.getLine(), from.getColumn() + size); + parser.rewind(to); + int toPosition = parser.position(); + contents = contents.substring(0, fromPosition) + contents.substring(toPosition); + } + private void replace(int from, int to, String with) { contents = contents.substring(0, from) + with + contents.substring(to); } diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/box.v2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/box.v2.smithy index 8633ec27dac..6e1f45cb48d 100644 --- a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/box.v2.smithy +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/box.v2.smithy @@ -24,7 +24,8 @@ list BadSparseList { member: NonBoxedInteger, } -set BadSparseSet { +@uniqueItems +list BadSparseSet { member: NonBoxedInteger, } diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v1.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v1.smithy new file mode 100644 index 00000000000..c1c2dc95d8b --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v1.smithy @@ -0,0 +1,27 @@ +$version: "1.0" + +namespace com.example + +integer NonBoxedInteger + +list SparseList { + @box + member: NonBoxedInteger, +} + +set SparseSet { + @box + member: NonBoxedInteger, +} + +list SingleLineList { member: NonBoxedInteger, } + +set SingleLineSet { member: NonBoxedInteger } + + set SetWithIndentation { member: NonBoxedInteger } + +set SetWithComment { // This comment is here + member: NonBoxedInteger, +} + +@tags(["set"]) set MultipleSetCharacters { member: NonBoxedInteger, } diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v2.smithy new file mode 100644 index 00000000000..0f9bb0b0f5b --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/list.v2.smithy @@ -0,0 +1,31 @@ +$version: "2.0" + +namespace com.example + +@default(0) +integer NonBoxedInteger + +list SparseList { + member: NonBoxedInteger, +} + +@uniqueItems +list SparseSet { + member: NonBoxedInteger, +} + +list SingleLineList { member: NonBoxedInteger, } + +@uniqueItems +list SingleLineSet { member: NonBoxedInteger } + + @uniqueItems +list SetWithIndentation { member: NonBoxedInteger } + +@uniqueItems +list SetWithComment { // This comment is here + member: NonBoxedInteger, +} + +@tags(["set"]) @uniqueItems +list MultipleSetCharacters { member: NonBoxedInteger, }