From bc2e3aeaba30b86fc421aefdfc78094b43700314 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Mon, 28 Feb 2022 11:24:36 -0500 Subject: [PATCH] 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 gopls-CI: kokoro Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Alan Donovan --- internal/jsonrpc2_v2/conn.go | 9 +++++++++ internal/jsonrpc2_v2/jsonrpc2.go | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/internal/jsonrpc2_v2/conn.go b/internal/jsonrpc2_v2/conn.go index f010d9a055..7995f404e5 100644 --- a/internal/jsonrpc2_v2/conn.go +++ b/internal/jsonrpc2_v2/conn.go @@ -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. diff --git a/internal/jsonrpc2_v2/jsonrpc2.go b/internal/jsonrpc2_v2/jsonrpc2.go index e685584427..e9164b0bc9 100644 --- a/internal/jsonrpc2_v2/jsonrpc2.go +++ b/internal/jsonrpc2_v2/jsonrpc2.go @@ -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 {