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

Introduce Applicative Validation #121

Merged
merged 23 commits into from
May 14, 2021
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
176 changes: 176 additions & 0 deletions scripts/generate-applicative.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#!/bin/bash
set -e
n=10

for i in `seq 1 ${n}`;do
class="Function${i}"
file="$(dirname $0)/../src/main/java/am/ik/yavi/fn/${class}.java"
echo $file
cat <<EOF > ${file}
/*
* Copyright (C) 2018-2021 Toshiaki Maki <makingx@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package am.ik.yavi.fn;

/**
* Generated by
* https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh
*
* @since 0.6.0
*/
public interface ${class}<$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//'), R> {

R apply($(echo $(for j in `seq 1 ${i}`;do echo -n "T${j} t${j}, ";done) | sed 's/,$//'));
}
EOF
done

for i in `seq 1 ${n}`;do
class="Composing${i}"
file="$(dirname $0)/../src/main/java/am/ik/yavi/fn/${class}.java"
echo $file
cat <<EOF > ${file}
/*
* Copyright (C) 2018-2021 Toshiaki Maki <makingx@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package am.ik.yavi.fn;

import java.util.List;

/**
* Generated by
* https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh
*
* @since 0.6.0
*/
public class ${class}<E, $(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//')> {
$(for j in `seq 1 ${i}`;do echo " protected final Validation<E, T${j}> v${j};";echo;done)

public ${class}($(echo $(for j in `seq 1 ${i}`;do echo -n "Validation<E, T${j}> v${j}, ";done) | sed 's/,$//')) {
$(for j in `seq 1 ${i}`;do echo " this.v${j} = v${j};";done)
}

public <R> Validation<E, R> apply(Function${i}<$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//'), R> f) {
return $(echo $(for j in `seq ${i} 1`;do echo -n "v${j}.apply(";done) | sed 's/,$//')Validation.success(Functions.curry(f))$(echo $(for j in `seq 1 ${i}`;do echo -n ")";done));
}
$(if [ ${i} -lt ${n} ];then echo;echo " public <T$((${i} + 1))> Composing$((${i} + 1))<E, $(echo $(for j in `seq 1 $((${i} + 1))`;do echo -n "T${j}, ";done) | sed 's/,$//')> compose(Validation<E, T$((${i} + 1))> v$((${i} + 1))) {"; echo " return new Composing$((${i} + 1))<>($(echo $(for j in `seq 1 $((${i} + 1))`;do echo -n "v${j}, ";done) | sed 's/,$//'));"; echo " }"; else echo -n "";fi)
}
EOF
done

class="Validations"
file="$(dirname $0)/../src/main/java/am/ik/yavi/fn/${class}.java"
echo $file
cat <<EOF > ${file}
/*
* Copyright (C) 2018-2021 Toshiaki Maki <makingx@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package am.ik.yavi.fn;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Generated by
* https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh
*
* @since 0.6.0
*/
public class ${class} {
$(for i in `seq 1 ${n}`;do echo " public static <E, $(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//')> Composing${i}<E, $(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//')> compose($(echo $(for j in `seq 1 ${i}`;do echo -n "Validation<E, T${j}> v${j}, ";done) | sed 's/,$//')) {"; echo " return new Composing${i}<>($(echo $(for j in `seq 1 ${i}`;do echo -n "v${j}, ";done) | sed 's/,$//'));"; echo " }";echo;done)

$(for i in `seq 1 ${n}`;do echo " public static <R, E, $(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//')> Validation<E, R> apply(Function${i}<$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done) | sed 's/,$//'), R> f, $(echo $(for j in `seq 1 ${i}`;do echo -n "Validation<E, T${j}> v${j}, ";done) | sed 's/,$//')) {"; echo " return compose($(echo $(for j in `seq 1 ${i}`;do echo -n "v${j}, ";done) | sed 's/,$//')).apply(f);"; echo " }";echo;done)

public static <E, T> Validation<E, List<T>> sequence(
Iterable<Validation<E, T>> validations) {
final List<E> errors = new ArrayList<>();
final List<T> values = new ArrayList<>();
for (Validation<E, T> validation : validations) {
if (!validation.isValid()) {
errors.addAll(validation.errors());
}
else if (errors.isEmpty()) {
values.add(validation.value());
}
}
return errors.isEmpty() ? Validation.success(values) : Validation.failure(errors);
}

public static <E, T, U> Validation<E, List<U>> traverse(Iterable<T> values,
Function<T, Validation<E, U>> mapper) {
return sequence(StreamSupport.stream(values.spliterator(), false).map(mapper)
.collect(Collectors.toList()));
}
}
EOF

class="Functions"
file="$(dirname $0)/../src/main/java/am/ik/yavi/fn/${class}.java"
echo $file
cat <<EOF > ${file}
/*
* Copyright (C) 2018-2021 Toshiaki Maki <makingx@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package am.ik.yavi.fn;

/**
* Generated by
* https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh
*
* @since 0.6.0
*/
public class ${class} {
$(for i in `seq 1 ${n}`;do echo " public static <$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done)) R> $(echo $(for j in `seq 1 ${i}`;do echo -n "Function1<T${j}, ";done)) R$(echo $(for j in `seq 1 ${i}`;do echo -n ">";done)) curry(Function${i}<$(echo $(for j in `seq 1 ${i}`;do echo -n "T${j}, ";done)) R> f) {"; echo " return $(echo $(for j in `seq 1 ${i}`;do echo -n "t${j} -> ";done)) f.apply($(echo $(for j in `seq 1 ${i}`;do echo -n "t${j}, ";done) | sed 's/,$//'));"; echo " }";echo;done)
}
EOF
2 changes: 1 addition & 1 deletion src/main/java/am/ik/yavi/builder/ValidatorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ private <N> Consumer<Pair<ConstraintCondition<N>, ValidatorSubset<N>>> appendNes
final ConstraintCondition<T> condition = new NestedConstraintCondition<>(
nested, conditionalValidator.first());
final ValidatorSubset<T> v = new NestedValidatorSubset<>(nested,
conditionalValidator.second(), name + this.messageKeySeparator);
conditionalValidator.second(), name);
this.conditionalValidators.add(new Pair<>(condition, v));
};
}
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/am/ik/yavi/core/ApplicativeValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2018-2021 Toshiaki Maki <makingx@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package am.ik.yavi.core;

