1
0
Fork 0
mirror of https://github.com/sourcegraph/jsonrpc2.git synced 2026-07-04 16:23:41 +02:00

fix some golangci-lint, revive linter warnings (#25)

fix some golangci-lint, revive linter warnings
This commit is contained in:
Quinn Slack 2019-11-02 21:42:37 -07:00 committed by GitHub
commit 8819199291
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 38 deletions

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module github.com/sourcegraph/jsonrpc2
go 1.13
require github.com/gorilla/websocket v1.4.1

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

View file

@ -11,6 +11,8 @@ func HandlerWithError(handleFunc func(context.Context, *Conn, *Request) (result
return &HandlerWithErrorConfigurer{handleFunc: handleFunc} return &HandlerWithErrorConfigurer{handleFunc: handleFunc}
} }
// HandlerWithErrorConfigurer is a handler created by HandlerWithError.
type HandlerWithErrorConfigurer struct { type HandlerWithErrorConfigurer struct {
handleFunc func(context.Context, *Conn, *Request) (result interface{}, err error) handleFunc func(context.Context, *Conn, *Request) (result interface{}, err error)
suppressErrClosed bool suppressErrClosed bool

View file

@ -84,11 +84,12 @@ func (r *Request) UnmarshalJSON(data []byte) error {
return err return err
} }
r.Method = r2.Method r.Method = r2.Method
if r2.Params == nil { switch {
case r2.Params == nil:
r.Params = &jsonNull r.Params = &jsonNull
} else if len(*r2.Params) == 0 { case len(*r2.Params) == 0:
r.Params = nil r.Params = nil
} else { default:
r.Params = r2.Params r.Params = r2.Params
} }
r.Meta = r2.Meta r.Meta = r2.Meta
@ -212,22 +213,20 @@ func (e *Error) Error() string {
return fmt.Sprintf("jsonrpc2: code %v message: %s", e.Code, e.Message) return fmt.Sprintf("jsonrpc2: code %v message: %s", e.Code, e.Message)
} }
// Errors defined in the JSON-RPC spec. See
// http://www.jsonrpc.org/specification#error_object.
const ( const (
// Errors defined in the JSON-RPC spec. See
// http://www.jsonrpc.org/specification#error_object.
CodeParseError = -32700 CodeParseError = -32700
CodeInvalidRequest = -32600 CodeInvalidRequest = -32600
CodeMethodNotFound = -32601 CodeMethodNotFound = -32601
CodeInvalidParams = -32602 CodeInvalidParams = -32602
CodeInternalError = -32603 CodeInternalError = -32603
codeServerErrorStart = -32099
codeServerErrorEnd = -32000
) )
// Handler handles JSON-RPC requests and notifications. // Handler handles JSON-RPC requests and notifications.
type Handler interface { type Handler interface {
// Handle is called to handle a request. No other requests are handled // Handle is called to handle a request. No other requests are handled
// until it returns. If you do not require strict ordering behaviour // until it returns. If you do not require strict ordering behavior
// of received RPCs, it is suggested to wrap your handler in // of received RPCs, it is suggested to wrap your handler in
// AsyncHandler. // AsyncHandler.
Handle(context.Context, *Conn, *Request) Handle(context.Context, *Conn, *Request)
@ -345,7 +344,7 @@ func (c *Conn) Close() error {
return c.stream.Close() return c.stream.Close()
} }
func (c *Conn) send(ctx context.Context, m *anyMessage, wait bool) (cc *call, err error) { func (c *Conn) send(_ context.Context, m *anyMessage, wait bool) (cc *call, err error) {
c.sending.Lock() c.sending.Lock()
defer c.sending.Unlock() defer c.sending.Unlock()
@ -441,7 +440,6 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
if call.response.Result == nil { if call.response.Result == nil {
call.response.Result = &jsonNull call.response.Result = &jsonNull
} }
// TODO(sqs): error handling
if err := json.Unmarshal(*call.response.Result, result); err != nil { if err := json.Unmarshal(*call.response.Result, result); err != nil {
return err return err
} }
@ -648,11 +646,11 @@ func (m *anyMessage) UnmarshalJSON(data []byte) error {
} }
} }
} else { } else {
var msg msg var m msg
if err := json.Unmarshal(data, &msg); err != nil { if err := json.Unmarshal(data, &m); err != nil {
return err return err
} }
if err := checkType(&msg); err != nil { if err := checkType(&m); err != nil {
return err return err
} }
} }
@ -694,7 +692,3 @@ func (v *anyValueWithExplicitNull) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &v.value) return json.Unmarshal(data, &v.value)
} }
var (
errInvalidRequestJSON = errors.New("jsonrpc2: request must be either a JSON object or JSON array")
errInvalidResponseJSON = errors.New("jsonrpc2: response must be either a JSON object or JSON array")
)

View file

