HLRC: Use Optional in validation logic (#33104)

The Validatable class comes from an old class in server code, that
assumed null was returned in the event of validation having no
errors. This commit changes that to use Optional, which is cleaner than
passing around null objects.
This commit is contained in:
Michael Basnight 2018-08-28 10:33:18 -05:00 committed by GitHub
parent 1e11b05b58
commit 5f0f990afd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 18 deletions

View File

@ -176,6 +176,7 @@ import java.util.HashMap;
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;
@ -1010,9 +1011,9 @@ public class RestHighLevelClient implements Closeable {
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);
}
@ -1105,9 +1106,9 @@ public class RestHighLevelClient implements Closeable {
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()) {
listener.onFailure(validationException.get());
return;
}
internalPerformRequestAsync(request, requestConverter, options, responseConverter, listener, ignores);

View File

@ -18,24 +18,20 @@
*/
package org.elasticsearch.client;
import java.util.Optional;
/**
* Defines a validation layer for Requests.
*/
public interface Validatable {
ValidationException EMPTY_VALIDATION = new ValidationException() {
@Override
public void addValidationError(String error) {
throw new UnsupportedOperationException("Validation messages should not be added to the empty validation");
}
};
/**
* Perform validation. This method does not have to be overridden in the event that no validation needs to be done.
* Perform validation. This method does not have to be overridden in the event that no validation needs to be done,
* or the validation was done during object construction time. A {@link ValidationException} that is not null is
* assumed to contain validation errors and will be thrown.
*
* @return potentially null, in the event of older actions, an empty {@link ValidationException} in newer actions, or finally a
* {@link ValidationException} that contains a list of all failed validation.
* @return An {@link Optional} {@link ValidationException} that contains a list of validation errors.
*/
default ValidationException validate() {
return EMPTY_VALIDATION;
default Optional<ValidationException> validate() {
return Optional.empty();
}
}