-
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 validation events decorators (#1774)
* Add validation events decorators Adds a new interface `ValidationEventDecorator` that libraries can implement and plug into Smithy by making those discoverable using the java builtin [`ServiceLoader`](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html). All of the `ValidationEventDecorator` instances are loaded and each `ValidationEvent` is passed to each of the decorators. The decorators can make use of the `ValidationEvent#eventId()` method to quickly filter out the events that it knows how to decorate form the events it does not care about, and the `ValidationEvent#toBuilder()` to create a builder that can be used to mutate the instance. The current use case is to add hints to a know set of events to point the user into the right solution for it (e.g., you need to pull X or Y dependency for this unresolved shape). Since often these solutions might require specific details about the environment in which Smithy is being used, those decorators cannot be part of Smithy itself but now can be implemented by libraries and loaded automatically by Smithy in a similar way in which validators are currently loaded. For instance, a decorator that suggest the user to use or remove unused structures might look like this ```java @OverRide public ValidationEvent decorate(ValidationEvent ev) { if (ev.containsId("UnreferencedShape")) { if (ev.getMessage().contains("The structure ")) { return ev.toBuilder() .hint("Consider using this structure in your service or remove it from the model") .build(); } } return ev; } ``` * Load the decorators using its own factory In order to be able to decorate events that are returned before running the validation logic we need to pull the decoration logic up to the ModelAssambler itself. For this, a new factory was created to load the decorators instead of coupling this logic with the ValidatorFactory one and the assembler will take care of loading them and passing them down to the validation logic. * Fix an event duplication bug * Update smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecorator.java Co-authored-by: Michael Dowling <michael@mtdowling.com> * Update smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java Co-authored-by: Michael Dowling <michael@mtdowling.com> * Address pull request comments * Move back the responsibility of loading the decorators to the ValidatorFactory and pass it down to the ModelValidator from the ModelAssembler * Overwrite the events in place in the list instead of crating a new list and copying + decorating. * Create a new method to decorate a single event and use it as part of the stream that returns core error events. * Remove the posibility of manually adding decorators in the validator and assembler and just use the ones loaded by the `ValidatorFacotry`. We don't need this now and we can add it back if and when needed. * Address pull request comments * Do not catch exceptions thrown by loaded decorators. We will ship like this for now and add later if the need arises. * Add a new method to create the validation factory instead of using the existing one to avoid braking backwards compatibility. * Address pull request comments Remove the 'builtin' qualifier that doesn't make sense in this context --------- Co-authored-by: Michael Dowling <michael@mtdowling.com>
- Loading branch information
Showing
13 changed files
with
418 additions
and
22 deletions.
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
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
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
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
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
Oops, something went wrong.