Add named parsers
This commit is contained in:
parent
96fcd03a75
commit
c336a2d7e8
4 changed files with 71 additions and 9 deletions
|
|
@ -261,14 +261,28 @@ public final class AnnotationParser<C> {
|
||||||
final TypeToken<?> token = TypeToken.of(parameter.getParameterizedType());
|
final TypeToken<?> token = TypeToken.of(parameter.getParameterizedType());
|
||||||
final ParserParameters parameters = this.manager.getParserRegistry()
|
final ParserParameters parameters = this.manager.getParserRegistry()
|
||||||
.parseAnnotations(token, annotations);
|
.parseAnnotations(token, annotations);
|
||||||
final ArgumentParser<C, ?> parser = this.manager.getParserRegistry()
|
|
||||||
.createParser(token, parameters)
|
final ArgumentParser<C, ?> parser;
|
||||||
.orElseThrow(() -> new IllegalArgumentException(
|
if (argumentPair.getArgument().parserName().isEmpty()) {
|
||||||
String.format("Parameter '%s' in method '%s' "
|
parser = this.manager.getParserRegistry()
|
||||||
+ "has parser '%s' but no parser exists "
|
.createParser(token, parameters)
|
||||||
+ "for that type",
|
.orElseThrow(() -> new IllegalArgumentException(
|
||||||
parameter.getName(), method.getName(),
|
String.format("Parameter '%s' in method '%s' "
|
||||||
token.toString())));
|
+ "has parser '%s' but no parser exists "
|
||||||
|
+ "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) {
|
if (syntaxFragment == null || syntaxFragment.getArgumentMode() == ArgumentMode.LITERAL) {
|
||||||
throw new IllegalArgumentException(String.format(
|
throw new IllegalArgumentException(String.format(
|
||||||
"Invalid command argument '%s' in method '%s': "
|
"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.Command;
|
||||||
import com.intellectualsites.commands.CommandManager;
|
import com.intellectualsites.commands.CommandManager;
|
||||||
import com.intellectualsites.commands.annotations.specifier.Range;
|
import com.intellectualsites.commands.annotations.specifier.Range;
|
||||||
|
import com.intellectualsites.commands.arguments.standard.StringArgument;
|
||||||
import com.intellectualsites.commands.meta.SimpleCommandMeta;
|
import com.intellectualsites.commands.meta.SimpleCommandMeta;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
|
@ -33,6 +34,7 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.concurrent.CompletionException;
|
import java.util.concurrent.CompletionException;
|
||||||
|
|
||||||
class AnnotationParserTest {
|
class AnnotationParserTest {
|
||||||
|
|
@ -44,6 +46,8 @@ class AnnotationParserTest {
|
||||||
static void setup() {
|
static void setup() {
|
||||||
manager = new TestCommandManager();
|
manager = new TestCommandManager();
|
||||||
annotationParser = new AnnotationParser<>(manager, TestCommandSender.class, p -> SimpleCommandMeta.empty());
|
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
|
@Test
|
||||||
|
|
@ -59,7 +63,8 @@ class AnnotationParserTest {
|
||||||
@CommandMethod("test|t <int> [string]")
|
@CommandMethod("test|t <int> [string]")
|
||||||
public void testCommand(@Nonnull final TestCommandSender sender,
|
public void testCommand(@Nonnull final TestCommandSender sender,
|
||||||
@Argument("int") @Range(max = "100") final int argument,
|
@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);
|
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,
|
<T> void registerParserSupplier(@Nonnull TypeToken<T> type,
|
||||||
@Nonnull Function<ParserParameters, ArgumentParser<C, ?>> supplier);
|
@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
|
* 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,
|
<T> Optional<ArgumentParser<C, T>> createParser(@Nonnull TypeToken<T> type,
|
||||||
@Nonnull ParserParameters parserParameters);
|
@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)
|
.put(boolean.class, Boolean.class)
|
||||||
.build();
|
.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<TypeToken<?>, Function<ParserParameters, ArgumentParser<C, ?>>> parserSuppliers = new HashMap<>();
|
||||||
private final Map<Class<? extends Annotation>, BiFunction<? extends Annotation, TypeToken<?>, ParserParameters>>
|
private final Map<Class<? extends Annotation>, BiFunction<? extends Annotation, TypeToken<?>, ParserParameters>>
|
||||||
annotationMappers = new HashMap<>();
|
annotationMappers = new HashMap<>();
|
||||||
|
|
@ -109,6 +110,12 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
||||||
this.parserSuppliers.put(type, supplier);
|
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
|
@Override
|
||||||
public <A extends Annotation, T> void registerAnnotationMapper(@Nonnull final Class<A> annotation,
|
public <A extends Annotation, T> void registerAnnotationMapper(@Nonnull final Class<A> annotation,
|
||||||
@Nonnull final BiFunction<A, TypeToken<?>,
|
@Nonnull final BiFunction<A, TypeToken<?>,
|
||||||
|
|
@ -160,6 +167,19 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
||||||
return Optional.of(parser);
|
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> {
|
private static final class RangeMapper<T> implements BiFunction<Range, TypeToken<?>, ParserParameters> {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue