All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jbock.validate.VarargsParameterValidator Maven / Gradle / Ivy

There is a newer version: 5.18
Show newest version
package net.jbock.validate;

import io.jbock.simple.Inject;
import io.jbock.util.Either;
import net.jbock.annotated.Items;
import net.jbock.annotated.VarargsParameter;
import net.jbock.common.TypeTool;
import net.jbock.common.ValidationFailure;
import net.jbock.convert.Mapping;
import net.jbock.convert.MappingFinder;
import net.jbock.processor.SourceElement;

import java.util.List;
import java.util.Optional;

import static io.jbock.util.Either.right;
import static io.jbock.util.Eithers.allFailures;
import static io.jbock.util.Eithers.toOptionalList;

final class VarargsParameterValidator {

    private final MappingFinder mappingFinder;
    private final SourceElement sourceElement;
    private final TypeTool tool;

    @Inject
    VarargsParameterValidator(
            MappingFinder mappingFinder,
            SourceElement sourceElement,
            TypeTool tool) {
        this.mappingFinder = mappingFinder;
        this.sourceElement = sourceElement;
        this.tool = tool;
    }

    Either, Optional>> wrapVarargsParameters(
            Items items) {
        return validateDuplicateParametersAnnotation(items.varargsParameters())
                .filter(this::validateVarargsParameterInSuperCommand)
                .flatMap(parameters -> parameters.stream()
                        .map(mappingFinder::findMapping)
                        .collect(allFailures()))
                .map(mappings -> mappings.stream().findAny());
    }

    private Either, List> validateDuplicateParametersAnnotation(
            List parameters) {
        return parameters.stream()
                .skip(1)
                .map(param -> param.fail("duplicate @" + net.jbock.VarargsParameter.class.getSimpleName() + " annotation"))
                .collect(toOptionalList())
                ., List>>map(Either::left)
                .orElseGet(() -> right(parameters));
    }

    /* Left-Optional
     */
    private Optional> validateVarargsParameterInSuperCommand(
            List parameters) {
        if (!sourceElement.isSuperCommand()) {
            return Optional.empty();
        }
        if (parameters.isEmpty()) {
            return Optional.of(List.of(sourceElement.fail("At least one @VarargsParameter must be defined" +
                    " in a @SuperCommand")));
        }
        if (!tool.isListOfString(parameters.get(0).returnType())) {
            return Optional.of(List.of(sourceElement.fail("The @VarargsParameter" +
                    " in a @SuperCommand must return List")));
        }
        return Optional.empty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy