Simplify some code and increase test coverage.
This commit is contained in:
parent
ed7214afda
commit
3b6c9b6d78
3 changed files with 21 additions and 35 deletions
|
@ -31,6 +31,7 @@ var resulttests = []struct {
|
|||
// Generate a fake message to make sure we don't make a command from it.
|
||||
{"anycommand", []byte(`{"result":"test","id":1}`), false, false},
|
||||
{"anycommand", []byte(`{some junk}`), false, false},
|
||||
{"anycommand", []byte(`{"error":null,"result":null,"id":"test"}`), false, true},
|
||||
{"getinfo", []byte(`{"error":null,"result":null,"id":"test"}`), false, true},
|
||||
{"getinfo", []byte(`{"error":null,"result":null}`), false, false},
|
||||
{"getinfo", []byte(`{"error":null,"id":1,"result":[{"a":"b"}]}`), false, false},
|
||||
|
|
49
jsonapi.go
49
jsonapi.go
|
@ -646,55 +646,44 @@ func readResultCmd(cmd string, message []byte) (Reply, error) {
|
|||
|
||||
// If it is a command where we have already worked out the reply,
|
||||
// generate put the results in the proper structure.
|
||||
// We handle the error condition after the switch statement.
|
||||
switch cmd {
|
||||
case "getinfo":
|
||||
var res InfoResult
|
||||
err = json.Unmarshal(objmap["result"], &res)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
if err == nil {
|
||||
result.Result = res
|
||||
}
|
||||
result.Result = res
|
||||
case "getblock":
|
||||
var res BlockResult
|
||||
err = json.Unmarshal(objmap["result"], &res)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
if err == nil {
|
||||
result.Result = res
|
||||
}
|
||||
result.Result = res
|
||||
case "getrawtransaction":
|
||||
var res TxRawResult
|
||||
err = json.Unmarshal(objmap["result"], &res)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
if err == nil {
|
||||
result.Result = res
|
||||
}
|
||||
result.Result = res
|
||||
case "decoderawtransaction":
|
||||
var res TxRawDecodeResult
|
||||
err = json.Unmarshal(objmap["result"], &res)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
if err == nil {
|
||||
result.Result = res
|
||||
}
|
||||
result.Result = res
|
||||
case "getaddressesbyaccount", "getrawmempool":
|
||||
var res []string
|
||||
err = json.Unmarshal(objmap["result"], &res)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
if err == nil {
|
||||
result.Result = res
|
||||
}
|
||||
result.Result = res
|
||||
case "getmininginfo":
|
||||
var res GetMiningInfoResult
|
||||
err = json.Unmarshal(objmap["result"], &res)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
if err == nil {
|
||||
result.Result = res
|
||||
}
|
||||
result.Result = res
|
||||
// For commands that return a single item (or no items), we get it with
|
||||
// the correct concrete type for free (but treat them separately
|
||||
// for clarity).
|
||||
|
@ -702,18 +691,14 @@ func readResultCmd(cmd string, message []byte) (Reply, error) {
|
|||
"getconnetioncount", "getdifficulty", "gethashespersec",
|
||||
"setgenerate", "stop", "settxfee":
|
||||
err = json.Unmarshal(message, &result)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
}
|
||||
// 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)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Error unmarshalling json reply: %v", err)
|
||||
return result, err
|
||||
}
|
||||
// Only want the error field when there is an actual error to report.
|
||||
if jsonErr.Code != 0 {
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
|
||||
github.com/conformal/btcjson/jsonapi.go CreateMessage 100.00% (310/310)
|
||||
github.com/conformal/btcjson/jsonapi.go readResultCmd 100.00% (51/51)
|
||||
github.com/conformal/btcjson/jsonapi.go JSONToAmount 100.00% (15/15)
|
||||
github.com/conformal/btcjson/jsonfxns.go jsonRpcSend 100.00% (7/7)
|
||||
github.com/conformal/btcjson/jsonfxns.go MarshallAndSend 100.00% (7/7)
|
||||
github.com/conformal/btcjson/jsonfxns.go jsonRpcSend 100.00% (7/7)
|
||||
github.com/conformal/btcjson/jsonfxns.go GetRaw 100.00% (6/6)
|
||||
github.com/conformal/btcjson/jsonapi.go jsonWithArgs 100.00% (5/5)
|
||||
github.com/conformal/btcjson/jsonapi.go IsValidIdType 100.00% (3/3)
|
||||
github.com/conformal/btcjson/jsonapi.go readResultCmd 90.91% (60/66)
|
||||
github.com/conformal/btcjson/jsonapi.go RpcCommand 66.67% (18/27)
|
||||
github.com/conformal/btcjson --------------- 96.64% (431/446)
|
||||
github.com/conformal/btcjson --------------- 97.91% (422/431)
|
||||
|
||||
|
|
Loading…
Reference in a new issue