internal/jsonrpc2_v2: add Func convenience wrappers for the Binder and Preempter interfaces

Change-Id: Ib21dabb908c13d9bbf50f053a342a8644d3ee68b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/388596
Run-TryBot: Bryan Mills <bcmills@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Bryan C. Mills 2022-02-28 11:24:36 -05:00 committed by Gopher Robot
parent a9b653b411
commit bc2e3aeaba
2 changed files with 21 additions and 0 deletions

View File

@ -28,6 +28,15 @@ type Binder interface {
Bind(context.Context, *Connection) (ConnectionOptions, error)
}
// A BinderFunc implements the Binder interface for a standalone Bind function.
type BinderFunc func(context.Context, *Connection) (ConnectionOptions, error)
func (f BinderFunc) Bind(ctx context.Context, c *Connection) (ConnectionOptions, error) {
return f(ctx, c)
}
var _ Binder = BinderFunc(nil)
// ConnectionOptions holds the options for new connections.
type ConnectionOptions struct {
// Framer allows control over the message framing and encoding.

View File

@ -47,6 +47,15 @@ type Preempter interface {
Preempt(ctx context.Context, req *Request) (result interface{}, err error)
}
// A PreempterFunc implements the Preempter interface for a standalone Preempt function.
type PreempterFunc func(ctx context.Context, req *Request) (interface{}, error)
func (f PreempterFunc) Preempt(ctx context.Context, req *Request) (interface{}, error) {
return f(ctx, req)
}
var _ Preempter = PreempterFunc(nil)
// Handler handles messages on a connection.
type Handler interface {
// Handle is invoked sequentially for each incoming request that has not
@ -75,12 +84,15 @@ func (defaultHandler) Handle(context.Context, *Request) (interface{}, error) {
return nil, ErrNotHandled
}
// A HandlerFunc implements the Handler interface for a standalone Handle function.
type HandlerFunc func(ctx context.Context, req *Request) (interface{}, error)
func (f HandlerFunc) Handle(ctx context.Context, req *Request) (interface{}, error) {
return f(ctx, req)
}
var _ Handler = HandlerFunc(nil)
// async is a small helper for operations with an asynchronous result that you
// can wait for.
type async struct {