Skip to content

Validation factory parameter order #208

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
ValidUtils correct parameter order
Tests reinforced
  • Loading branch information
GoodforGod committed Jul 20, 2023
commit 0e9c84a1c0c25bd0de7a70de5d3a9f6936c0d9dd
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ public static List<ValidMeta.Constraint> getValidatedByConstraints(ProcessingEnv
.filter(ft -> ft instanceof DeclaredType)
.map(factoryType -> {
final DeclaredType factoryRawType = (DeclaredType) factoryType;
final Map<String, Object> parameters = env.getElementUtils().getElementValuesWithDefaults(annotation).entrySet().stream()
final Map<String, Object> parametersWithDefaults = env.getElementUtils().getElementValuesWithDefaults(annotation).entrySet().stream()
.collect(Collectors.toMap(
ae -> ae.getKey().getSimpleName().toString(),
ae -> castParameterValue(ae.getValue()),
(v1, v2) -> v2,
LinkedHashMap::new
));

var annotationParameters = annotation.getAnnotationType().asElement().getEnclosedElements().stream()
.filter(e -> e instanceof ExecutableElement)
.toList();

final Map<String, Object> parameters = new LinkedHashMap<>();
for (var parameter : annotationParameters) {
final String parameterName = parameter.getSimpleName().toString();
final Object parameterValue = parametersWithDefaults.get(parameterName);
parameters.put(parameterName, parameterValue);
}

if (parameters.size() > 0) {
factoryRawType.asElement().getEnclosedElements()
.stream()
Expand All @@ -39,7 +50,7 @@ public static List<ValidMeta.Constraint> getValidatedByConstraints(ProcessingEnv
.filter(e -> e.getParameters().size() == parameters.size())
.findFirst()
.orElseThrow(() -> new ProcessingErrorException("Expected " + factoryRawType.asElement().getSimpleName()
+ "#create() method with " + parameters.size() + " parameters, but was didn't find such", factoryRawType.asElement(), annotation));
+ "#create() method with " + parameters.size() + " parametersWithDefaults, but was didn't find such", factoryRawType.asElement(), annotation));
}

final TypeMirror fieldType = getBoxType(parameterType, env);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected Validator<ValidBar> getBarValidator() {
return (Validator<ValidBar>) clazz.getConstructors()[0].newInstance(
sizeListConstraintFactory(TypeRef.of(Integer.class)),
notBlankStringConstraintFactory(),
sizeStringConstraintFactory(),
listValidator(getTazValidator(), TypeRef.of(ValidTaz.class)));
} catch (RuntimeException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public class ValidBar {

@Nullable
@NotBlank
@Size(max = 50)
private String id;
@Size(min = 1, max = 5)
@Size(max = 5, min = 1)
private List<Integer> codes;
@Valid
private List<ValidTaz> tazs;
Expand Down