-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…r OptionalArg (#458) * Support literal arguments. * Add support for OptionalArg with a collection.
- Loading branch information
Showing
17 changed files
with
421 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
litecommands-annotations/src/dev/rollczi/litecommands/annotations/literal/Literal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.rollczi.litecommands.annotations.literal; | ||
|
||
import dev.rollczi.litecommands.annotations.requirement.RequirementDefinition; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@Target({ ElementType.PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@RequirementDefinition(type = RequirementDefinition.Type.ARGUMENT, nameProviders = { "value" }) | ||
@ApiStatus.Experimental | ||
public @interface Literal { | ||
|
||
String[] value(); | ||
|
||
boolean ignoreCase() default false; | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...nnotations/src/dev/rollczi/litecommands/annotations/literal/LiteralArgumentProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dev.rollczi.litecommands.annotations.literal; | ||
|
||
import dev.rollczi.litecommands.annotations.argument.profile.ProfileAnnotationProcessor; | ||
import dev.rollczi.litecommands.argument.Argument; | ||
import dev.rollczi.litecommands.literal.LiteralProfile; | ||
import java.lang.reflect.Parameter; | ||
|
||
public class LiteralArgumentProcessor<SENDER> extends ProfileAnnotationProcessor<SENDER, Literal, LiteralProfile> { | ||
|
||
public LiteralArgumentProcessor() { | ||
super(Literal.class); | ||
} | ||
|
||
@Override | ||
protected LiteralProfile createProfile(Parameter parameter, Literal annotation, Argument<?> argument) { | ||
return new LiteralProfile(annotation.value(), annotation.ignoreCase()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
...ction/ArgCollectionArgumentProcessor.java → .../varargs/VarargsAnyArgumentProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../rollczi/litecommands/annotations/argument/resolver/collector/OptionalCollectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.rollczi.litecommands.annotations.argument.resolver.collector;import dev.rollczi.litecommands.annotations.argument.Arg; | ||
import dev.rollczi.litecommands.annotations.command.Command; | ||
import dev.rollczi.litecommands.annotations.execute.Execute; | ||
import dev.rollczi.litecommands.annotations.optional.OptionalArg; | ||
import dev.rollczi.litecommands.unit.annotations.LiteTestSpec; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class OptionalCollectionTest extends LiteTestSpec { | ||
|
||
@Command(name = "test") | ||
static class OptionalCollectionTestCommand { | ||
@Execute | ||
String test(@OptionalArg List<String> names) { | ||
if (names == null) { | ||
return "null"; | ||
} | ||
|
||
return String.join(", ", names); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("Should always return not null collection") | ||
void testOptionalCollectionExecute() { | ||
platform.execute("test") | ||
.assertSuccess(""); | ||
|
||
platform.execute("test name1 name2 name3") | ||
.assertSuccess("name1, name2, name3"); | ||
} | ||
|
||
@Test | ||
void testOptionalCollectionSuggest() { | ||
platform.suggest("test ") | ||
.assertSuggest("<names>"); | ||
|
||
platform.suggest("test name1 name2 ") | ||
.assertSuggest("<names>"); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
litecommands-annotations/test/dev/rollczi/litecommands/annotations/literal/LiteralTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.rollczi.litecommands.annotations.literal;import dev.rollczi.litecommands.annotations.argument.Arg; | ||
import dev.rollczi.litecommands.annotations.command.Command; | ||
import dev.rollczi.litecommands.annotations.execute.Execute; | ||
import dev.rollczi.litecommands.annotations.join.Join; | ||
import dev.rollczi.litecommands.unit.annotations.LiteTestSpec; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class LiteralTest extends LiteTestSpec { | ||
|
||
@Command(name = "user") | ||
static class LiteralTestCommand { | ||
@Execute | ||
String setGroup(@Arg String name, @Literal("group set") String literal, @Arg String group) { | ||
return name + " " + literal + " " + group; | ||
} | ||
|
||
@Execute | ||
String ban(@Arg String name, @Literal("ban") String ban, @Join String reason) { | ||
return name + " " + ban + " " + reason; | ||
} | ||
@Execute | ||
String getName(@Arg String name, @Literal("get name") String literal) { | ||
return name + " " + literal; | ||
} | ||
|
||
@Execute | ||
String getUuid(@Arg String name, @Literal("get uuid") String literal) { | ||
return name + " " + literal; | ||
} | ||
|
||
} | ||
|
||
@Test | ||
void testLiteralExecute() { | ||
platform.execute("user Rollczi group set Admin") | ||
.assertSuccess("Rollczi group set Admin"); | ||
|
||
platform.execute("user Rollczi get name") | ||
.assertSuccess("Rollczi get name"); | ||
|
||
platform.execute("user Rollczi get uuid") | ||
.assertSuccess("Rollczi get uuid"); | ||
|
||
platform.execute("user Rollczi ban test reason") | ||
.assertSuccess("Rollczi ban test reason"); | ||
} | ||
|
||
@Test | ||
void testLiteralSuggest() { | ||
platform.suggest("user Rollczi ") | ||
.assertSuggest("group set", "get name", "get uuid", "ban"); | ||
|
||
platform.suggest("user Rollczi group set ") | ||
.assertSuggest("<group>"); | ||
|
||
platform.suggest("user Rollczi ban ") | ||
.assertSuggest("<reason>"); | ||
|
||
platform.suggest("user Rollczi get name ") | ||
.assertSuggest(); | ||
|
||
platform.suggest("user Rollczi get uuid ") | ||
.assertSuggest(); | ||
|
||
platform.suggest("user Rollczi get ") | ||
.assertSuggest("name", "uuid"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
litecommands-core/src/dev/rollczi/litecommands/literal/Literal.java
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
litecommands-core/src/dev/rollczi/litecommands/literal/LiteralArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package dev.rollczi.litecommands.literal; | ||
|
||
import dev.rollczi.litecommands.argument.Argument; | ||
import dev.rollczi.litecommands.argument.parser.ParseResult; | ||
import dev.rollczi.litecommands.argument.profile.ProfileNamespaces; | ||
import dev.rollczi.litecommands.argument.profile.ProfiledMultipleArgumentResolver; | ||
import dev.rollczi.litecommands.input.raw.RawInput; | ||
import dev.rollczi.litecommands.invalidusage.InvalidUsage; | ||
import dev.rollczi.litecommands.invocation.Invocation; | ||
import dev.rollczi.litecommands.range.Range; | ||
import dev.rollczi.litecommands.shared.FailedReason; | ||
import dev.rollczi.litecommands.suggestion.SuggestionContext; | ||
import dev.rollczi.litecommands.suggestion.SuggestionResult; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LiteralArgumentResolver<SENDER> extends ProfiledMultipleArgumentResolver<SENDER, String, LiteralProfile> { | ||
|
||
public LiteralArgumentResolver() { | ||
super(ProfileNamespaces.LITERAL); | ||
} | ||
|
||
@Override | ||
public ParseResult<String> parse(Invocation<SENDER> invocation, Argument<String> argument, RawInput rawInput, LiteralProfile literalProfile) { | ||
return parse(new ArrayList<>(), rawInput, literalProfile); | ||
} | ||
|
||
private ParseResult<String> parse(List<String> before, RawInput rawInput, LiteralProfile literalProfile) { | ||
int max = literalProfile.getExpectedRange().getMax(); | ||
|
||
if (before.size() >= max) { | ||
return ParseResult.failure(FailedReason.of(InvalidUsage.Cause.INVALID_ARGUMENT)); | ||
} | ||
|
||
if (!rawInput.hasNext()) { | ||
return ParseResult.failure(FailedReason.of(InvalidUsage.Cause.MISSING_ARGUMENT)); | ||
} | ||
|
||
String partOfLiteral = rawInput.next(); | ||
String fullLiteral = before.isEmpty() ? partOfLiteral : String.join(" ", before) + " " + partOfLiteral; | ||
|
||
if (literalProfile.getLiterals().contains(fullLiteral)) { | ||
return ParseResult.success(fullLiteral); | ||
} | ||
|
||
before.add(partOfLiteral); | ||
return parse(before, rawInput, literalProfile); | ||
} | ||
|
||
@Override | ||
public Range getRange(Argument<String> argument, LiteralProfile literalProfile) { | ||
return literalProfile.getExpectedRange(); | ||
} | ||
|
||
@Override | ||
protected SuggestionResult suggest(Invocation<SENDER> invocation, Argument<String> argument, SuggestionContext context, LiteralProfile literalProfile) { | ||
return SuggestionResult.of(literalProfile.getLiterals()); | ||
} | ||
|
||
} |
Oops, something went wrong.