From 8dcd661285b70c48c6c3067797c4be4191301df5 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 | 26 ++++++++++++++++ .../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, 86 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..496e92ed992 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 @@ -46,6 +46,7 @@ import software.amazon.smithy.model.loader.ModelAssembler; import software.amazon.smithy.model.loader.Prelude; 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 +322,14 @@ public Void stringShape(StringShape shape) { return null; } + @Override + public Void setShape(SetShape shape) { + getDefault(shape); + 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 +434,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 +477,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, }