Add named parsers
This commit is contained in:
parent
96fcd03a75
commit
c336a2d7e8
4 changed files with 71 additions and 9 deletions
|
|
@ -261,7 +261,10 @@ public final class AnnotationParser<C> {
|
|||
final TypeToken<?> token = TypeToken.of(parameter.getParameterizedType());
|
||||
final ParserParameters parameters = this.manager.getParserRegistry()
|
||||
.parseAnnotations(token, annotations);
|
||||
final ArgumentParser<C, ?> parser = this.manager.getParserRegistry()
|
||||
|
||||
final ArgumentParser<C, ?> parser;
|
||||
if (argumentPair.getArgument().parserName().isEmpty()) {
|
||||
parser = this.manager.getParserRegistry()
|
||||
.createParser(token, parameters)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
String.format("Parameter '%s' in method '%s' "
|
||||
|
|
@ -269,6 +272,17 @@ public final class AnnotationParser<C> {
|
|||
+ "for that type",
|
||||
parameter.getName(), method.getName(),
|
||||
token.toString())));
|
||||
} else {
|
||||
parser = this.manager.getParserRegistry()
|
||||
.createParser(argumentPair.getArgument().parserName(), parameters)
|
||||
.orElseThrow(() -> new IllegalArgumentException(
|
||||
String.format("Parameter '%s' in method '%s' "
|
||||
+ "has parser '%s' but no parser exists "
|
||||
+ "for that type",
|
||||
parameter.getName(), method.getName(),
|
||||
token.toString())));
|
||||
}
|
||||
|
||||
if (syntaxFragment == null || syntaxFragment.getArgumentMode() == ArgumentMode.LITERAL) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Invalid command argument '%s' in method '%s': "
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ package com.intellectualsites.commands.annotations;
|
|||
import com.intellectualsites.commands.Command;
|
||||
import com.intellectualsites.commands.CommandManager;
|
||||
import com.intellectualsites.commands.annotations.specifier.Range;
|
||||
import com.intellectualsites.commands.arguments.standard.StringArgument;
|
||||
import com.intellectualsites.commands.meta.SimpleCommandMeta;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
|
@ -33,6 +34,7 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.CompletionException;
|
||||
|
||||
class AnnotationParserTest {
|
||||
|
|
@ -44,6 +46,8 @@ class AnnotationParserTest {
|
|||
static void setup() {
|
||||
manager = new TestCommandManager();
|
||||
annotationParser = new AnnotationParser<>(manager, TestCommandSender.class, p -> SimpleCommandMeta.empty());
|
||||
manager.getParserRegistry().registerNamedParserSupplier("potato", p -> new StringArgument.StringParser<>(
|
||||
StringArgument.StringMode.SINGLE, (c, s) -> Collections.singletonList("potato")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -59,7 +63,8 @@ class AnnotationParserTest {
|
|||
@CommandMethod("test|t <int> [string]")
|
||||
public void testCommand(@Nonnull final TestCommandSender sender,
|
||||
@Argument("int") @Range(max = "100") final int argument,
|
||||
@Nonnull @Argument(value = "string", defaultValue = "potato") final String string) {
|
||||
@Nonnull @Argument(value = "string", defaultValue = "potato", parserName = "potato")
|
||||
final String string) {
|
||||
System.out.printf("Received int: %d and string '%s'\n", argument, string);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -52,6 +52,16 @@ public interface ParserRegistry<C> {
|
|||
<T> void registerParserSupplier(@Nonnull TypeToken<T> type,
|
||||
@Nonnull Function<ParserParameters, ArgumentParser<C, ?>> supplier);
|
||||
|
||||
/**
|
||||
* Register a named parser supplier
|
||||
*
|
||||
* @param name Parser name
|
||||
* @param supplier The function that generates the parser. The map supplied my contain parameters used
|
||||
* to configure the parser, many of which are documented in {@link StandardParameters}
|
||||
*/
|
||||
void registerNamedParserSupplier(@Nonnull String name,
|
||||
@Nonnull Function<ParserParameters, ArgumentParser<C, ?>> supplier);
|
||||
|
||||
/**
|
||||
* Register a mapper that maps annotation instances to a map of parameter-object pairs
|
||||
*
|
||||
|
|
@ -88,4 +98,17 @@ public interface ParserRegistry<C> {
|
|||
<T> Optional<ArgumentParser<C, T>> createParser(@Nonnull TypeToken<T> type,
|
||||
@Nonnull ParserParameters parserParameters);
|
||||
|
||||
/**
|
||||
* Attempt to create a {@link ArgumentParser} for a specified type, using
|
||||
* an instance of {@link ParserParameter} to configure the parser settings
|
||||
*
|
||||
* @param name Parser
|
||||
* @param parserParameters Parser parameters
|
||||
* @param <T> Generic type
|
||||
* @return Parser, if one can be created
|
||||
*/
|
||||
@Nonnull
|
||||
<T> Optional<ArgumentParser<C, T>> createParser(@Nonnull String name,
|
||||
@Nonnull ParserParameters parserParameters);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
.put(boolean.class, Boolean.class)
|
||||
.build();
|
||||
|
||||
private final Map<String, Function<ParserParameters, ArgumentParser<C, ?>>> namedParsers = new HashMap<>();
|
||||
private final Map<TypeToken<?>, Function<ParserParameters, ArgumentParser<C, ?>>> parserSuppliers = new HashMap<>();
|
||||
private final Map<Class<? extends Annotation>, BiFunction<? extends Annotation, TypeToken<?>, ParserParameters>>
|
||||
annotationMappers = new HashMap<>();
|
||||
|
|
@ -109,6 +110,12 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
this.parserSuppliers.put(type, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerNamedParserSupplier(@Nonnull final String name,
|
||||
@Nonnull final Function<ParserParameters, ArgumentParser<C, ?>> supplier) {
|
||||
this.namedParsers.put(name, supplier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A extends Annotation, T> void registerAnnotationMapper(@Nonnull final Class<A> annotation,
|
||||
@Nonnull final BiFunction<A, TypeToken<?>,
|
||||
|
|
@ -160,6 +167,19 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
return Optional.of(parser);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public <T> Optional<ArgumentParser<C, T>> createParser(@Nonnull final String name,
|
||||
@Nonnull final ParserParameters parserParameters) {
|
||||
final Function<ParserParameters, ArgumentParser<C, ?>> producer = this.namedParsers.get(name);
|
||||
if (producer == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@SuppressWarnings("unchecked") final ArgumentParser<C, T> parser = (ArgumentParser<C, T>) producer.apply(
|
||||
parserParameters);
|
||||
return Optional.of(parser);
|
||||
}
|
||||
|
||||
|
||||
private static final class RangeMapper<T> implements BiFunction<Range, TypeToken<?>, ParserParameters> {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue