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 Upgrade1to2Command for Set shape #1569

Merged
merged 1 commit into from
Jan 9, 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 @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> lines = new ArrayList<>(Arrays.asList(contents.split("\\r?\\n")));
lines.remove(lineNumber - 1);
Expand Down Expand Up @@ -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);
}
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, }