import java.util.Locale;

import am.ik.yavi.fn.Validation;

/**
* Applicative validator class
* @param <T> Target class
* @since 0.6.0
*/
public final class ApplicativeValidator<T> {
private final Validator<T> validator;

ApplicativeValidator(Validator<T> validator) {
this.validator = validator;
}

public Validation<ConstraintViolation, T> validate(T target) {
return this.validate(target, Locale.getDefault(), ConstraintGroup.DEFAULT);
}

public Validation<ConstraintViolation, T> validate(T target, Locale locale,
ConstraintGroup constraintGroup) {
final ConstraintViolations violations = this.validator.validate(target, locale,
constraintGroup);
if (violations.isValid()) {
return Validation.success(target);
}
else {
return Validation.failure(violations);
}
}
}
32 changes: 31 additions & 1 deletion src/main/java/am/ik/yavi/core/ConstraintViolations.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,37 @@
import java.util.stream.Collectors;

public class ConstraintViolations implements List<ConstraintViolation> {
private final List<ConstraintViolation> delegate = new ArrayList<>();
private final List<ConstraintViolation> delegate;

/**
* Constructs with the constraintViolations to delegate. If the given value is
* <code>ConstraintViolations</code> itself, cast and return it.
*
* @param delegate constraintViolations to delegate
* @since 0.6.0
*/
public static ConstraintViolations of(List<ConstraintViolation> delegate) {
if (delegate instanceof ConstraintViolations) {
return (ConstraintViolations) delegate;
}
return new ConstraintViolations(delegate);
}

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ConstraintViolations() {
this.delegate = new ArrayList<>();
}

/**
* Constructs with the constraintViolations to delegate
* @param delegate constraintViolations to delegate
* @since 0.6.0
*/
public ConstraintViolations(List<ConstraintViolation> delegate) {
this.delegate = delegate;
}

/**
* Appends the specified element to the end of this list (optional operation).
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/am/ik/yavi/core/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

/**
* Validates the target instances.
*
*
* A <code>Validator</code> instance is immutable and can be used as a singleton.
*
*
* @param <T> the type of the instance to validate
* @author Toshiaki Maki
*/
Expand Down Expand Up @@ -67,18 +67,20 @@ private Validator(String messageKeySeparator,
this.collectionValidators = collectionValidators;
this.conditionalValidators = conditionalValidators;
this.messageFormatter = messageFormatter;
this.prefix = prefix;
this.prefix = (prefix == null || prefix.isEmpty()
|| prefix.endsWith(this.messageKeySeparator)) ? prefix
: prefix + this.messageKeySeparator;
}

Validator<T> prefixed(String prefix) {
public Validator<T> prefixed(String prefix) {
return new Validator<>(this.messageKeySeparator, this.predicatesList,
this.collectionValidators, this.conditionalValidators,
this.messageFormatter, prefix);
}

/**
* This method is supposed to be used only internally.
*
*
* @param action callback per <code>ConstraintPredicates</code>.
*/
public void forEachPredicates(Consumer<ConstraintPredicates<T, ?>> action) {
Expand Down Expand Up @@ -121,7 +123,7 @@ public ConstraintViolations validate(T target, ConstraintGroup constraintGroup)
/**
* Validates all constraints on {@code target}.<br>
* {@code ConstraintGroup.DEFAULT} is used as a constraint group.
*
*
* @param target target to validate
* @param locale the locale targeted for the violation messages.
* @return constraint violations
Expand Down Expand Up @@ -158,6 +160,15 @@ public ConstraintViolations validate(T target) {
return this.validate(target, Locale.getDefault(), ConstraintGroup.DEFAULT);
}

/**
* Convert to the applicative validator
* @return applicative validator
* @since 0.6.0
*/
public ApplicativeValidator<T> applicative() {
return new ApplicativeValidator<>(this);
}

/**
* Validates all constraints on {@code target} and returns {@code Either} object that
* has constraint violations on the left or validated object on the right. <br>
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/am/ik/yavi/fn/Composing1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018-2021 Toshiaki Maki <makingx@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package am.ik.yavi.fn;

import java.util.List;

/**
* Generated by
* https://github.com/making/yavi/blob/develop/scripts/generate-applicative.sh
*
* @since 0.6.0
*/
public class Composing1<E, T1> {
protected final Validation<E, T1> v1;

public Composing1(Validation<E, T1> v1) {
this.v1 = v1;
}

public <R> Validation<E, R> apply(Function1<T1, R> f) {
return v1.apply(Validation.success(Functions.curry(f)));
}

public <T2> Composing2<E, T1, T2> compose(Validation<E, T2> v2) {
return new Composing2<>(v1, v2);
}
}
Loading