-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NodeValidationVisitor plugin for @Uniqueitems
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
47 changes: 47 additions & 0 deletions
47
...y-model/src/main/java/software/amazon/smithy/model/validation/node/UniqueItemsPlugin.java
This file contains 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,47 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.model.validation.node; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import software.amazon.smithy.model.node.ArrayNode; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.shapes.CollectionShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.traits.UniqueItemsTrait; | ||
|
||
/** | ||
* Validates that items in lists with the `@uniqueItems` trait are unique. | ||
*/ | ||
final class UniqueItemsPlugin extends MemberAndShapeTraitPlugin<CollectionShape, ArrayNode, UniqueItemsTrait> { | ||
UniqueItemsPlugin() { | ||
super(CollectionShape.class, ArrayNode.class, UniqueItemsTrait.class); | ||
} | ||
|
||
@Override | ||
protected void check(Shape shape, UniqueItemsTrait trait, ArrayNode value, Context context, Emitter emitter) { | ||
Set<Node> uniqueNodes = new HashSet<>(value.getElements()); | ||
if (uniqueNodes.size() != value.size()) { | ||
List<Node> duplicateNodes = new ArrayList<>(value.getElements()); | ||
for (Node uniqueNode : uniqueNodes) { | ||
// Collections remove the first encountered entry, so our unique | ||
// nodes through a set will leave behind any duplicates. | ||
duplicateNodes.remove(uniqueNode); | ||
} | ||
|
||
Set<String> duplicateValues = new LinkedHashSet<>(); | ||
for (Node duplicateNode : duplicateNodes) { | ||
duplicateValues.add(duplicateNode.toString()); | ||
} | ||
emitter.accept(value, String.format( | ||
"Value provided for `%s` must have unique items, but the following items had multiple entries: " | ||
+ "[`%s`]", shape.getId(), String.join("`, `", duplicateValues))); | ||
} | ||
} | ||
} |
This file contains 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 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