Skip to content

Commit 7fb5e2b

Browse files
authored
GH-502 Fix optional resolver (#502)
* Fix optional resolver * Fix unit tests
1 parent c74497c commit 7fb5e2b

File tree

7 files changed

+67
-7
lines changed

7 files changed

+67
-7
lines changed

litecommands-annotations/test/dev/rollczi/litecommands/annotations/schematic/SchematicConfiguredTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package dev.rollczi.litecommands.annotations.schematic;
33

4+
import dev.rollczi.litecommands.unit.Parsers;
45
import dev.rollczi.litecommands.unit.annotations.LiteTestSpec;
56
import dev.rollczi.litecommands.annotations.argument.Arg;
67
import dev.rollczi.litecommands.annotations.async.Async;
@@ -20,6 +21,7 @@
2021
class SchematicConfiguredTest extends LiteTestSpec {
2122

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

2527
static class ServerRank {}

litecommands-annotations/test/dev/rollczi/litecommands/annotations/schematic/SchematicTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package dev.rollczi.litecommands.annotations.schematic;
22

3+
import dev.rollczi.litecommands.argument.Argument;
4+
import dev.rollczi.litecommands.argument.parser.ParseResult;
5+
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
6+
import dev.rollczi.litecommands.invocation.Invocation;
7+
import dev.rollczi.litecommands.unit.Parsers;
38
import dev.rollczi.litecommands.unit.annotations.LiteTestSpec;
49
import dev.rollczi.litecommands.annotations.argument.Arg;
510
import dev.rollczi.litecommands.annotations.flag.Flag;
@@ -18,6 +23,9 @@
1823

1924
class SchematicTest extends LiteTestSpec {
2025

26+
static LiteTestConfig config = builder -> builder
27+
.argumentParser(ServerRank.class, Parsers.of(new ServerRank()));
28+
2129
static class ServerRank {}
2230

2331
@Command(name = "rank")

litecommands-bukkit/test/dev/rollczi/litecommands/bukkit/argument/LocationArgumentTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import dev.rollczi.litecommands.argument.Argument;
44
import dev.rollczi.litecommands.argument.ArgumentKey;
5+
import dev.rollczi.litecommands.argument.parser.Parser;
56
import dev.rollczi.litecommands.argument.parser.ParserRegistryImpl;
67
import dev.rollczi.litecommands.argument.resolver.optional.OptionalArgumentResolver;
78
import dev.rollczi.litecommands.input.raw.RawInput;
89
import dev.rollczi.litecommands.invocation.Invocation;
910
import dev.rollczi.litecommands.message.MessageRegistry;
11+
import dev.rollczi.litecommands.range.Range;
1012
import dev.rollczi.litecommands.reflect.type.TypeRange;
1113
import dev.rollczi.litecommands.reflect.type.TypeToken;
1214
import dev.rollczi.litecommands.requirement.RequirementResult;
@@ -27,18 +29,29 @@ class LocationArgumentTest extends BukkitTestSpec {
2729
@BeforeEach
2830
void before() {
2931
parsers.registerParser(Location.class, ArgumentKey.DEFAULT, new LocationArgument(new MessageRegistry<>()));
30-
parsers.registerParser(TypeRange.same(Optional.class), ArgumentKey.DEFAULT, new OptionalArgumentResolver<>());
32+
parsers.registerParser(TypeRange.same(Optional.class), ArgumentKey.DEFAULT, new OptionalArgumentResolver<>(parsers));
3133
}
3234

3335
@Test
3436
void test() {
35-
Invocation<CommandSender> invocation = invocation("test", "pos1");
36-
RequirementResult<Optional<Location>> result = parsers.parse(invocation, ARGUMENT, RawInput.of("1", "2", "3"))
37+
Invocation<CommandSender> invocation = invocation("test", "-4", "64", "-4");
38+
RequirementResult<Optional<Location>> result = parsers.parse(invocation, ARGUMENT, RawInput.of("-4", "64", "-4"))
3739
.await();
3840

3941
assertTrue(result.isSuccessful());
4042
assertThat(result.getSuccess())
41-
.hasValue(new Location(null, 1, 2, 3));
43+
.hasValue(new Location(null, -4, 64, -4));
44+
}
45+
46+
@Test
47+
void testRange() {
48+
Parser<CommandSender, Optional<Location>> parser = parsers.getParser(ARGUMENT);
49+
Range range = parser.getRange(ARGUMENT);
50+
51+
assertThat(range.getMin())
52+
.isEqualTo(0);
53+
assertThat(range.getMax())
54+
.isEqualTo(3);
4255
}
4356

4457
@Test

litecommands-core/src/dev/rollczi/litecommands/argument/resolver/optional/OptionalArgumentResolver.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import dev.rollczi.litecommands.argument.Argument;
44
import dev.rollczi.litecommands.argument.parser.ParseResult;
5+
import dev.rollczi.litecommands.argument.parser.Parser;
56
import dev.rollczi.litecommands.argument.parser.ParserChainAccessor;
7+
import dev.rollczi.litecommands.argument.parser.ParserRegistry;
68
import dev.rollczi.litecommands.argument.resolver.MultipleArgumentResolverChained;
79
import dev.rollczi.litecommands.argument.suggester.SuggesterChainAccessor;
810
import dev.rollczi.litecommands.input.raw.RawInput;
@@ -17,6 +19,12 @@
1719
@SuppressWarnings("rawtypes")
1820
public class OptionalArgumentResolver<SENDER> implements MultipleArgumentResolverChained<SENDER, Optional> {
1921

22+
private final ParserRegistry<SENDER> parserRegistry;
23+
24+
public OptionalArgumentResolver(ParserRegistry<SENDER> parserRegistry) {
25+
this.parserRegistry = parserRegistry;
26+
}
27+
2028
@Override
2129
public ParseResult<Optional> parse(Invocation<SENDER> invocation, Argument<Optional> optionalArgument, RawInput input, ParserChainAccessor<SENDER> chainAccessor) {
2230
TypeToken<Optional> optionalType = optionalArgument.getType();
@@ -40,7 +48,16 @@ private <E> ParseResult<Optional> parseValue(TypeToken<E> type, Invocation<SENDE
4048

4149
@Override
4250
public Range getRange(Argument<Optional> optionalArgument) {
43-
return Range.range(0, 1);
51+
TypeToken<Optional> optionalType = optionalArgument.getType();
52+
return getRange(optionalType.getParameterized(), optionalArgument);
53+
}
54+
55+
private <T> Range getRange(TypeToken<T> parameterized, Argument<Optional> optionalArgument) {
56+
Argument<T> argument = optionalArgument.child(parameterized);
57+
Parser<SENDER, T> parser = parserRegistry.getParser(argument);
58+
Range range = parser.getRange(argument);
59+
60+
return Range.range(0, range.getMax());
4461
}
4562

4663
@Override

litecommands-core/src/dev/rollczi/litecommands/event/SimpleEventPublisher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void invoke(Event event) {
106106
throw new LiteCommandsReflectInvocationException(declaredMethod, "Cannot access method", exception);
107107
}
108108
catch (InvocationTargetException exception) {
109-
throw new LiteCommandsReflectInvocationException(declaredMethod, "Cannot invoke method", exception);
109+
throw new LiteCommandsReflectInvocationException(declaredMethod, "Cannot invoke method", exception.getCause());
110110
}
111111
}
112112

litecommands-framework/src/dev/rollczi/litecommands/LiteCommandsFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static <SENDER, C extends PlatformSettings, B extends LiteCommandsBaseBui
135135
.argument(UUID.class, new UUIDArgumentResolver<>(messageRegistry))
136136

137137
.argument(upwards(Enum.class), new EnumArgumentResolver<>())
138-
.argument(Optional.class, new OptionalArgumentResolver<>())
138+
.argument(Optional.class, new OptionalArgumentResolver<>(parser))
139139
.argument(CompletableFuture.class, new CompletableFutureResolver<>(scheduler, parser))
140140
.argument(upwards(Object.class), ProfileNamespaces.NULLABLE, new NullableArgumentResolver<>(parser, suggester))
141141

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.rollczi.litecommands.unit;
2+
3+
import dev.rollczi.litecommands.argument.Argument;
4+
import dev.rollczi.litecommands.argument.parser.ParseResult;
5+
import dev.rollczi.litecommands.argument.parser.Parser;
6+
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
7+
import dev.rollczi.litecommands.invocation.Invocation;
8+
9+
public final class Parsers {
10+
11+
public static <S, T> Parser<S, T> of(T result) {
12+
return new ArgumentResolver<S, T>() {
13+
@Override
14+
protected ParseResult<T> parse(Invocation<S> invocation, Argument<T> context, String argument) {
15+
return ParseResult.success(result);
16+
}
17+
};
18+
}
19+
20+
}

0 commit comments

Comments
 (0)