From f2afe58f23908c780ba3f3a8fc9465fbed1439f8 Mon Sep 17 00:00:00 2001 From: Norbert Dejlich Date: Wed, 28 Aug 2024 00:42:55 +0200 Subject: [PATCH] GH-421 Add missing api. Remove useless method. (#421) --- .../jakarta/JakartaRawResultHandler.java | 17 ++++--- .../litecommands/jakarta/JakartaSettings.java | 44 ++++++++++++------- .../jakarta/JakartaViolation.java | 10 ++++- .../jakarta/LiteJakartaMessagesTest.java | 17 ++++--- 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaRawResultHandler.java b/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaRawResultHandler.java index adf5bec43..35d42be5f 100644 --- a/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaRawResultHandler.java +++ b/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaRawResultHandler.java @@ -26,24 +26,23 @@ class JakartaRawResultHandler implements ResultHandler invocation, JakartaRawResult result, ResultHandlerChain chain) { - List list = result.getViolations().stream() + List list = result.getViolations().stream() .map(entry -> getViolation(entry, settings.localeProvider.apply(invocation))) .map(violation -> settings.toMessage(invocation, violation)) .collect(Collectors.toList()); - Object message = settings.constraintViolationsMessage.get(invocation, new JakartaParsedResult(list)); - - chain.resolve(invocation, message); + chain.resolve(invocation, list); } - private JakartaViolation getViolation(JakartaRawResult.Entry entry, Locale locale) { - ConstraintViolation constraintViolation = entry.getViolation(); + private JakartaViolation getViolation(JakartaRawResult.Entry entry, Locale locale) { + ConstraintViolation violation = entry.getViolation(); return new JakartaViolation<>( - constraintViolation.getConstraintDescriptor().getAnnotation().annotationType(), - constraintViolation, + violation.getConstraintDescriptor().getAnnotation().annotationType(), + violation.getInvalidValue(), + violation, entry.getRequirement(), - getInterpolatedMessage(constraintViolation, locale), + getInterpolatedMessage(violation, locale), entry.getRequirement().getName(), schematicFormat, locale ); diff --git a/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaSettings.java b/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaSettings.java index 22a5efe77..7e1d2ce94 100644 --- a/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaSettings.java +++ b/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaSettings.java @@ -2,6 +2,8 @@ import dev.rollczi.litecommands.invocation.Invocation; import dev.rollczi.litecommands.message.InvokedMessage; +import dev.rollczi.litecommands.shared.BiHashMap; +import dev.rollczi.litecommands.shared.BiMap; import jakarta.validation.Validation; import jakarta.validation.ValidatorFactory; @@ -16,13 +18,10 @@ public class JakartaSettings { Function, Locale> localeProvider = ignored -> Locale.getDefault(); ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); - InvokedMessage constraintViolationsMessage = - (invocation, parsedResult) -> String.format("Constraint violations: %n%s", parsedResult.asJoinedString()); - - private InvokedMessage> defaultViolationMessage = + private InvokedMessage> defaultViolationMessage = (invocation, violation) -> String.format("%s - %s", violation.getFormattedParameterName(), violation.getMessage()); - private Map, InvokedMessage>> violationMessages = new HashMap<>(); + private BiMap, Class, InvokedMessage>> violationMessages = new BiHashMap<>(); public JakartaSettings locale(Function, Locale> localeProvider) { this.localeProvider = localeProvider; @@ -34,27 +33,42 @@ public JakartaSettings validatorFactory(ValidatorFactory validatorFactor return this; } + /** + * It is not recommended to use this method. Use {@link #violationMessage(Class, InvokedMessage)} instead. + * If you want to join all violations into one message, then implement your ResultHandler for JakartaRawResult type. + */ + @Deprecated public JakartaSettings constraintViolationsMessage(InvokedMessage message) { - this.constraintViolationsMessage = message; - return this; + throw new UnsupportedOperationException("Use violationMessage(Class, InvokedMessage) instead. If you want to join all violations into one message, then implement your handler: ResultHandler."); + } + + public JakartaSettings violationMessage(Class annotationType, InvokedMessage> message) { + return violationMessage(annotationType, Object.class, message); } - public JakartaSettings violationMessage(Class annotationType, InvokedMessage> message) { - this.violationMessages.put(annotationType, message); + public JakartaSettings violationMessage(Class annotationType, Class type, InvokedMessage> message) { + this.violationMessages.put(annotationType, type, message); return this; } - public JakartaSettings violationMessage(InvokedMessage> message) { + public JakartaSettings violationMessage(InvokedMessage> message) { this.defaultViolationMessage = message; return this; } - String toMessage(Invocation invocation, JakartaViolation violation) { - InvokedMessage> message = (InvokedMessage>) violationMessages.get(violation.getAnnotationType()); + @SuppressWarnings("unchecked") + Object toMessage(Invocation invocation, JakartaViolation violation) { + InvokedMessage> message = (InvokedMessage>) violationMessages.get(violation.getAnnotationType(), violation.getInvalidValue().getClass()); + + if (message == null) { + message = (InvokedMessage>) violationMessages.get(violation.getAnnotationType(), Object.class); + } + + if (message == null) { + return defaultViolationMessage.get(invocation, violation); + } - return message != null - ? message.get(invocation, violation) - : defaultViolationMessage.get(invocation, violation); + return message.get(invocation, violation); } } diff --git a/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaViolation.java b/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaViolation.java index e400682ca..2068950ba 100644 --- a/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaViolation.java +++ b/litecommands-jakarta/src/dev/rollczi/litecommands/jakarta/JakartaViolation.java @@ -7,9 +7,10 @@ import java.lang.annotation.Annotation; import java.util.Locale; -public class JakartaViolation { +public class JakartaViolation { private final Class annotationType; + private final T value; private final ConstraintViolation violation; private final Requirement requirement; private final String message; @@ -17,8 +18,9 @@ public class JakartaViolation { private final SchematicFormat schematicFormat; private final Locale locale; - public JakartaViolation(Class annotationType, ConstraintViolation violation, Requirement requirement, String message, String parameterName, SchematicFormat schematicFormat, Locale locale) { + public JakartaViolation(Class annotationType, T value, ConstraintViolation violation, Requirement requirement, String message, String parameterName, SchematicFormat schematicFormat, Locale locale) { this.annotationType = annotationType; + this.value = value; this.violation = violation; this.requirement = requirement; this.message = message; @@ -27,6 +29,10 @@ public JakartaViolation(Class annotationType, ConstraintViolation vio this.locale = locale; } + public T getInvalidValue() { + return value; + } + public Class getAnnotationType() { return annotationType; } diff --git a/litecommands-jakarta/test/dev/rollczi/litecommands/jakarta/LiteJakartaMessagesTest.java b/litecommands-jakarta/test/dev/rollczi/litecommands/jakarta/LiteJakartaMessagesTest.java index d7861be2d..3e51cd9fd 100644 --- a/litecommands-jakarta/test/dev/rollczi/litecommands/jakarta/LiteJakartaMessagesTest.java +++ b/litecommands-jakarta/test/dev/rollczi/litecommands/jakarta/LiteJakartaMessagesTest.java @@ -54,9 +54,6 @@ public void negative(@Negative @Arg("number") int number) {} : Locale.ENGLISH; }) - // constraint violations message - .constraintViolationsMessage((invocation, parsedResult) -> "Constraint violations: " + parsedResult.asJoinedString()) - // global violation message .violationMessage((invocation, violation) -> "Invalid value for " + violation.getFormattedParameterName()) @@ -76,6 +73,8 @@ public void negative(@Negative @Arg("number") int number) {} : format("[Other] Range must be between %d and %d", range.min(), range.max()); }) + // violation message for @Max annotation + .violationMessage(Max.class, Integer.class, (invocation, violation) -> "Invalid value: " + violation.getInvalidValue() + " for " + violation.getParameterName()) ) ); @@ -83,31 +82,31 @@ public void negative(@Negative @Arg("number") int number) {} @DisplayName("should return configured message specific for @Size annotation") void size() { platform.execute("test size 1234") - .assertMessage("Constraint violations: Size must be between 5 and 10 for text"); + .assertMessage("Size must be between 5 and 10 for text"); } @Test @DisplayName("should return configured message specific for @Range annotation and locale") void range() { platform.execute(TestPlatformSender.locale(Locale.ENGLISH), "test range 4") - .assertMessage("Constraint violations: [English] Range must be between 5 and 10"); + .assertMessage("[English] Range must be between 5 and 10"); platform.execute(TestPlatformSender.locale(Locale.GERMAN), "test range 4") - .assertMessage("Constraint violations: [Other] Range must be between 5 and 10"); + .assertMessage("[Other] Range must be between 5 and 10"); } @Test @DisplayName("should return configured message specific for @Max annotation") void max() { platform.execute("test max 2") - .assertMessage("Constraint violations: Invalid value for "); + .assertMessage("Invalid value: 2 for number"); } @Test - @DisplayName("should return configured message specific for @Min annotation") + @DisplayName("should return configured message") void min() { platform.execute("test min 0") - .assertMessage("Constraint violations: Invalid value for "); + .assertMessage("Invalid value for "); } }