Skip to content

Commit

Permalink
Fix Upgrade1to2Command for Set shape
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFossAWS committed Jan 9, 2023
1 parent d2f9c0f commit 8b39977
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> lines = new ArrayList<>(Arrays.asList(contents.split("\\r?\\n")));
lines.remove(lineNumber - 1);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ list BadSparseList {
member: NonBoxedInteger,
}

set BadSparseSet {
@uniqueItems
list BadSparseSet {
member: NonBoxedInteger,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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, }
Original file line number Diff line number Diff line change
@@ -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, }

0 comments on commit 8b39977

Please sign in to comment.