creachadair / jrpc2

A Go implementation of a JSON-RPC 2.0 server and client.
BSD 3-Clause "New" or "Revised" License
62 stars 10 forks source link

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

Closed creachadair closed 1 year ago

creachadair commented 1 year ago

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 commented 1 year ago

@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.