-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
HLRC: Use Optional in validation logic #33104
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6504612
HLRC: Use Optional in validation logic
hub-cap 1c6b618
Merge remote-tracking branch 'upstream/master' into hlrc_optional_val…
hub-cap 55d522c
Fix validation wording for clarification
hub-cap 2df9d9c
Merge remote-tracking branch 'upstream/master' into hlrc_optional_val…
hub-cap 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 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 |
---|---|---|
|
@@ -177,6 +177,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
@@ -1011,9 +1012,9 @@ protected final <Req extends Validatable, Resp> Resp performRequest(Req request, | |
RequestOptions options, | ||
CheckedFunction<Response, Resp, IOException> responseConverter, | ||
Set<Integer> ignores) throws IOException { | ||
ValidationException validationException = request.validate(); | ||
if (validationException != null && validationException.validationErrors().isEmpty() == false) { | ||
throw validationException; | ||
Optional<ValidationException> validationException = request.validate(); | ||
if (validationException != null && validationException.isPresent()) { | ||
throw validationException.get(); | ||
} | ||
return internalPerformRequest(request, requestConverter, options, responseConverter, ignores); | ||
} | ||
|
@@ -1106,9 +1107,9 @@ protected final <Req extends Validatable, Resp> void performRequestAsync(Req req | |
RequestOptions options, | ||
CheckedFunction<Response, Resp, IOException> responseConverter, | ||
ActionListener<Resp> listener, Set<Integer> ignores) { | ||
ValidationException validationException = request.validate(); | ||
if (validationException != null && validationException.validationErrors().isEmpty() == false) { | ||
listener.onFailure(validationException); | ||
Optional<ValidationException> validationException = request.validate(); | ||
if (validationException != null && validationException.isPresent()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
listener.onFailure(validationException.get()); | ||
return; | ||
} | ||
internalPerformRequestAsync(request, requestConverter, options, responseConverter, listener, ignores); | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The philosophy I've usually gone with is that you shouldn't null-check
Optional
s - just let it throw an NPE because you use an empty Optional to represent "no value" so a null value means something is misbehaving.The counterargument is that leaving the null-check here is probably safer, especially if we're converting code that used to use null to represent "no value" - but if we're going to go with that logic, we should probably still check for the
isEmpty()
case too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We used to the null check to mean "no validation errors", therefore I am using
isPresent()
. My only fear about removing the null check could trip people because they might still return null, instead ofOptional.EMPTY
orOptional.ofNullable
.I think the
isEmpty()
is less of an issue than before, given that we have to redo the code here anyway. Its just a matter of accounting for the null check itself. I think its worthwhile to remove the null check once we have moved all the actions over, but to remove theisEmpty()
check now to reduce a potential bug (any non null validation exception will be assumed to be a valid exception). Thoughts?(edit: remove bullet point at the begin of first sentence)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both used to mean "no validation errors", but looking through the code it looks like returning a request where
isEmpty()
is true is pretty rare. So I'm okay with removing that check now and removing the null check later.