✨ Add command argument preprocessors
This commit is contained in:
parent
fcd269b6e7
commit
1f3c3f2bd9
9 changed files with 405 additions and 17 deletions
|
|
@ -357,9 +357,20 @@ public final class CommandTree<C> {
|
|||
final CommandArgument<C, ?> argument = child.getValue();
|
||||
final CommandContext.ArgumentTiming argumentTiming = commandContext.createTiming(argument);
|
||||
|
||||
// START: Parsing
|
||||
argumentTiming.setStart(System.nanoTime());
|
||||
final ArgumentParseResult<?> result = argument.getParser().parse(commandContext, commandQueue);
|
||||
final ArgumentParseResult<?> result;
|
||||
final ArgumentParseResult<Boolean> preParseResult = child.getValue().preprocess(
|
||||
commandContext,
|
||||
commandQueue
|
||||
);
|
||||
if (!preParseResult.getFailure().isPresent() && preParseResult.getParsedValue().orElse(false)) {
|
||||
result = argument.getParser().parse(commandContext, commandQueue);
|
||||
} else {
|
||||
result = preParseResult;
|
||||
}
|
||||
argumentTiming.setEnd(System.nanoTime(), result.getFailure().isPresent());
|
||||
// END: Parsing
|
||||
|
||||
if (result.getParsedValue().isPresent()) {
|
||||
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
|
||||
|
|
@ -476,6 +487,19 @@ public final class CommandTree<C> {
|
|||
} else if (commandQueue.peek().isEmpty()) {
|
||||
return child.getValue().getSuggestionsProvider().apply(commandContext, commandQueue.remove());
|
||||
}
|
||||
|
||||
// START: Preprocessing
|
||||
final ArgumentParseResult<Boolean> preParseResult = child.getValue().preprocess(
|
||||
commandContext,
|
||||
commandQueue
|
||||
);
|
||||
if (preParseResult.getFailure().isPresent() || !preParseResult.getParsedValue().orElse(false)) {
|
||||
final String value = commandQueue.peek() == null ? "" : commandQueue.peek();
|
||||
return child.getValue().getSuggestionsProvider().apply(commandContext, value);
|
||||
}
|
||||
// END: Preprocessing
|
||||
|
||||
// START: Parsing
|
||||
final ArgumentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
|
||||
if (result.getParsedValue().isPresent()) {
|
||||
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
|
||||
|
|
@ -484,6 +508,7 @@ public final class CommandTree<C> {
|
|||
final String value = commandQueue.peek() == null ? "" : commandQueue.peek();
|
||||
return child.getValue().getSuggestionsProvider().apply(commandContext, value);
|
||||
}
|
||||
// END: Parsing
|
||||
}
|
||||
}
|
||||
/* There are 0 or more static arguments as children. No variable child arguments are present */
|
||||
|
|
|
|||
|
|
@ -33,8 +33,12 @@ import io.leangen.geantyref.TypeToken;
|
|||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Queue;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
|
@ -82,6 +86,12 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
* Suggestion provider
|
||||
*/
|
||||
private final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider;
|
||||
/**
|
||||
* Argument preprocessors that allows for extensions to existing argument types
|
||||
* without having to update all parsers
|
||||
*/
|
||||
private final Collection<BiFunction<@NonNull CommandContext<C>,
|
||||
@NonNull Queue<@NonNull String>, @NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors;
|
||||
/**
|
||||
* Whether or not the argument has been used before
|
||||
*/
|
||||
|
|
@ -89,6 +99,41 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
|
||||
private Command<C> owningCommand;
|
||||
|
||||
/**
|
||||
* Construct a new command argument
|
||||
*
|
||||
* @param required Whether or not the argument is required
|
||||
* @param name The argument name
|
||||
* @param parser The argument parser
|
||||
* @param defaultValue Default value used when no value is provided by the command sender
|
||||
* @param valueType Type produced by the parser
|
||||
* @param suggestionsProvider Suggestions provider
|
||||
* @param argumentPreprocessors Argument preprocessors
|
||||
*/
|
||||
public CommandArgument(
|
||||
final boolean required,
|
||||
final @NonNull String name,
|
||||
final @NonNull ArgumentParser<C, T> parser,
|
||||
final @NonNull String defaultValue,
|
||||
final @NonNull TypeToken<T> valueType,
|
||||
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
|
||||
final @NonNull Collection<@NonNull BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
|
||||
@NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors
|
||||
) {
|
||||
this.required = required;
|
||||
this.name = Objects.requireNonNull(name, "Name may not be null");
|
||||
if (!NAME_PATTERN.asPredicate().test(name)) {
|
||||
throw new IllegalArgumentException("Name must be alphanumeric");
|
||||
}
|
||||
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
||||
this.defaultValue = defaultValue;
|
||||
this.valueType = valueType;
|
||||
this.suggestionsProvider = suggestionsProvider == null
|
||||
? buildDefaultSuggestionsProvider(this)
|
||||
: suggestionsProvider;
|
||||
this.argumentPreprocessors = new LinkedList<>(argumentPreprocessors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new command argument
|
||||
*
|
||||
|
|
@ -107,17 +152,7 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
final @NonNull TypeToken<T> valueType,
|
||||
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
|
||||
) {
|
||||
this.required = required;
|
||||
this.name = Objects.requireNonNull(name, "Name may not be null");
|
||||
if (!NAME_PATTERN.asPredicate().test(name)) {
|
||||
throw new IllegalArgumentException("Name must be alphanumeric");
|
||||
}
|
||||
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
||||
this.defaultValue = defaultValue;
|
||||
this.valueType = valueType;
|
||||
this.suggestionsProvider = suggestionsProvider == null
|
||||
? buildDefaultSuggestionsProvider(this)
|
||||
: suggestionsProvider;
|
||||
this(required, name, parser, defaultValue, valueType, suggestionsProvider, Collections.emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -229,6 +264,48 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
return String.format("%s{name=%s}", this.getClass().getSimpleName(), this.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new preprocessor. If all preprocessor has succeeding {@link ArgumentParseResult results}
|
||||
* that all return {@code true}, the argument will be passed onto the parser.
|
||||
* <p>
|
||||
* It is important that the preprocessor doesn't pop any input. Instead, it should only peek.
|
||||
*
|
||||
* @param preprocessor Preprocessor
|
||||
* @return {@code this}
|
||||
*/
|
||||
public @NonNull CommandArgument<C, T> addPreprocessor(
|
||||
final @NonNull BiFunction<@NonNull CommandContext<C>, @NonNull Queue<String>,
|
||||
@NonNull ArgumentParseResult<Boolean>> preprocessor
|
||||
) {
|
||||
this.argumentPreprocessors.add(preprocessor);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preprocess command input. This will immediately forward any failed argument parse results.
|
||||
* If none fails, a {@code true} result will be returned
|
||||
*
|
||||
* @param context Command context
|
||||
* @param input Remaining command input. None will be popped
|
||||
* @return Parsing error, or argument containing {@code true}
|
||||
*/
|
||||
public @NonNull ArgumentParseResult<Boolean> preprocess(
|
||||
final @NonNull CommandContext<C> context,
|
||||
final @NonNull Queue<String> input
|
||||
) {
|
||||
for (final BiFunction<@NonNull CommandContext<C>, @NonNull Queue<String>,
|
||||
@NonNull ArgumentParseResult<Boolean>> preprocessor : this.argumentPreprocessors) {
|
||||
final ArgumentParseResult<Boolean> result = preprocessor.apply(
|
||||
context,
|
||||
input
|
||||
);
|
||||
if (result.getFailure().isPresent()) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return ArgumentParseResult.success(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the owning command
|
||||
*
|
||||
|
|
@ -376,6 +453,9 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
private String defaultValue = "";
|
||||
private BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>> suggestionsProvider;
|
||||
|
||||
private final Collection<BiFunction<@NonNull CommandContext<C>,
|
||||
@NonNull String, @NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors = new LinkedList<>();
|
||||
|
||||
protected Builder(
|
||||
final @NonNull TypeToken<T> valueType,
|
||||
final @NonNull String name
|
||||
|
|
@ -489,8 +569,13 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
if (this.suggestionsProvider == null) {
|
||||
this.suggestionsProvider = new DelegatingSuggestionsProvider<>(this.name, this.parser);
|
||||
}
|
||||
return new CommandArgument<>(this.required, this.name, this.parser,
|
||||
this.defaultValue, this.valueType, this.suggestionsProvider
|
||||
return new CommandArgument<>(
|
||||
this.required,
|
||||
this.name,
|
||||
this.parser,
|
||||
this.defaultValue,
|
||||
this.valueType,
|
||||
this.suggestionsProvider
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,127 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.arguments.preprocessor;
|
||||
|
||||
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Command preprocessor that filters based on regular expressions
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
public final class RegexPreprocessor<C> implements BiFunction<@NonNull CommandContext<C>, @NonNull Queue<@NonNull String>,
|
||||
@NonNull ArgumentParseResult<Boolean>> {
|
||||
|
||||
private final String rawPattern;
|
||||
private final Predicate<@NonNull String> predicate;
|
||||
|
||||
private RegexPreprocessor(final @NonNull String pattern) {
|
||||
this.rawPattern = pattern;
|
||||
this.predicate = Pattern.compile(pattern).asPredicate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new preprocessor
|
||||
*
|
||||
* @param pattern Regular expression
|
||||
* @param <C> Command sender type
|
||||
* @return Preprocessor instance
|
||||
*/
|
||||
public static <C> @NonNull RegexPreprocessor<C> of(final @NonNull String pattern) {
|
||||
return new RegexPreprocessor<>(pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ArgumentParseResult<Boolean> apply(
|
||||
@NonNull final CommandContext<C> context, @NonNull final Queue<@NonNull String> strings
|
||||
) {
|
||||
final String head = strings.peek();
|
||||
if (head == null) {
|
||||
throw new NullPointerException("No input");
|
||||
}
|
||||
if (predicate.test(head)) {
|
||||
return ArgumentParseResult.success(true);
|
||||
}
|
||||
return ArgumentParseResult.failure(
|
||||
new RegexValidationException(
|
||||
this.rawPattern,
|
||||
head
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Exception thrown when input fails regex matching in {@link RegexPreprocessor}
|
||||
*/
|
||||
public static final class RegexValidationException extends IllegalArgumentException {
|
||||
|
||||
private final String pattern;
|
||||
private final String failedString;
|
||||
|
||||
private RegexValidationException(
|
||||
@NonNull final String pattern,
|
||||
@NonNull final String failedString
|
||||
) {
|
||||
this.pattern = pattern;
|
||||
this.failedString = failedString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return String.format(
|
||||
"Input '%s' does not match the required pattern '%s'",
|
||||
failedString,
|
||||
pattern
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the string that failed the verification
|
||||
*
|
||||
* @return Failed string
|
||||
*/
|
||||
public @NonNull String getFailedString() {
|
||||
return this.failedString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pattern that caused the string to fail
|
||||
*
|
||||
* @return Pattern
|
||||
*/
|
||||
public @NonNull String getPattern() {
|
||||
return this.pattern;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
/**
|
||||
* Pre-made argument preprocessors
|
||||
*/
|
||||
package cloud.commandframework.arguments.preprocessor;
|
||||
|
|
@ -135,8 +135,12 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
|
|||
*/
|
||||
@Override
|
||||
public @NonNull BooleanArgument<C> build() {
|
||||
return new BooleanArgument<>(this.isRequired(), this.getName(), this.liberal,
|
||||
this.getDefaultValue(), this.getSuggestionsProvider()
|
||||
return new BooleanArgument<>(
|
||||
this.isRequired(),
|
||||
this.getName(),
|
||||
this.liberal,
|
||||
this.getDefaultValue(),
|
||||
this.getSuggestionsProvider()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ package cloud.commandframework;
|
|||
|
||||
import cloud.commandframework.arguments.CommandArgument;
|
||||
import cloud.commandframework.arguments.compound.ArgumentPair;
|
||||
import cloud.commandframework.arguments.preprocessor.RegexPreprocessor;
|
||||
import cloud.commandframework.arguments.standard.EnumArgument;
|
||||
import cloud.commandframework.arguments.standard.FloatArgument;
|
||||
import cloud.commandframework.arguments.standard.IntegerArgument;
|
||||
|
|
@ -134,6 +135,14 @@ class CommandTreeTest {
|
|||
.handler(c -> {
|
||||
System.out.printf("%f\n", c.<Float>get("num"));
|
||||
}));
|
||||
|
||||
/* Build command for testing preprocessing */
|
||||
manager.command(manager.commandBuilder("preprocess")
|
||||
.argument(
|
||||
StringArgument.<TestCommandSender>of("argument")
|
||||
.addPreprocessor(RegexPreprocessor.of("[A-Za-z]{3,5}"))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -252,6 +261,15 @@ class CommandTreeTest {
|
|||
manager.executeCommand(new TestCommandSender(), "float 100").join();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPreprocessors() {
|
||||
manager.executeCommand(new TestCommandSender(), "preprocess abc").join();
|
||||
Assertions.assertThrows(
|
||||
CompletionException.class,
|
||||
() -> manager.executeCommand(new TestCommandSender(), "preprocess ab").join()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static final class SpecificCommandSender extends TestCommandSender {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue