Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
go:
- 1.16
- 1.26.4
name: Go ${{ matrix.go }}
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 2 additions & 2 deletions call_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c callOptionFunc) apply(r *Request) error { return c(r) }
// Meta returns a call option which attaches the given meta object to
// the JSON-RPC 2.0 request (this is a Sourcegraph extension to JSON
// RPC 2.0 for carrying metadata).
func Meta(meta interface{}) CallOption {
func Meta(meta any) CallOption {
return callOptionFunc(func(r *Request) error {
return r.SetMeta(meta)
})
Expand All @@ -22,7 +22,7 @@ func Meta(meta interface{}) CallOption {
// ExtraField returns a call option which attaches the given name/value pair to
// the JSON-RPC 2.0 request. This can be used to add arbitrary extensions to
// JSON RPC 2.0.
func ExtraField(name string, value interface{}) CallOption {
func ExtraField(name string, value any) CallOption {
return callOptionFunc(func(r *Request) error {
return r.SetExtraField(name, value)
})
Expand Down
11 changes: 4 additions & 7 deletions call_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
)

func TestPickID(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

a, b := inMemoryPeerConns()
defer a.Close()
Expand All @@ -27,7 +26,7 @@ func TestPickID(t *testing.T) {
defer connB.Close()

const n = 100
for i := 0; i < n; i++ {
for i := range n {
var opts []jsonrpc2.CallOption
id := jsonrpc2.ID{Num: uint64(i)}

Expand All @@ -54,8 +53,7 @@ func TestPickID(t *testing.T) {

func TestStringID(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

a, b := inMemoryPeerConns()
defer a.Close()
Expand Down Expand Up @@ -93,8 +91,7 @@ func TestStringID(t *testing.T) {

func TestExtraField(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

a, b := inMemoryPeerConns()
defer a.Close()
Expand Down
20 changes: 10 additions & 10 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *Conn) Close() error {
// waits for the response. If the response is successful, its result is stored
// in result (a pointer to a value that can be JSON-unmarshaled into);
// otherwise, a non-nil error is returned. See DispatchCall for more details.
func (c *Conn) Call(ctx context.Context, method string, params, result interface{}, opts ...CallOption) error {
func (c *Conn) Call(ctx context.Context, method string, params, result any, opts ...CallOption) error {
call, err := c.DispatchCall(ctx, method, params, opts...)
if err != nil {
return err
Expand All @@ -108,7 +108,7 @@ func (c *Conn) DisconnectNotify() <-chan struct{} {
// The params member is omitted from the JSON-RPC request if the given params is
// nil. Use json.RawMessage("null") to send a JSON-RPC request with its params
// member set to null.
func (c *Conn) DispatchCall(ctx context.Context, method string, params interface{}, opts ...CallOption) (Waiter, error) {
func (c *Conn) DispatchCall(ctx context.Context, method string, params any, opts ...CallOption) (Waiter, error) {
req := &Request{Method: method}
for _, opt := range opts {
if opt == nil {
Expand Down Expand Up @@ -137,7 +137,7 @@ func (c *Conn) DispatchCall(ctx context.Context, method string, params interface
// The params member is omitted from the JSON-RPC request if the given params is
// nil. Use json.RawMessage("null") to send a JSON-RPC request with its params
// member set to null.
func (c *Conn) Notify(ctx context.Context, method string, params interface{}, opts ...CallOption) error {
func (c *Conn) Notify(ctx context.Context, method string, params any, opts ...CallOption) error {
req := &Request{Method: method, Notif: true}
for _, opt := range opts {
if opt == nil {
Expand All @@ -157,7 +157,7 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}, op
}

// Reply sends a successful response with a result.
func (c *Conn) Reply(ctx context.Context, id ID, result interface{}) error {
func (c *Conn) Reply(ctx context.Context, id ID, result any) error {
resp := &Response{ID: id}
if err := resp.SetResult(result); err != nil {
return err
Expand Down Expand Up @@ -346,7 +346,7 @@ type Waiter struct {
// is successful, its result is stored in result (a pointer to a
// value that can be JSON-unmarshaled into); otherwise, a non-nil
// error is returned.
func (w Waiter) Wait(ctx context.Context, result interface{}) error {
func (w Waiter) Wait(ctx context.Context, result any) error {
select {
case <-ctx.Done():
return ctx.Err()
Expand Down Expand Up @@ -380,7 +380,7 @@ type anyMessage struct {
}

func (m anyMessage) MarshalJSON() ([]byte, error) {
var v interface{}
var v any
switch {
case m.request != nil && m.response == nil:
v = m.request
Expand All @@ -397,10 +397,10 @@ func (m *anyMessage) UnmarshalJSON(data []byte) error {
// The presence of these fields distinguishes between the 2
// message types.
type msg struct {
ID interface{} `json:"id"`
ID any `json:"id"`
Method *string `json:"method"`
Result anyValueWithExplicitNull `json:"result"`
Error interface{} `json:"error"`
Error any `json:"error"`
}

var isRequest, isResponse bool
Expand Down Expand Up @@ -441,7 +441,7 @@ func (m *anyMessage) UnmarshalJSON(data []byte) error {
}
}

var v interface{}
var v any
switch {
case isRequest && !isResponse:
v = &m.request
Expand All @@ -461,7 +461,7 @@ func (m *anyMessage) UnmarshalJSON(data []byte) error {
// {"result":null} by anyMessage's JSON unmarshaler.
type anyValueWithExplicitNull struct {
null bool // JSON "null"
value interface{}
value any
}

func (v anyValueWithExplicitNull) MarshalJSON() ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion conn_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Logger interface implements one method - Printf.
// You can use the stdlib logger *log.Logger
type Logger interface {
Printf(format string, v ...interface{})
Printf(format string, v ...any)
}

// ConnOpt is the type of function that can be passed to NewConn to
Expand Down
6 changes: 2 additions & 4 deletions conn_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func TestSetLogger(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

rd, wr := io.Pipe()
defer rd.Close()
Expand Down Expand Up @@ -67,8 +66,7 @@ func (h *dummyHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jso
}

func TestLogMessages(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

rd, wr := io.Pipe()
defer rd.Close()
Expand Down
5 changes: 2 additions & 3 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestConn(t *testing.T) {
}

var paramsTests = []struct {
sendParams interface{}
sendParams any
wantParams *json.RawMessage
}{
{
Expand Down Expand Up @@ -220,8 +220,7 @@ func TestConn_Close(t *testing.T) {
}}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

connA, connB := net.Pipe()
nodeA := jsonrpc2.NewConn(
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/sourcegraph/jsonrpc2

go 1.12
go 1.26.4

require github.com/gorilla/websocket v1.4.1
4 changes: 2 additions & 2 deletions handler_with_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

// HandlerWithError implements Handler by calling the func for each
// request and handling returned errors and results.
func HandlerWithError(handleFunc func(context.Context, *Conn, *Request) (result interface{}, err error)) *HandlerWithErrorConfigurer {
func HandlerWithError(handleFunc func(context.Context, *Conn, *Request) (result any, err error)) *HandlerWithErrorConfigurer {
return &HandlerWithErrorConfigurer{handleFunc: handleFunc}
}

// HandlerWithErrorConfigurer is a handler created by HandlerWithError.
type HandlerWithErrorConfigurer struct {
handleFunc func(context.Context, *Conn, *Request) (result interface{}, err error)
handleFunc func(context.Context, *Conn, *Request) (result any, err error)
suppressErrClosed bool
}

Expand Down
6 changes: 3 additions & 3 deletions jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
// an API boundary.
type JSONRPC2 interface {
// Call issues a standard request (http://www.jsonrpc.org/specification#request_object).
Call(ctx context.Context, method string, params, result interface{}, opt ...CallOption) error
Call(ctx context.Context, method string, params, result any, opt ...CallOption) error

// Notify issues a notification request (http://www.jsonrpc.org/specification#notification).
Notify(ctx context.Context, method string, params interface{}, opt ...CallOption) error
Notify(ctx context.Context, method string, params any, opt ...CallOption) error

// Close closes the underlying connection, if it exists.
Close() error
Expand All @@ -34,7 +34,7 @@ type Error struct {

// SetError sets e.Data to the JSON encoding of v. If JSON
// marshaling fails, it panics.
func (e *Error) SetError(v interface{}) {
func (e *Error) SetError(v any) {
b, err := json.Marshal(v)
if err != nil {
panic("Error.SetData: " + err.Error())
Expand Down
7 changes: 3 additions & 4 deletions jsonrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func testClientServer(ctx context.Context, t *testing.T, stream jsonrpc2.ObjectS

// Simple
const n = 100
for i := 0; i < n; i++ {
for i := range n {
var got string
if err := cc.Call(ctx, "f", []int32{1, 2, 3}, &got); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -268,8 +268,7 @@ func TestHandlerBlocking(t *testing.T) {
// We send N notifications with an increasing parameter. Since the
// handler is blocking, we expect to process the notifications in the
// order they are sent.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx := t.Context()

a, b := inMemoryPeerConns()
defer a.Close()
Expand All @@ -291,7 +290,7 @@ func TestHandlerBlocking(t *testing.T) {
defer connB.Close()

const n = 100
for i := 0; i < n; i++ {
for i := range n {
wg.Add(1)
if err := connB.Notify(ctx, "f", i); err != nil {
t.Fatal(err)
Expand Down
14 changes: 7 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Request struct {
// MarshalJSON implements json.Marshaler and adds the "jsonrpc":"2.0"
// property.
func (r Request) MarshalJSON() ([]byte, error) {
r2 := map[string]interface{}{
r2 := map[string]any{
"jsonrpc": "2.0",
"method": r.Method,
}
Expand All @@ -54,8 +54,8 @@ func (r Request) MarshalJSON() ([]byte, error) {

// UnmarshalJSON implements json.Unmarshaler.
func (r *Request) UnmarshalJSON(data []byte) error {
r2 := make(map[string]interface{})
pop := func(key string) interface{} {
r2 := make(map[string]any)
pop := func(key string) any {
defer delete(r2, key)
return r2[key]
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (r *Request) UnmarshalJSON(data []byte) error {

// SetParams sets r.Params to the JSON encoding of v. If JSON
// marshaling fails, it returns an error.
func (r *Request) SetParams(v interface{}) error {
func (r *Request) SetParams(v any) error {
b, err := json.Marshal(v)
if err != nil {
return err
Expand All @@ -147,7 +147,7 @@ func (r *Request) SetParams(v interface{}) error {

// SetMeta sets r.Meta to the JSON encoding of v. If JSON
// marshaling fails, it returns an error.
func (r *Request) SetMeta(v interface{}) error {
func (r *Request) SetMeta(v any) error {
b, err := json.Marshal(v)
if err != nil {
return err
Expand All @@ -159,7 +159,7 @@ func (r *Request) SetMeta(v interface{}) error {
// SetExtraField adds an entry to r.ExtraFields, so that it is added to the
// JSON encoding of the request, as a way to add arbitrary extensions to
// JSON RPC 2.0. If JSON marshaling fails, it returns an error.
func (r *Request) SetExtraField(name string, v interface{}) error {
func (r *Request) SetExtraField(name string, v any) error {
switch name {
case "id", "jsonrpc", "meta", "method", "params":
return fmt.Errorf("invalid extra field %q", name)
Expand All @@ -174,5 +174,5 @@ func (r *Request) SetExtraField(name string, v interface{}) error {
// RequestField is a top-level field that can be added to the JSON-RPC request.
type RequestField struct {
Name string
Value interface{}
Value any
}
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *Response) UnmarshalJSON(data []byte) error {

// SetResult sets r.Result to the JSON representation of v. If JSON
// marshaling fails, it returns an error.
func (r *Response) SetResult(v interface{}) error {
func (r *Response) SetResult(v any) error {
b, err := json.Marshal(v)
if err != nil {
return err
Expand Down
Loading
Loading