Skip to content

Commit

Permalink
Merge pull request #78 from SentryMan/ctx
Browse files Browse the repository at this point in the history
Fix context Typo
  • Loading branch information
rob-bygrave authored Jul 17, 2023
2 parents 37835ad + 8e1172b commit f3a24bd
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Reflection-free pojo validation via apt source code generation. A light (~85kb +
- Composable Contraint Annotations
- loading and interpolating error messages (with multiple Locales) through ResourceBundles
- Getter Validation
- Method parameter validation (via Avaje Inject AOP only at the moment)
- Method parameter validation (requires a DI container to retrieve the generated MethodAdapter classes)

# Quick Start

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MethodTest {
public MethodTest(Validator apiValidator) {}

@NotNull
@ValidMethod
@ValidMethod(throwOnParamFailure = false)
String test(@NotEmpty List<@NotNull String> str, @Positive int inty, String regular) {
return regular;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

public final class AOPMethodValidator implements AspectProvider<ValidMethod> {

private List<BiConsumer<ValidationContext, Map<Method, MethodAdapterProvider>>> consumers =
private final List<BiConsumer<ValidationContext, Map<Method, MethodAdapterProvider>>> consumers =
new ArrayList<>();

@PostConstruct
public void post(ValidationContext ctx, Map<Method, MethodAdapterProvider> map) {

consumers.forEach(c -> c.accept(ctx, map));
consumers = null;
consumers.clear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.avaje.inject.spi.GenericType;
import io.avaje.validation.ValidMethod;
import io.avaje.validation.Validator;
import io.avaje.validation.adapter.ValidationContext;
import io.avaje.validation.inject.aspect.AOPMethodValidator;
import io.avaje.validation.inject.aspect.MethodAdapterProvider;

Expand Down Expand Up @@ -85,7 +86,7 @@ private void paramAspect(BeanScopeBuilder builder) {

builder.addPostConstructConsumerHook(
b -> {
final var ctx = b.get(Validator.class).getContext();
final var ctx = (ValidationContext) b.get(Validator.class);
final var map =
b.list(MethodAdapterProvider.class).stream()
.collect(toMap(MethodAdapterProvider::provide, p -> p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import io.avaje.validation.constraints.Positive;

@Component
@Component.Import(AOPMethodValidator.class)
public final class TestParamProvider implements MethodAdapterProvider {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/
public final class ConstraintViolationException extends RuntimeException {

private final Set<ConstraintViolation> violations;
private static final long serialVersionUID = 1L;
private final transient Set<ConstraintViolation> violations;

/** Create with the given constraint violations */
public ConstraintViolationException(Set<ConstraintViolation> violations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
public @interface ValidMethod {
String locale() default "";

boolean throwOnParamFailure() default false;
boolean throwOnParamFailure() default true;
}
2 changes: 1 addition & 1 deletion validator/src/main/java/io/avaje/validation/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void validate(Object any, @Nullable Locale locale, @Nullable Class<?>... groups)
throws ConstraintViolationException;

/** Return the validation context used to create adapters */
ValidationContext content();
ValidationContext context();

/**
* Return the Builder used to build the Validator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected AbstractContainerAdapter(ValidationAdapter<T> initialAdapter) {
* Compose the given adapter with the multiAdapter of this AbstractContainerAdapter for validating
* multiple items.
*/
@SuppressWarnings("unchecked")
public AbstractContainerAdapter<T> andThenMulti(ValidationAdapter<?> adapter) {
this.multiAdapter =
this.multiAdapter != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class OptionalValidationAdapter<T> extends AbstractContainerAdapter<T> {
public boolean validate(T value, ValidationRequest req, String propertyName) {
if (value == null) {
return true;
} else if (value instanceof final Optional o) {
} else if (value instanceof final Optional<?> o) {
o.ifPresent(v -> initalAdapter.validate((T) v, req, propertyName));
} else if (value instanceof final OptionalInt i) {
i.ifPresent(v -> initalAdapter.validate(((T) (Object) v), req, propertyName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public String interpolate(String template, Map<String, Object> attributes) {
for (final Map.Entry<String, Object> entry : attributes.entrySet()) {
result = result.replace('{' + entry.getKey() + '}', String.valueOf(entry.getValue()));
}
// return the message
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

/** Builds and caches the ValidationAdapter adapters for DValidator. */
final class CoreAdapterBuilder {
@SuppressWarnings("rawtypes")
public static final ValidationAdapter NOOP = (type, req, propertyName) -> true;

private final DValidator context;
private final List<ValidationContext.AdapterFactory> factories = new ArrayList<>();
private final List<ValidationContext.AnnotationFactory> annotationFactories = new ArrayList<>();
Expand Down

This file was deleted.

14 changes: 6 additions & 8 deletions validator/src/main/java/io/avaje/validation/core/DValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.time.Clock;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -79,7 +78,7 @@ public void validate(Object any, @Nullable Locale locale, @Nullable Class<?>...
}

@Override
public ValidationContext content() {
public ValidationContext context() {
return this;
}

Expand All @@ -89,7 +88,7 @@ private <T> ValidationType<T> type(Class<T> cls) {

@SuppressWarnings("unchecked")
private <T> ValidationType<T> typeWithCache(Type type) {
return (ValidationType<T>) typeCache.computeIfAbsent(type, _type -> new ValidationType<>(this, adapter(_type)));
return (ValidationType<T>) typeCache.computeIfAbsent(type, k -> new ValidationType<>(this, adapter(k)));
}

@Override
Expand Down Expand Up @@ -273,7 +272,7 @@ private void registerComponents() {
public DValidator build() {
registerComponents();

final LocaleResolver localeResolver = new DLocaleResolver(defaultLocal, otherLocals);
final var localeResolver = new LocaleResolver(defaultLocal, otherLocals);
final var interpolator =
ServiceLoader.load(MessageInterpolator.class)
.findFirst()
Expand Down Expand Up @@ -317,10 +316,9 @@ private static AnnotationFactory newAdapterFactory(
return (targetType, ctx, groups, attributes) ->
simpleMatch(type, targetType) ? builder.build(ctx, groups, attributes) : null;
}
}

private static boolean simpleMatch(Type type, Type targetType) {
return Util.typesMatch(type, targetType);
private static boolean simpleMatch(Type type, Type targetType) {
return Util.typesMatch(type, targetType);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
package io.avaje.validation.core;

import io.avaje.lang.Nullable;

import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

interface LocaleResolver {
Locale resolve(@Nullable Locale requestLocale);
import io.avaje.lang.Nullable;

final class LocaleResolver {

private final Locale defaultLocale;
private final Set<Locale> otherLocales = new HashSet<>();

LocaleResolver(Locale defaultLocale, Collection<Locale> others) {
this.defaultLocale = defaultLocale;
otherLocales.addAll(others);
}

public Locale defaultLocale() {
return defaultLocale;
}

Locale defaultLocale();
public Set<Locale> otherLocales() {
return otherLocales;
}

Set<Locale> otherLocales();
public Locale resolve(@Nullable Locale requestLocale) {
if (requestLocale == null || !otherLocales.contains(requestLocale)) {
return defaultLocale;
} else {
return requestLocale;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TemplateLookupTest {
private final TemplateLookup lookup;

TemplateLookupTest() {
final var localeResolver = new DLocaleResolver(Locale.ENGLISH, List.of(Locale.GERMAN));
final var localeResolver = new LocaleResolver(Locale.ENGLISH, List.of(Locale.GERMAN));
final var defaultResourceBundle =
new ResourceBundleManager(List.of(), List.of(), localeResolver);
this.lookup = new TemplateLookup(defaultResourceBundle);
Expand Down

0 comments on commit f3a24bd

Please sign in to comment.