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

Bot: v2: AddMiddleware to use Go methods instead of arbitrary strings #146

Closed
diamondburned opened this issue Sep 25, 2020 · 0 comments
Closed

Comments

@diamondburned
Copy link
Owner

Preamble

As of right now, the AddMiddleware API takes in an arbitrary string for method names. This is because at the time of writing this API, I did not know that method names could be taken by passing the method function in.

Code example:

func main() {
	var t T
	printMethod(t.Method_dash)
}

type T struct{}

func (T) Method_dash() {}

func printMethod(v interface{}) {
	ptr := reflect.ValueOf(v).Pointer()
	fmt.Println(runtime.FuncForPC(ptr).Name()) // Output: main.T.Method_dash-fm
}

As the example's output shows, some string manipulation will be needed.

Proposal

This issue proposes the new AddMiddleware API to have this function signature:

func (sub *Subcommand) AddMiddleware(methodName, middleware interface{})

To preserve backwards compatibility, AddMiddleware will use reflect.Kind to ensure that the passed in type is only either a string or a function. If a string is provided, the old behavior will run. If a function is provided, the library will use the above method to get the method name, then execute the old behavior with that name. If any other type is given, the function will panic.

An example of this API would be:

func (t *Tags) Setup(sub *bot.Subcommand) {
	sub.AddMiddleware(t.A, func() {}) // new behavior
	sub.AddMiddleware("B", func() {}) // old behavior
}

func (t *Tags) A(*gateway.MessageCreateEvent) {}
func (t *Tags) B(*gateway.MessageCreateEvent) {}
@diamondburned diamondburned changed the title AddMiddleware to use Go methods instead of arbitrary strings Bot: AddMiddleware to use Go methods instead of arbitrary strings Sep 25, 2020
@diamondburned diamondburned changed the title Bot: AddMiddleware to use Go methods instead of arbitrary strings Bot: v2: AddMiddleware to use Go methods instead of arbitrary strings Nov 12, 2020
diamondburned added a commit that referenced this issue Nov 13, 2020
This commit adds subcommand aliases as well as additional code in
HelpGenerate to cover for both subcommand and command aliases.

A breaking change is that {,Must}RegisterSubcommandCustom methods are
now replaced with normal {,Must}RegisterSubcommand methods. This is
because they now use variadic strings, which could take 0, 1 or more
arguments.

This commit also allows AddMiddleware and similar methods to be given a
method directly:

    sub.Plumb(cmds.PlumbedHandler)
    sub.AddMiddleware(cmds.PlumbedHandler, cmds.plumbMiddleware)

This change closes issue #146.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant