From 593c127257fc4d903f4b56ed68f3098a5df6c6ac Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 8 Nov 2017 00:25:36 +0000 Subject: [PATCH] rpc/lib/types: RPCResponse.Result is not a pointer --- rpc/client/httpclient.go | 2 +- rpc/lib/client/http_client.go | 2 +- rpc/lib/client/ws_client_test.go | 4 ++-- rpc/lib/rpc_test.go | 8 ++++---- rpc/lib/types/types.go | 17 ++++++++--------- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/rpc/client/httpclient.go b/rpc/client/httpclient.go index 66cf89165..bf901e96b 100644 --- a/rpc/client/httpclient.go +++ b/rpc/client/httpclient.go @@ -311,7 +311,7 @@ func (w *WSEvents) eventListener() { continue } result := new(ctypes.ResultEvent) - err := json.Unmarshal(*resp.Result, result) + err := json.Unmarshal(resp.Result, result) if err != nil { // ignore silently (eg. subscribe, unsubscribe and maybe other events) // TODO: ? diff --git a/rpc/lib/client/http_client.go b/rpc/lib/client/http_client.go index 1f06112d2..f19c2e941 100644 --- a/rpc/lib/client/http_client.go +++ b/rpc/lib/client/http_client.go @@ -153,7 +153,7 @@ func unmarshalResponseBytes(responseBytes []byte, result interface{}) (interface return nil, errors.Errorf("Response error: %v", response.Error) } // unmarshal the RawMessage into the result - err = json.Unmarshal(*response.Result, result) + err = json.Unmarshal(response.Result, result) if err != nil { return nil, errors.Errorf("Error unmarshalling rpc response result: %v", err) } diff --git a/rpc/lib/client/ws_client_test.go b/rpc/lib/client/ws_client_test.go index 23f19dc00..190cbcdc2 100644 --- a/rpc/lib/client/ws_client_test.go +++ b/rpc/lib/client/ws_client_test.go @@ -46,7 +46,7 @@ func (h *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.mtx.RUnlock() res := json.RawMessage(`{}`) - emptyRespBytes, _ := json.Marshal(types.RPCResponse{Result: &res}) + emptyRespBytes, _ := json.Marshal(types.RPCResponse{Result: res}) if err := conn.WriteMessage(messageType, emptyRespBytes); err != nil { return } @@ -204,7 +204,7 @@ func callWgDoneOnResult(t *testing.T, c *WSClient, wg *sync.WaitGroup) { if resp.Error != nil { t.Fatalf("unexpected error: %v", resp.Error) } - if *resp.Result != nil { + if resp.Result != nil { wg.Done() } case <-c.Quit: diff --git a/rpc/lib/rpc_test.go b/rpc/lib/rpc_test.go index aa7319026..b5af0e43a 100644 --- a/rpc/lib/rpc_test.go +++ b/rpc/lib/rpc_test.go @@ -223,7 +223,7 @@ func echoViaWS(cl *client.WSClient, val string) (string, error) { } result := new(ResultEcho) - err = json.Unmarshal(*msg.Result, result) + err = json.Unmarshal(msg.Result, result) if err != nil { return "", nil } @@ -247,7 +247,7 @@ func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) { } result := new(ResultEchoBytes) - err = json.Unmarshal(*msg.Result, result) + err = json.Unmarshal(msg.Result, result) if err != nil { return []byte{}, nil } @@ -328,7 +328,7 @@ func TestWSNewWSRPCFunc(t *testing.T) { t.Fatal(err) } result := new(ResultEcho) - err = json.Unmarshal(*msg.Result, result) + err = json.Unmarshal(msg.Result, result) require.Nil(t, err) got := result.Value assert.Equal(t, got, val) @@ -353,7 +353,7 @@ func TestWSHandlesArrayParams(t *testing.T) { t.Fatalf("%+v", err) } result := new(ResultEcho) - err = json.Unmarshal(*msg.Result, result) + err = json.Unmarshal(msg.Result, result) require.Nil(t, err) got := result.Value assert.Equal(t, got, val) diff --git a/rpc/lib/types/types.go b/rpc/lib/types/types.go index 5bf95cc6d..d0c3d6787 100644 --- a/rpc/lib/types/types.go +++ b/rpc/lib/types/types.go @@ -67,14 +67,14 @@ func (err RPCError) Error() string { } type RPCResponse struct { - JSONRPC string `json:"jsonrpc"` - ID string `json:"id"` - Result *json.RawMessage `json:"result,omitempty"` - Error *RPCError `json:"error,omitempty"` + JSONRPC string `json:"jsonrpc"` + ID string `json:"id"` + Result json.RawMessage `json:"result,omitempty"` + Error *RPCError `json:"error,omitempty"` } func NewRPCSuccessResponse(id string, res interface{}) RPCResponse { - var raw *json.RawMessage + var rawMsg json.RawMessage if res != nil { var js []byte @@ -82,11 +82,10 @@ func NewRPCSuccessResponse(id string, res interface{}) RPCResponse { if err != nil { return RPCInternalError(id, errors.Wrap(err, "Error marshalling response")) } - rawMsg := json.RawMessage(js) - raw = &rawMsg + rawMsg = json.RawMessage(js) } - return RPCResponse{JSONRPC: "2.0", ID: id, Result: raw} + return RPCResponse{JSONRPC: "2.0", ID: id, Result: rawMsg} } func NewRPCErrorResponse(id string, code int, msg string, data string) RPCResponse { @@ -98,7 +97,7 @@ func NewRPCErrorResponse(id string, code int, msg string, data string) RPCRespon } func (resp RPCResponse) String() string { - if resp.Error == nil { + if resp.Error != nil { return fmt.Sprintf("[%s %v]", resp.ID, resp.Result) } else { return fmt.Sprintf("[%s %s]", resp.ID, resp.Error)