Browse Source

rpc/lib/types: RPCResponse.Result is not a pointer

pull/788/head
Ethan Buchman 7 years ago
parent
commit
593c127257
5 changed files with 16 additions and 17 deletions
  1. +1
    -1
      rpc/client/httpclient.go
  2. +1
    -1
      rpc/lib/client/http_client.go
  3. +2
    -2
      rpc/lib/client/ws_client_test.go
  4. +4
    -4
      rpc/lib/rpc_test.go
  5. +8
    -9
      rpc/lib/types/types.go

+ 1
- 1
rpc/client/httpclient.go View File

@ -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: ?


+ 1
- 1
rpc/lib/client/http_client.go View File

@ -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)
}


+ 2
- 2
rpc/lib/client/ws_client_test.go View File

@ -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:


+ 4
- 4
rpc/lib/rpc_test.go View File

@ -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)


+ 8
- 9
rpc/lib/types/types.go View File

@ -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)


Loading…
Cancel
Save