-
Notifications
You must be signed in to change notification settings - Fork 219
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
Add validation events decorators #1774
Merged
sugmanue
merged 8 commits into
smithy-lang:main
from
sugmanue:pluggable-validation-decorators
May 24, 2023
Merged
Add validation events decorators #1774
sugmanue
merged 8 commits into
smithy-lang:main
from
sugmanue:pluggable-validation-decorators
May 24, 2023
Conversation
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
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; } ```
kstich
reviewed
May 17, 2023
...hy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecorator.java
Show resolved
Hide resolved
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.
mtdowling
reviewed
May 22, 2023
...hy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecorator.java
Outdated
Show resolved
Hide resolved
…tion/ValidationEventDecorator.java Co-authored-by: Michael Dowling <[email protected]>
mtdowling
requested changes
May 22, 2023
...l/src/main/java/software/amazon/smithy/model/validation/ValidationEventDecoratorFactory.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelAssembler.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java
Outdated
Show resolved
Hide resolved
smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java
Outdated
Show resolved
Hide resolved
…/ModelValidator.java Co-authored-by: Michael Dowling <[email protected]>
* 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.
mtdowling
reviewed
May 23, 2023
smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidatorFactory.java
Show resolved
Hide resolved
* 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.
mtdowling
requested changes
May 24, 2023
smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidatorFactory.java
Outdated
Show resolved
Hide resolved
Remove the 'builtin' qualifier that doesn't make sense in this context
Approved offline after the last requested change to change |
syall
pushed a commit
to Xtansia/smithy
that referenced
this pull request
Aug 11, 2023
* 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 <[email protected]> * Update smithy-model/src/main/java/software/amazon/smithy/model/loader/ModelValidator.java Co-authored-by: Michael Dowling <[email protected]> * 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 <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description of changes:
Adds a new interface
ValidationEventDecorator
that libraries can implement and plug into Smithy by making those discoverable using the java builtinServiceLoader
. All of theValidationEventDecorator
instances are loaded and eachValidationEvent
is passed to each of the decorators. The decorators can make use of theValidationEvent#eventId()
method to quickly filter out the events that it knows how to decorate form the events it does not care about, and theValidationEvent#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
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.