Skip to content
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

Use Interpolation Interface #49

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ <T> ValidationAdapter<T> buildAnnotation(Class<? extends Annotation> cls, Map<St
return result;
}
}
// unknown annotation have noop
// unknown annotations have noop
return NoopAnnotationValidator.INSTANCE;
}
}
17 changes: 4 additions & 13 deletions validator/src/main/java/io/avaje/validation/core/DValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import io.avaje.lang.Nullable;
Expand Down Expand Up @@ -108,22 +107,14 @@ ValidationRequest request(@Nullable Locale locale) {
return new DRequest(this, locale);
}


String interpolate(Message msg, Locale requestLocale) {

// resolve the locale to use to produce the message
final Locale locale = localeResolver.resolve(requestLocale);
// lookup in resource bundles using resolved locale and template
final String template = templateLookup.lookup(msg.template(), locale);
// translate the template using msg attributes
final Map<String, Object> attributes = msg.attributes();
final Set<Map.Entry<String, Object>> entries = attributes.entrySet();
String result = template;
for (final Map.Entry<String, Object> entry : entries) {
// needs work here to improve functionality, support local specific value formatting eg Duration Max
result = result.replace('{' + entry.getKey() + '}', String.valueOf(entry.getValue()));
}
// return the message
return result;

return interpolator.interpolate(template, msg.attributes());
}

/** Implementation of Validator.Builder. */
Expand Down Expand Up @@ -184,7 +175,7 @@ public DValidator build() {
final var interpolator =
ServiceLoader.load(MessageInterpolator.class)
.findFirst()
.orElseGet(NoopMessageInterpolator::new);
.orElseGet(BasicMessageInterpolator::new);
return new DValidator(factories, afactories, interpolator, localeResolver);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.avaje.validation.core;

import java.util.Map;

public interface MessageInterpolator {

String interpolate(String string);
String interpolate(String template, Map<String, Object> attributes);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package io.avaje.validation.core;

final class NoopMessageInterpolator implements MessageInterpolator {
import java.util.Map;

final class BasicMessageInterpolator implements MessageInterpolator {

@Override
public String interpolate(String string) {
return string;
public String interpolate(String template, Map<String, Object> attributes) {

String result = template;

for (final Map.Entry<String, Object> entry : attributes.entrySet()) {
// needs work here to improve functionality, support local specific value formatting eg
// Duration Max
result = result.replace('{' + entry.getKey() + '}', String.valueOf(entry.getValue()));
}
// return the message
return result;
}
}