net.jbock.validate.CommandProcessor 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.simple.Inject;
import io.jbock.util.Either;
import net.jbock.annotated.Items;
import net.jbock.annotated.ItemsFactory;
import net.jbock.annotated.Option;
import net.jbock.annotated.Parameter;
import net.jbock.annotated.VarargsParameter;
import net.jbock.common.ValidationFailure;
import net.jbock.convert.Mapping;
import net.jbock.processor.SourceElement;
import net.jbock.writing.CommandRepresentation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import static io.jbock.util.Eithers.optionalList;
/**
* This class is responsible for item validation.
* If validation succeeds, a {@link CommandRepresentation} is created.
*/
public final class CommandProcessor {
private final ItemsFactory itemsFactory;
private final SourceElement sourceElement;
private final OptionValidator optionValidator;
private final ParameterValidator parameterValidator;
private final VarargsParameterValidator varargsParameterValidator;
@Inject
CommandProcessor(
ItemsFactory itemsFactory,
SourceElement sourceElement,
OptionValidator optionValidator,
ParameterValidator parameterValidator,
VarargsParameterValidator varargsParameterValidator) {
this.itemsFactory = itemsFactory;
this.sourceElement = sourceElement;
this.optionValidator = optionValidator;
this.parameterValidator = parameterValidator;
this.varargsParameterValidator = varargsParameterValidator;
}
public Either, CommandRepresentation> generate() {
return itemsFactory.createItems()
.filter(this::checkDuplicateDescriptionKeys)
.flatMap(items -> {
Either, List>> a = parameterValidator.wrapPositionalParams(items);
Either, Optional>> b = varargsParameterValidator.wrapVarargsParameters(items);
Either, List>> c = optionValidator.wrapOptions(items);
return a.flatMap(parameters ->
b.flatMap(varargsParameter ->
c.map(options -> new CommandRepresentation(sourceElement, options, parameters, varargsParameter))));
});
}
/* Left-Optional
*/
private Optional> checkDuplicateDescriptionKeys(
Items items) {
List failures = new ArrayList<>();
Set keys = new HashSet<>();
sourceElement.descriptionKey().ifPresent(keys::add);
Stream.of(items.namedOptions(),
items.positionalParameters(),
items.varargsParameters())
.flatMap(List::stream)
.forEach(m -> m.descriptionKey().ifPresent(key -> {
if (!keys.add(key)) {
String message = "duplicate description key: " + key;
failures.add(m.fail(message));
}
}));
return optionalList(failures);
}
}