Skip to content

Commit bc2e3ae

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
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 <[email protected]> gopls-CI: kokoro <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent a9b653b commit bc2e3ae

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

internal/jsonrpc2_v2/conn.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ type Binder interface {
2828
Bind(context.Context, *Connection) (ConnectionOptions, error)
2929
}
3030

31+
// A BinderFunc implements the Binder interface for a standalone Bind function.
32+
type BinderFunc func(context.Context, *Connection) (ConnectionOptions, error)
33+
34+
func (f BinderFunc) Bind(ctx context.Context, c *Connection) (ConnectionOptions, error) {
35+
return f(ctx, c)
36+
}
37+
38+
var _ Binder = BinderFunc(nil)
39+
3140
// ConnectionOptions holds the options for new connections.
3241
type ConnectionOptions struct {
3342
// Framer allows control over the message framing and encoding.

internal/jsonrpc2_v2/jsonrpc2.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ type Preempter interface {
4747
Preempt(ctx context.Context, req *Request) (result interface{}, err error)
4848
}
4949

50+
// A PreempterFunc implements the Preempter interface for a standalone Preempt function.
51+
type PreempterFunc func(ctx context.Context, req *Request) (interface{}, error)
52+
53+
func (f PreempterFunc) Preempt(ctx context.Context, req *Request) (interface{}, error) {
54+
return f(ctx, req)
55+
}
56+
57+
var _ Preempter = PreempterFunc(nil)
58+
5059
// Handler handles messages on a connection.
5160
type Handler interface {
5261
// Handle is invoked sequentially for each incoming request that has not
@@ -75,12 +84,15 @@ func (defaultHandler) Handle(context.Context, *Request) (interface{}, error) {
7584
return nil, ErrNotHandled
7685
}
7786

87+
// A HandlerFunc implements the Handler interface for a standalone Handle function.
7888
type HandlerFunc func(ctx context.Context, req *Request) (interface{}, error)
7989

8090
func (f HandlerFunc) Handle(ctx context.Context, req *Request) (interface{}, error) {
8191
return f(ctx, req)
8292
}
8393

94+
var _ Handler = HandlerFunc(nil)
95+
8496
// async is a small helper for operations with an asynchronous result that you
8597
// can wait for.
8698
type async struct {

0 commit comments

Comments
 (0)