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

Replace the Handler interface with a plain function type. #90

Merged
merged 4 commits into from Dec 12, 2022

Conversation

creachadair
Copy link
Owner

@creachadair creachadair commented Dec 10, 2022

In practice there turns out to be no practical advantage to having the Handler type be an interface. All the existing usage that I can find, including in the handler support package, is based on explicit functions.

This change replaces the Handler interface with a type alias to the expected function signature for the interface's Handle method. Any existing use based on the interface can be updated by extracting the method directly. For example, given a type like:

type T struct{ ...
func (t *T) Handle(ctx context.Context, req *jrpc2.Request) (any, error) { ... }
h := &T{ ... }

Replace usage like:

m := handler.Map{"Method": h}

with:

m := handler.Map{"Method": h.Handle}

This is a breaking change to the package API.

@creachadair creachadair added enhancement New feature or request and removed enhancement New feature or request labels Dec 10, 2022
@creachadair
Copy link
Owner Author

@radeksimko I wanted to give you a chance to comment on this change before I merge and release it.

I patched this change locally against the terraform-ls package: It does require one small usage change at current line 579 of internal/langserver/handlers/service.go, but otherwise everything builds and tests pass locally both with and without -tags=longtest set.

Still, if you have other uses I haven't caught, or other concerns, please let me know.

In practice there turns out to be no practical advantage to having the Handler
type be an interface. All the existing usage that I can find, including in the
handler support package, is based on explicit functions.

This change replaces the Handler interface with a type alias to the expected
function signature for the interface's Handle method. Any existing use based on
the interface can be updated by extracting the method directly. For example,
given a type like:

    type T struct{ ...
    func (t *T) Handle(ctx context.Context, req *jrpc2.Request) (any, error) { ... }
    h := &T{ ... }

Replace usage like:

    m := handler.Map{"Method": h}

with:

    m := handler.Map{"Method": h.Handle}

This is a breaking change to the package API.
Copy link
Contributor

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

I can't think of any negative side effects, it looks like a reasonable change and thanks for the ping.

:shipit:

@creachadair creachadair merged commit 70c3cd8 into main Dec 12, 2022
@creachadair creachadair deleted the mjf/handler-func branch December 12, 2022 15:43
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

Successfully merging this pull request may close these issues.

None yet

2 participants