@ -42,11 +42,11 @@ func TestResponse_MarshalJSON_jsonrpc(t *testing.T) {
func TestResponseMarshalJSON_Notif(t *testing.T) { func TestResponseMarshalJSON_Notif(t *testing.T) {
tests := map[*jsonrpc2.Request]bool{ tests := map[*jsonrpc2.Request]bool{
&jsonrpc2.Request{ID: jsonrpc2.ID{Num: 0}}: true, {ID: jsonrpc2.ID{Num: 0}}: true,
&jsonrpc2.Request{ID: jsonrpc2.ID{Num: 1}}: true, {ID: jsonrpc2.ID{Num: 1}}: true,
&jsonrpc2.Request{ID: jsonrpc2.ID{Str: "", IsString: true}}: true, {ID: jsonrpc2.ID{Str: "", IsString: true}}: true,
&jsonrpc2.Request{ID: jsonrpc2.ID{Str: "a", IsString: true}}: true, {ID: jsonrpc2.ID{Str: "a", IsString: true}}: true,
&jsonrpc2.Request{Notif: true}: false, {Notif: true}: false,
} }
for r, wantIDKey := range tests { for r, wantIDKey := range tests {
b, err := json.Marshal(r) b, err := json.Marshal(r)
@ -125,7 +125,7 @@ func TestClientServer(t *testing.T) {
if lis == nil { if lis == nil {
return // already closed return // already closed
} }
if err := lis.Close(); err != nil { if err = lis.Close(); err != nil {
if !strings.HasSuffix(err.Error(), "use of closed network connection") { if !strings.HasSuffix(err.Error(), "use of closed network connection") {
t.Fatal(err) t.Fatal(err)
} }
@ -134,7 +134,7 @@ func TestClientServer(t *testing.T) {
ha := testHandlerA{t: t} ha := testHandlerA{t: t}
go func() { go func() {
if err := serve(ctx, lis, &ha); err != nil { if err = serve(ctx, lis, &ha); err != nil {
if !strings.HasSuffix(err.Error(), "use of closed network connection") { if !strings.HasSuffix(err.Error(), "use of closed network connection") {
t.Error(err) t.Error(err)
} }
@ -169,10 +169,11 @@ func TestClientServer(t *testing.T) {
})) }))
defer s.Close() defer s.Close()
c, _, err := websocket.DefaultDialer.Dial(strings.Replace(s.URL, "http:", "ws:", 1), nil) c, resp, err := websocket.DefaultDialer.Dial(strings.Replace(s.URL, "http:", "ws:", 1), nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer resp.Body.Close()
defer c.Close() defer c.Close()
testClientServer(ctx, t, websocketjsonrpc2.NewObjectStream(c)) testClientServer(ctx, t, websocketjsonrpc2.NewObjectStream(c))
@ -296,9 +297,10 @@ func TestHandlerBlocking(t *testing.T) {
a, b := inMemoryPeerConns() a, b := inMemoryPeerConns()
defer a.Close() defer a.Close()
defer b.Close() defer b.Close()
var (
var wg sync.WaitGroup wg sync.WaitGroup
var params []int params []int
)
handler := handlerFunc(func(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) { handler := handlerFunc(func(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
var i int var i int
_ = json.Unmarshal(*req.Params, &i) _ = json.Unmarshal(*req.Params, &i)

View file

@ -5,18 +5,18 @@ package websocket
import ( import (
"io" "io"
"github.com/gorilla/websocket" ws "github.com/gorilla/websocket"
) )
// A ObjectStream is a jsonrpc2.ObjectStream that uses a WebSocket to // A ObjectStream is a jsonrpc2.ObjectStream that uses a WebSocket to
// send and receive JSON-RPC 2.0 objects. // send and receive JSON-RPC 2.0 objects.
type ObjectStream struct { type ObjectStream struct {
conn *websocket.Conn conn *ws.Conn
} }
// NewObjectStream creates a new jsonrpc2.ObjectStream for sending and // NewObjectStream creates a new jsonrpc2.ObjectStream for sending and
// receiving JSON-RPC 2.0 objects over a WebSocket. // receiving JSON-RPC 2.0 objects over a WebSocket.
func NewObjectStream(conn *websocket.Conn) ObjectStream { func NewObjectStream(conn *ws.Conn) ObjectStream {
return ObjectStream{conn: conn} return ObjectStream{conn: conn}
} }
@ -28,8 +28,8 @@ func (t ObjectStream) WriteObject(obj interface{}) error {
// ReadObject implements jsonrpc2.ObjectStream. // ReadObject implements jsonrpc2.ObjectStream.
func (t ObjectStream) ReadObject(v interface{}) error { func (t ObjectStream) ReadObject(v interface{}) error {
err := t.conn.ReadJSON(v) err := t.conn.ReadJSON(v)
if e, ok := err.(*websocket.CloseError); ok { if e, ok := err.(*ws.CloseError); ok {
if e.Code == websocket.CloseAbnormalClosure && e.Text == io.ErrUnexpectedEOF.Error() { if e.Code == ws.CloseAbnormalClosure && e.Text == io.ErrUnexpectedEOF.Error() {
// Suppress a noisy (but harmless) log message by // Suppress a noisy (but harmless) log message by
// unwrapping this error. // unwrapping this error.
err = io.ErrUnexpectedEOF err = io.ErrUnexpectedEOF