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

fix some golangci-lint, revive linter warnings

Signed-off-by: s3rj1k <evasive.gyron@gmail.com>
This commit is contained in:
s3rj1k 2019-11-02 22:31:21 +02:00
parent 35a74f039c
commit 456318691a
4 changed files with 43 additions and 36 deletions

View file

@ -11,6 +11,7 @@ func HandlerWithError(handleFunc func(context.Context, *Conn, *Request) (result
return &HandlerWithErrorConfigurer{handleFunc: handleFunc} return &HandlerWithErrorConfigurer{handleFunc: handleFunc}
} }
// HandlerWithErrorConfigurer is a handler configuration struct
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,22 @@ 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 CodeParseError = -32700
// http://www.jsonrpc.org/specification#error_object. CodeInvalidRequest = -32600
CodeParseError = -32700 CodeMethodNotFound = -32601
CodeInvalidRequest = -32600 CodeInvalidParams = -32602
CodeMethodNotFound = -32601 CodeInternalError = -32603
CodeInvalidParams = -32602 // codeServerErrorStart = -32099
CodeInternalError = -32603 // codeServerErrorEnd = -32000
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 +346,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 +442,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
} }
@ -642,17 +642,18 @@ func (m *anyMessage) UnmarshalJSON(data []byte) error {
if len(msgs) == 0 { if len(msgs) == 0 {
return errors.New("jsonrpc2: invalid empty batch") return errors.New("jsonrpc2: invalid empty batch")
} }
for _, msg := range msgs { for i := range msgs {
if err := checkType(&msg); err != nil { var m = msgs[i]
if err := checkType(&m); err != nil {
return err return err
} }
} }
} 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 +695,10 @@ func (v *anyValueWithExplicitNull) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &v.value) return json.Unmarshal(data, &v.value)
} }
/*
var ( var (
errInvalidRequestJSON = errors.New("jsonrpc2: request must be either a JSON object or JSON array") 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") 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