-
Notifications
You must be signed in to change notification settings - Fork 244
Add exact match configuration to repeated shape name validator #1041
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
Merged
JordonPhillips
merged 2 commits into
smithy-lang:main
from
JordonPhillips:repeated-name
Jan 10, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
smithy-linters/src/main/java/software/amazon/smithy/linters/RepeatedShapeNameValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.smithy.linters; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.stream.Collectors; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.node.NodeMapper; | ||
| import software.amazon.smithy.model.shapes.Shape; | ||
| import software.amazon.smithy.model.shapes.StructureShape; | ||
| import software.amazon.smithy.model.shapes.UnionShape; | ||
| import software.amazon.smithy.model.validation.AbstractValidator; | ||
| import software.amazon.smithy.model.validation.ValidationEvent; | ||
| import software.amazon.smithy.model.validation.ValidatorService; | ||
|
|
||
| /** | ||
| * Validates that structure members and union member names do not | ||
| * repeat their shape name as prefixes of their member or tag names. | ||
| */ | ||
| public final class RepeatedShapeNameValidator extends AbstractValidator { | ||
|
|
||
| public static final class Config { | ||
| private boolean exactMatch = false; | ||
|
|
||
| public boolean getExactMatch() { | ||
| return exactMatch; | ||
| } | ||
|
|
||
| public void setExactMatch(boolean exactMatch) { | ||
| this.exactMatch = exactMatch; | ||
| } | ||
| } | ||
|
|
||
| public static final class Provider extends ValidatorService.Provider { | ||
| public Provider() { | ||
| super(RepeatedShapeNameValidator.class, node -> { | ||
| Config config = new NodeMapper().deserialize(node, Config.class); | ||
| return new RepeatedShapeNameValidator(config); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private final Config config; | ||
|
|
||
| private RepeatedShapeNameValidator(Config config) { | ||
| this.config = config; | ||
| } | ||
|
|
||
| @Override | ||
| public List<ValidationEvent> validate(Model model) { | ||
| List<ValidationEvent> events = new ArrayList<>(); | ||
| model.shapes(StructureShape.class) | ||
| .forEach(shape -> events.addAll(validateNames(model, shape, shape.getMemberNames()))); | ||
| model.shapes(UnionShape.class) | ||
| .forEach(shape -> events.addAll(validateNames(model, shape, shape.getMemberNames()))); | ||
| return events; | ||
| } | ||
|
|
||
| private List<ValidationEvent> validateNames(Model model, Shape shape, Collection<String> memberNames) { | ||
| String shapeName = shape.getId().getName(); | ||
| String lowerCaseShapeName = shapeName.toLowerCase(Locale.US); | ||
| return memberNames.stream() | ||
| .filter(memberName -> nameConflicts(lowerCaseShapeName, memberName)) | ||
| .map(memberName -> repeatedMemberName(model, shape, shapeName, memberName)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private boolean nameConflicts(String lowerCaseShapeName, String memberName) { | ||
| String lowerCaseMemberName = memberName.toLowerCase(Locale.US); | ||
| if (config.getExactMatch()) { | ||
| return lowerCaseMemberName.equals(lowerCaseShapeName); | ||
| } else { | ||
| return lowerCaseMemberName.startsWith(lowerCaseShapeName); | ||
| } | ||
| } | ||
|
|
||
| private ValidationEvent repeatedMemberName(Model model, Shape shape, String shapeName, String memberName) { | ||
| Shape member = model.expectShape(shape.getId().withMember(memberName)); | ||
| if (config.getExactMatch()) { | ||
| return warning(member, String.format( | ||
| "The `%s` %s shape repeats its name in the member `%s`; %2$s member names should not be " | ||
| + "equal to the %2$s name.", shapeName, shape.getType(), memberName)); | ||
| } else { | ||
| return warning(member, String.format( | ||
| "The `%s` %s shape repeats its name in the member `%s`; %2$s member names should not be " | ||
| + "prefixed with the %2$s name.", shapeName, shape.getType(), memberName)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...test/resources/software/amazon/smithy/linters/errorfiles/repeated-shape-name-exact.errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [WARNING] smithy.example#RepeatingStructure$repeatingStructure: The `RepeatingStructure` structure shape repeats its name in the member `repeatingStructure`; structure member names should not be equal to the structure name. | RepeatedShapeName | ||
| [WARNING] smithy.example#RepeatingUnion$repeatingUnion: The `RepeatingUnion` union shape repeats its name in the member `repeatingUnion`; union member names should not be equal to the union name. | RepeatedShapeName |
22 changes: 22 additions & 0 deletions
22
...test/resources/software/amazon/smithy/linters/errorfiles/repeated-shape-name-exact.smithy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| metadata validators = [{ | ||
| name: "RepeatedShapeName", | ||
| configuration: { | ||
| exactMatch: true | ||
| } | ||
| }] | ||
|
|
||
| namespace smithy.example | ||
|
|
||
| structure RepeatingStructure { | ||
| repeatingStructure: String, | ||
|
|
||
| // This is fine because it's not an exact match | ||
| repeatingStructureMember: String, | ||
| } | ||
|
|
||
| union RepeatingUnion { | ||
| repeatingUnion: String, | ||
|
|
||
| // This is fine because it's not an exact match | ||
| repeatingUnionMember: String, | ||
| } |
6 changes: 6 additions & 0 deletions
6
...s/src/test/resources/software/amazon/smithy/linters/errorfiles/repeated-shape-name.errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [WARNING] ns.foo#Invalid$InvalidBaz: The `Invalid` structure shape repeats its name in the member `InvalidBaz`; structure member names should not be prefixed with the structure name. | RepeatedShapeName | ||
| [WARNING] ns.foo#Invalid$invalidBar: The `Invalid` structure shape repeats its name in the member `invalidBar`; structure member names should not be prefixed with the structure name. | RepeatedShapeName | ||
| [WARNING] ns.foo#Invalid$invalidFoo: The `Invalid` structure shape repeats its name in the member `invalidFoo`; structure member names should not be prefixed with the structure name. | RepeatedShapeName | ||
| [WARNING] ns.foo#Invalid2$Invalid2Baz: The `Invalid2` union shape repeats its name in the member `Invalid2Baz`; union member names should not be prefixed with the union name. | RepeatedShapeName | ||
| [WARNING] ns.foo#Invalid2$invalid2Bar: The `Invalid2` union shape repeats its name in the member `invalid2Bar`; union member names should not be prefixed with the union name. | RepeatedShapeName | ||
| [WARNING] ns.foo#Invalid2$invalid2Foo: The `Invalid2` union shape repeats its name in the member `invalid2Foo`; union member names should not be prefixed with the union name. | RepeatedShapeName |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ | |
| "metadata": { | ||
| "validators": [ | ||
| { | ||
| "name": "StutteredShapeName" | ||
| "name": "RepeatedShapeName" | ||
| } | ||
| ] | ||
| } | ||
|
|
||
6 changes: 0 additions & 6 deletions
6
.../src/test/resources/software/amazon/smithy/linters/errorfiles/stuttered-shape-name.errors
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.