Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-427 Support async parse result. #435

Merged
merged 18 commits into from
Oct 5, 2024
Merged

Conversation

Rollczi
Copy link
Owner

@Rollczi Rollczi commented Sep 26, 2024

Resolve: #427
🟢 Add alias for @Context annotation

@Command(name = "hello")
public class HelloCommand {
    @Execute
    void command(@Sender CommandSender sender, @Arg String name) {
        // ...
    }
}

🟢 Add an option to create Annotation aliases like as @Sender
You can implement your annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@RequirementDefinition(type = RequirementDefinition.Type.CONTEXT)
public @interface MySender {
}

And use it in your commands:

    @Execute
    void command(@MySender CommandSender sender, @Arg String name) {
        // ...
    }

🟢 Support completableFuture/async parse/context result:

public class UserArgumentResolver extends ArgumentResolver<CommandSender, User> {

    private final Pattern VALID_USER_PATTERN = Pattern.compile("^[a-zA-Z0-9_]{3,16}$");
    private final UserService userService;

    public UserArgumentResolver(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        CompletableFuture<ParseResult<User>> userNotFound = userService.getUser(argument)
            .thenApply(user -> user != null ? ParseResult.success(user) : ParseResult.failure("User not found"));

        return ParseResult.completableFuture(userNotFound);
    }

    @Override
    public boolean match(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        return VALID_USER_PATTERN.matcher(argument).matches();
    }
}

🟢 when you call blocking methods you can use ParseResult.async()

    @Override
    protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        return ParseResult.async(() -> userDatabase.getUser(argument));
    }

⚠️ When you return async/completableFuture result then you must also implement match method for correct suggestion validation

🟢 See also same API for ContextResult

ContextResult.completableFuture()
ContextResult.async()

🔴 Rmoved wrapper API

🔴 Breaking changes (Internal API)

incompatible:

AbstractCollectorArgumentResolver
Meta.ARGUMENT_KEY
Parser#canParse
SchematicFastGenerator#isOptional
SimpleArgument#constructor
AnnotationHolder#getFormat
Requirement#getWrapperFormat
Argument#getWrapperFormat
BindRequirement.of(Supplier, WrapFormat) -> BindRequirement.of(Supplier, TypeToken)
ContextRequirement.of(Supplier, WrapFormat) -> ContextRequirement.of(Supplier, TypeToken)

relocation:

litecommands.annotations.argument.collector -> [...]litecommands.annotations.argument.collection
litecommands.requirement.BindRequirement -> litecommands.bind.BindRequirement
litecommands.requirement.ContextRequirement -> litecommands.context.ContextRequirement

CollectionArgument -> CollectionArgumentProfile
JoinArgument -> JoinProfile
FlagArgument -> FlagProfile

removed:

WrapperRegistry, Wrap, WrapFormat, ValueToWrap
Wrapper, CompletableFutureWrapper, OptionaWrapper, ValueWrapper
TypedArgumentResolver, TypedParser, TypedSuggester
LiteCommandsBuilder#wrapper()
RequirementProcessor
AnnotationHolder

@Rollczi Rollczi changed the title Support async parse result. WiP Support async parse result. Oct 4, 2024
@Rollczi Rollczi changed the title Support async parse result. GH-427 Support async parse result. Oct 4, 2024
@Rollczi Rollczi merged commit 723a091 into master Oct 5, 2024
6 checks passed
@Rollczi Rollczi deleted the support-async-parse-result branch October 5, 2024 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Async context and arguments
1 participant