Skip to content

Commit

Permalink
GH-502 Fix optional resolver (#502)
Browse files Browse the repository at this point in the history
* Fix optional resolver

* Fix unit tests
  • Loading branch information
Rollczi authored Dec 19, 2024
1 parent c74497c commit 7fb5e2b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package dev.rollczi.litecommands.annotations.schematic;

import dev.rollczi.litecommands.unit.Parsers;
import dev.rollczi.litecommands.unit.annotations.LiteTestSpec;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.async.Async;
Expand All @@ -20,6 +21,7 @@
class SchematicConfiguredTest extends LiteTestSpec {

static LiteTestConfig config = builder -> builder
.argumentParser(ServerRank.class, Parsers.of(new ServerRank()))
.schematicGenerator(SchematicFormat.parentheses());

static class ServerRank {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package dev.rollczi.litecommands.annotations.schematic;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.unit.Parsers;
import dev.rollczi.litecommands.unit.annotations.LiteTestSpec;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.flag.Flag;
Expand All @@ -18,6 +23,9 @@

class SchematicTest extends LiteTestSpec {

static LiteTestConfig config = builder -> builder
.argumentParser(ServerRank.class, Parsers.of(new ServerRank()));

static class ServerRank {}

@Command(name = "rank")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.ArgumentKey;
import dev.rollczi.litecommands.argument.parser.Parser;
import dev.rollczi.litecommands.argument.parser.ParserRegistryImpl;
import dev.rollczi.litecommands.argument.resolver.optional.OptionalArgumentResolver;
import dev.rollczi.litecommands.input.raw.RawInput;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.range.Range;
import dev.rollczi.litecommands.reflect.type.TypeRange;
import dev.rollczi.litecommands.reflect.type.TypeToken;
import dev.rollczi.litecommands.requirement.RequirementResult;
Expand All @@ -27,18 +29,29 @@ class LocationArgumentTest extends BukkitTestSpec {
@BeforeEach
void before() {
parsers.registerParser(Location.class, ArgumentKey.DEFAULT, new LocationArgument(new MessageRegistry<>()));
parsers.registerParser(TypeRange.same(Optional.class), ArgumentKey.DEFAULT, new OptionalArgumentResolver<>());
parsers.registerParser(TypeRange.same(Optional.class), ArgumentKey.DEFAULT, new OptionalArgumentResolver<>(parsers));
}

@Test
void test() {
Invocation<CommandSender> invocation = invocation("test", "pos1");
RequirementResult<Optional<Location>> result = parsers.parse(invocation, ARGUMENT, RawInput.of("1", "2", "3"))
Invocation<CommandSender> invocation = invocation("test", "-4", "64", "-4");
RequirementResult<Optional<Location>> result = parsers.parse(invocation, ARGUMENT, RawInput.of("-4", "64", "-4"))
.await();

assertTrue(result.isSuccessful());
assertThat(result.getSuccess())
.hasValue(new Location(null, 1, 2, 3));
.hasValue(new Location(null, -4, 64, -4));
}

@Test
void testRange() {
Parser<CommandSender, Optional<Location>> parser = parsers.getParser(ARGUMENT);
Range range = parser.getRange(ARGUMENT);

assertThat(range.getMin())
.isEqualTo(0);
assertThat(range.getMax())
.isEqualTo(3);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.parser.Parser;
import dev.rollczi.litecommands.argument.parser.ParserChainAccessor;
import dev.rollczi.litecommands.argument.parser.ParserRegistry;
import dev.rollczi.litecommands.argument.resolver.MultipleArgumentResolverChained;
import dev.rollczi.litecommands.argument.suggester.SuggesterChainAccessor;
import dev.rollczi.litecommands.input.raw.RawInput;
Expand All @@ -17,6 +19,12 @@
@SuppressWarnings("rawtypes")
public class OptionalArgumentResolver<SENDER> implements MultipleArgumentResolverChained<SENDER, Optional> {

private final ParserRegistry<SENDER> parserRegistry;

public OptionalArgumentResolver(ParserRegistry<SENDER> parserRegistry) {
this.parserRegistry = parserRegistry;
}

@Override
public ParseResult<Optional> parse(Invocation<SENDER> invocation, Argument<Optional> optionalArgument, RawInput input, ParserChainAccessor<SENDER> chainAccessor) {
TypeToken<Optional> optionalType = optionalArgument.getType();
Expand All @@ -40,7 +48,16 @@ private <E> ParseResult<Optional> parseValue(TypeToken<E> type, Invocation<SENDE

@Override
public Range getRange(Argument<Optional> optionalArgument) {
return Range.range(0, 1);
TypeToken<Optional> optionalType = optionalArgument.getType();
return getRange(optionalType.getParameterized(), optionalArgument);
}

private <T> Range getRange(TypeToken<T> parameterized, Argument<Optional> optionalArgument) {
Argument<T> argument = optionalArgument.child(parameterized);
Parser<SENDER, T> parser = parserRegistry.getParser(argument);
Range range = parser.getRange(argument);

return Range.range(0, range.getMax());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void invoke(Event event) {
throw new LiteCommandsReflectInvocationException(declaredMethod, "Cannot access method", exception);
}
catch (InvocationTargetException exception) {
throw new LiteCommandsReflectInvocationException(declaredMethod, "Cannot invoke method", exception);
throw new LiteCommandsReflectInvocationException(declaredMethod, "Cannot invoke method", exception.getCause());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static <SENDER, C extends PlatformSettings, B extends LiteCommandsBaseBui
.argument(UUID.class, new UUIDArgumentResolver<>(messageRegistry))

.argument(upwards(Enum.class), new EnumArgumentResolver<>())
.argument(Optional.class, new OptionalArgumentResolver<>())
.argument(Optional.class, new OptionalArgumentResolver<>(parser))
.argument(CompletableFuture.class, new CompletableFutureResolver<>(scheduler, parser))
.argument(upwards(Object.class), ProfileNamespaces.NULLABLE, new NullableArgumentResolver<>(parser, suggester))

Expand Down
20 changes: 20 additions & 0 deletions litecommands-unit/src/dev/rollczi/litecommands/unit/Parsers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.rollczi.litecommands.unit;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.parser.Parser;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;

public final class Parsers {

public static <S, T> Parser<S, T> of(T result) {
return new ArgumentResolver<S, T>() {
@Override
protected ParseResult<T> parse(Invocation<S> invocation, Argument<T> context, String argument) {
return ParseResult.success(result);
}
};
}

}

0 comments on commit 7fb5e2b

Please sign in to comment.