net.jbock.validate.SourceParametersValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbock-compiler Show documentation
Show all versions of jbock-compiler Show documentation
jbock annotation processor
package net.jbock.validate;
import io.jbock.util.Either;
import net.jbock.Parameters;
import net.jbock.annotated.AnnotatedParameters;
import net.jbock.common.ValidationFailure;
import net.jbock.convert.MappingFinder;
import net.jbock.processor.SourceElement;
import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
import static io.jbock.util.Either.right;
import static io.jbock.util.Eithers.toOptionalList;
import static io.jbock.util.Eithers.toValidListAll;
@ValidateScope
public class SourceParametersValidator {
private final MappingFinder converterFinder;
private final SourceElement sourceElement;
@Inject
SourceParametersValidator(
MappingFinder converterFinder,
SourceElement sourceElement) {
this.converterFinder = converterFinder;
this.sourceElement = sourceElement;
}
Either, ContextBuilder.Step3> wrapRepeatablePositionalParams(
ContextBuilder.Step2 step) {
return validateDuplicateParametersAnnotation(step.repeatablePositionalParameters())
.filter(this::validateNoRepeatableParameterInSuperCommand)
.flatMap(repeatablePositionalParameters -> repeatablePositionalParameters.stream()
.map(converterFinder::findMapping)
.collect(toValidListAll()))
.map(step::accept);
}
private Either, List> validateDuplicateParametersAnnotation(
List repeatablePositionalParameters) {
return repeatablePositionalParameters.stream()
.skip(1)
.map(param -> param.fail("duplicate @" + Parameters.class.getSimpleName() + " annotation"))
.collect(toOptionalList())
., List>>map(Either::left)
.orElseGet(() -> right(repeatablePositionalParameters));
}
/* Left-Optional
*/
private Optional> validateNoRepeatableParameterInSuperCommand(
List repeatablePositionalParameters) {
if (!sourceElement.isSuperCommand()) {
return Optional.empty();
}
return repeatablePositionalParameters.stream()
.map(param -> param.fail("@" + Parameters.class.getSimpleName() +
" cannot be used when superCommand=true"))
.collect(toOptionalList());
}
}