make RPCRequest params not a pointer

https://github.com/tendermint/tendermint/pull/724#issuecomment-335362927
This commit is contained in:
Anton Kaliaev 2017-10-10 13:50:06 +04:00
parent d935a4f0a8
commit aae4e94998
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
2 changed files with 18 additions and 15 deletions

View File

@ -129,10 +129,13 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
WriteRPCResponseHTTP(w, types.RPCMethodNotFoundError(request.ID)) WriteRPCResponseHTTP(w, types.RPCMethodNotFoundError(request.ID))
return return
} }
args, err := jsonParamsToArgsRPC(rpcFunc, request.Params) var args []reflect.Value
if err != nil { if len(request.Params) > 0 {
WriteRPCResponseHTTP(w, types.RPCInvalidParamsError(request.ID, errors.Wrap(err, "Error converting json params to arguments"))) args, err = jsonParamsToArgsRPC(rpcFunc, request.Params)
return if err != nil {
WriteRPCResponseHTTP(w, types.RPCInvalidParamsError(request.ID, errors.Wrap(err, "Error converting json params to arguments")))
return
}
} }
returns := rpcFunc.f.Call(args) returns := rpcFunc.f.Call(args)
logger.Info("HTTPJSONRPC", "method", request.Method, "args", args, "returns", returns) logger.Info("HTTPJSONRPC", "method", request.Method, "args", args, "returns", returns)
@ -210,13 +213,13 @@ func jsonParamsToArgs(rpcFunc *RPCFunc, raw []byte, argsOffset int) ([]reflect.V
} }
// Convert a []interface{} OR a map[string]interface{} to properly typed values // Convert a []interface{} OR a map[string]interface{} to properly typed values
func jsonParamsToArgsRPC(rpcFunc *RPCFunc, params *json.RawMessage) ([]reflect.Value, error) { func jsonParamsToArgsRPC(rpcFunc *RPCFunc, params json.RawMessage) ([]reflect.Value, error) {
return jsonParamsToArgs(rpcFunc, *params, 0) return jsonParamsToArgs(rpcFunc, params, 0)
} }
// Same as above, but with the first param the websocket connection // Same as above, but with the first param the websocket connection
func jsonParamsToArgsWS(rpcFunc *RPCFunc, params *json.RawMessage, wsCtx types.WSRPCContext) ([]reflect.Value, error) { func jsonParamsToArgsWS(rpcFunc *RPCFunc, params json.RawMessage, wsCtx types.WSRPCContext) ([]reflect.Value, error) {
values, err := jsonParamsToArgs(rpcFunc, *params, 1) values, err := jsonParamsToArgs(rpcFunc, params, 1)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -547,11 +550,11 @@ func (wsc *wsConnection) readRoutine() {
var args []reflect.Value var args []reflect.Value
if rpcFunc.ws { if rpcFunc.ws {
wsCtx := types.WSRPCContext{Request: request, WSRPCConnection: wsc} wsCtx := types.WSRPCContext{Request: request, WSRPCConnection: wsc}
if request.Params != nil { if len(request.Params) > 0 {
args, err = jsonParamsToArgsWS(rpcFunc, request.Params, wsCtx) args, err = jsonParamsToArgsWS(rpcFunc, request.Params, wsCtx)
} }
} else { } else {
if request.Params != nil { if len(request.Params) > 0 {
args, err = jsonParamsToArgsRPC(rpcFunc, request.Params) args, err = jsonParamsToArgsRPC(rpcFunc, request.Params)
} }
} }

View File

@ -14,10 +14,10 @@ import (
// REQUEST // REQUEST
type RPCRequest struct { type RPCRequest struct {
JSONRPC string `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
ID string `json:"id"` ID string `json:"id"`
Method string `json:"method"` Method string `json:"method"`
Params *json.RawMessage `json:"params"` // must be map[string]interface{} or []interface{} Params json.RawMessage `json:"params"` // must be map[string]interface{} or []interface{}
} }
func NewRPCRequest(id string, method string, params json.RawMessage) RPCRequest { func NewRPCRequest(id string, method string, params json.RawMessage) RPCRequest {
@ -25,7 +25,7 @@ func NewRPCRequest(id string, method string, params json.RawMessage) RPCRequest
JSONRPC: "2.0", JSONRPC: "2.0",
ID: id, ID: id,
Method: method, Method: method,
Params: &params, Params: params,
} }
} }