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

Improve dulplicate member handling #513

Merged
merged 1 commit into from
Jul 29, 2020
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 @@ -458,9 +458,11 @@ private void parseCollection(ShapeId id, SourceLocation location, CollectionShap
}

private void parseMembers(ShapeId id, Set<String> requiredMembers) {
Set<String> definedMembers = new HashSet<>();
Set<String> remaining = requiredMembers.isEmpty()
? requiredMembers
: new HashSet<>(requiredMembers);

ws();
expect('{');
// Don't keep any previous state of captured doc comments when
Expand All @@ -469,9 +471,7 @@ private void parseMembers(ShapeId id, Set<String> requiredMembers) {
ws();

if (peek() != '}') {
// Remove the parsed member from the remaining set to detect
// when duplicates are found, or when members are missing.
remaining.remove(parseMember(id, remaining));
parseMember(id, requiredMembers, definedMembers, remaining);
while (!eof()) {
ws();
if (peek() == ',') {
Expand All @@ -484,15 +484,7 @@ private void parseMembers(ShapeId id, Set<String> requiredMembers) {
// Trailing comma: "," "}"
break;
}

// Special casing to detect invalid members early on, even
// after draining all the valid members. This keeps builders
// from raising confusing errors about invalid members.
if (remaining.isEmpty() && !requiredMembers.isEmpty()) {
parseMember(id, requiredMembers);
} else {
remaining.remove(parseMember(id, remaining));
}
parseMember(id, requiredMembers, definedMembers, remaining);
} else {
// Assume '}'; break to enforce.
break;
Expand All @@ -508,14 +500,22 @@ private void parseMembers(ShapeId id, Set<String> requiredMembers) {
expect('}');
}

private String parseMember(ShapeId parent, Set<String> requiredMembers) {
private String parseMember(ShapeId parent, Set<String> required, Set<String> defined, Set<String> remaining) {
// Parse optional member traits.
List<TraitEntry> memberTraits = parseDocsAndTraits();
SourceLocation memberLocation = currentLocation();
String memberName = ParserUtils.parseIdentifier(this);

if (defined.contains(memberName)) {
// This is a duplicate member name.
throw syntax("Duplicate member of " + parent + ": '" + memberName + '\'');
}

defined.add(memberName);
remaining.remove(memberName);

// Only enforce "allowedMembers" if it isn't empty.
if (!requiredMembers.isEmpty() && !requiredMembers.contains(memberName)) {
if (!required.isEmpty() && !required.contains(memberName)) {
throw syntax("Unexpected member of " + parent + ": '" + memberName + '\'');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ERROR] -: Duplicate shape definition for `com.foo#List$member` found at | Model
[ERROR] -: Parse error at line 5, column 11 near `: `: Duplicate member of com.foo#List: 'member' | Model
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ERROR] -: Parse error at line 5, column 8 near `: `: Unexpected member of com.foo#Map: 'key' | Model
[ERROR] -: Parse error at line 5, column 8 near `: `: Duplicate member of com.foo#Map: 'key' | Model
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ERROR] -: Duplicate shape definition for `com.foo#Set$member` found at | Model
[ERROR] -: Parse error at line 5, column 11 near `: `: Duplicate member of com.foo#Set: 'member' | Model
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ERROR] -: Duplicate shape definition for `com.foo#Struct$foo` found at | Model
[ERROR] -: Parse error at line 5, column 8 near `: `: Duplicate member of com.foo#Struct: 'foo' | Model