diff --git a/jsoncmd.go b/jsoncmd.go index 39913821..130898db 100644 --- a/jsoncmd.go +++ b/jsoncmd.go @@ -63,17 +63,26 @@ func NewRawCmd(id interface{}, method string, params []interface{}) (*RawCmd, er // RawCmdParser is a function to create a custom Cmd from a RawCmd. type RawCmdParser func(*RawCmd) (Cmd, error) +// ReplyParser is a function a custom Cmd can use to unmarshal the results of a +// reply into a concrete struct. +type ReplyParser func(json.RawMessage) (interface{}, error) + type cmd struct { - parser RawCmdParser - helpString string + parser RawCmdParser + replyParser ReplyParser + helpString string } var customCmds = make(map[string]cmd) -// RegisterCustomCmd registers a custom RawCmd parsing func for a -// non-standard Bitcoin command. -func RegisterCustomCmd(method string, parser RawCmdParser, helpString string) { - customCmds[method] = cmd{parser: parser, helpString: helpString} +// RegisterCustomCmd registers a custom RawCmd parsing func, reply parsing func, +// and help text for a non-standard Bitcoin command. +func RegisterCustomCmd(method string, parser RawCmdParser, replyParser ReplyParser, helpString string) { + customCmds[method] = cmd{ + parser: parser, + replyParser: replyParser, + helpString: helpString, + } } // ParseMarshaledCmd parses a raw command and unmarshals as a Cmd. diff --git a/jsonresults.go b/jsonresults.go index c767908c..095cfb7e 100644 --- a/jsonresults.go +++ b/jsonresults.go @@ -509,10 +509,21 @@ func ReadResultCmd(cmd string, message []byte) (Reply, error) { "getnewaddress", "sendtoaddress", "createrawtransaction", "sendrawtransaction", "getbestblockhash", "getrawchangeaddress": err = json.Unmarshal(message, &result) - // For anything else put it in an interface. All the data is still - // there, just a little less convenient to deal with. default: - err = json.Unmarshal(message, &result) + // None of the standard Bitcoin RPC methods matched. Try + // registered custom command reply parsers. + if c, ok := customCmds[cmd]; ok && c.replyParser != nil { + var res interface{} + res, err = c.replyParser(objmap["result"]) + if err == nil { + result.Result = res + } + } else { + // For anything else put it in an interface. All the + // data is still there, just a little less convenient + // to deal with. + err = json.Unmarshal(message, &result) + } } if err != nil { err = fmt.Errorf("Error unmarshalling json reply: %v", err) diff --git a/test_coverage.txt b/test_coverage.txt index ac28070a..4ed5f54e 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -436,12 +436,12 @@ github.com/conformal/btcjson/jsoncmd.go VerifyMessageCmd.UnmarshalJSON 68.42 github.com/conformal/btcjson/jsoncmd.go SignRawTransactionCmd.MarshalJSON 66.67% (8/12) github.com/conformal/btcjson/jsoncmd.go NewLockUnspentCmd 66.67% (4/6) github.com/conformal/btcjson/jsoncmd.go NewAddNodeCmd 66.67% (2/3) -github.com/conformal/btcjson/jsoncmd.go GetBlockCmd.MarshalJSON 63.64% (7/11) github.com/conformal/btcjson/jsonfxns.go jsonRpcSend 62.50% (10/16) github.com/conformal/btcjson/jsonapi.go rpcCommand 61.54% (8/13) +github.com/conformal/btcjson/jsoncmd.go GetBlockCmd.MarshalJSON 60.00% (6/10) github.com/conformal/btcjson/jsoncmd.go GetTxOutCmd.UnmarshalJSON 59.09% (13/22) -github.com/conformal/btcjson/jsonresults.go ReadResultCmd 58.73% (74/126) github.com/conformal/btcjson/jsoncmd.go LockUnspentCmd.UnmarshalJSON 57.89% (11/19) +github.com/conformal/btcjson/jsonresults.go ReadResultCmd 57.25% (75/131) github.com/conformal/btcjson/jsonapi.go rpcRawCommand 53.33% (8/15) github.com/conformal/btcjson/jsoncmd.go GetBlockCmd.UnmarshalJSON 50.00% (12/24) github.com/conformal/btcjson/cmdhelp.go GetHelpString 50.00% (3/6) @@ -462,5 +462,5 @@ github.com/conformal/btcjson/jsoncmd.go unparsableCmd.Method 0.00% (0/1) github.com/conformal/btcjson/jsonapi.go TlsRpcRawCommand 0.00% (0/1) github.com/conformal/btcjson/jsonapi.go RpcRawCommand 0.00% (0/1) github.com/conformal/btcjson/jsonresults.go Vin.IsCoinBase 0.00% (0/1) -github.com/conformal/btcjson --------------------------------------- 78.61% (2338/2974) +github.com/conformal/btcjson --------------------------------------- 78.51% (2338/2978)