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

Implementation of ValidateAndGetBestMatch #1700

Merged
merged 7 commits into from
Nov 26, 2021

Conversation

alikindsys
Copy link
Contributor

As discussed on #1699 this pr comes with a rewrite of the logic on CommandService#ExecuteAsync, splitting it into two functions.

ExecuteAsync - will continue to do the Search-Verify-Match-Execute loop as it did before.

ValidateAndGetBestMatch - Verifies from a set of commands (SearchResult) with the specified ICommandContext, IServiceProvider and MultiMatchHandling which command is the optimal one, using the previously established classification and ranking systems.

Breaking Changes

None.

Focus

Code cleanup and creation of an new public api.

This function will validate all commands from a SearchResult and return the result of said validation, along with the command matched, if a valid match was found.
@quinchs quinchs added enhancement priority: improvement V3 Todo Something that needs to be done for V3 labels Nov 24, 2021
@quinchs
Copy link
Member

quinchs commented Nov 24, 2021

Going to cc @Cenngo on this just for sanity

Comment on lines 558 to 561
public async Task<(IResult, Optional<CommandMatch>)> ValidateAndGetBestMatch(SearchResult matches, ICommandContext context, IServiceProvider provider, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
{
if (!matches.IsSuccess)
return (matches, Optional.Create<CommandMatch>());
Copy link
Collaborator

@Cenngo Cenngo Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think returning an optional CommandMatch to indicate to the user that their SearchResult wasnt successful isnt the most user friendly way of validating a command search. Instead of returning a Tuple, i think you should encapsulate this in an IResult that way you can provide more information to the user and it would be easier to build a command execution pipeline around this, since your goal is to allow people to utilize this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

//Since ValidateAndGetBestMatch is deterministic on the return type, we can use pattern matching on the type for infering the code flow.
var (validationResult, commandMatch) = await ValidateAndGetBestMatch(searchResult, context, services, multiMatchHandling);

if(validationResult is SearchResult)
Copy link
Collaborator

@Cenngo Cenngo Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tho i understand the way you are using this workflow to branch out the execution method, i dont think it is the most descript way of doing this. I think this should be more user readable to make it easier to maintain. Using an IResult to return the method result, with some additional info for branching out the pipeline, might be a better idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood.

@Cenngo
Copy link
Collaborator

Cenngo commented Nov 24, 2021

Other than that, it all seems good.

@quinchs
Copy link
Member

quinchs commented Nov 25, 2021

@roridev Are you able to make the requested changes?

@alikindsys
Copy link
Contributor Author

Im able now, just give me a moment

Changes:
- Use IResult instead of Optional CommandMatch

- Rework branching workflow
@alikindsys
Copy link
Contributor Author

alikindsys commented Nov 25, 2021

Please check if the changes done align with what was requested.

Fun things that were discovered while doing this:

  • A whole branch of execution was missing on my old code. The event wouldn't be fired if ValidateAndGetBestMatch returned SearchResult. This is now fixed.

@quinchs
Copy link
Member

quinchs commented Nov 25, 2021

@Cenngo waiting on your final approval

Copy link
Collaborator

@Cenngo Cenngo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes you made address the issues i mentioned. Great to hear that this also led you to fixing a problem.

return validationResult;
}

private async Task<IResult> handleCommandPipeline(MatchResult matchResult, ICommandContext context, IServiceProvider services)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please make this pascal casing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sure rider autocorrected it to that.

@quinchs quinchs merged commit 3cd9f39 into discord-net:dev Nov 26, 2021
@quinchs quinchs linked an issue Nov 26, 2021 that may be closed by this pull request
3 tasks
@alikindsys alikindsys deleted the commands/validate-get-best-match branch November 26, 2021 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement priority: improvement V3 Todo Something that needs to be done for V3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rework CommandService#ExecuteAsync logic and allow easier extensibility.
4 participants