Add support for registering custom reply parsers.
This commit extends the RegisterCustomCmd function to also accept a reply parser which will be invoked for replies to the custom command via ReadResultCmd. This allows replies to custom commands to be returned a concrete structs instead of map[string]interface{}. ok @jcvernaleo, @jrick
This commit is contained in:
parent
c2dd398b1e
commit
31a354e585
3 changed files with 32 additions and 12 deletions
17
jsoncmd.go
17
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
|
||||
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.
|
||||
|
|
|
@ -509,11 +509,22 @@ 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:
|
||||
// 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)
|
||||
return result, err
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue