Simplify some code and increase test coverage.

This commit is contained in:
John C. Vernaleo 2013-07-16 16:01:51 -04:00
parent ed7214afda
commit 3b6c9b6d78
3 changed files with 21 additions and 35 deletions

View file

@ -31,6 +31,7 @@ var resulttests = []struct {
// Generate a fake message to make sure we don't make a command from it. // 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(`{"result":"test","id":1}`), false, false},
{"anycommand", []byte(`{some junk}`), 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,"id":"test"}`), false, true},
{"getinfo", []byte(`{"error":null,"result":null}`), false, false}, {"getinfo", []byte(`{"error":null,"result":null}`), false, false},
{"getinfo", []byte(`{"error":null,"id":1,"result":[{"a":"b"}]}`), false, false}, {"getinfo", []byte(`{"error":null,"id":1,"result":[{"a":"b"}]}`), false, false},

View file

@ -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, // If it is a command where we have already worked out the reply,
// generate put the results in the proper structure. // generate put the results in the proper structure.
// We handle the error condition after the switch statement.
switch cmd { switch cmd {
case "getinfo": case "getinfo":
var res InfoResult var res InfoResult
err = json.Unmarshal(objmap["result"], &res) err = json.Unmarshal(objmap["result"], &res)
if err != nil { if err == nil {
err = fmt.Errorf("Error unmarshalling json reply: %v", err) result.Result = res
return result, err
} }
result.Result = res
case "getblock": case "getblock":
var res BlockResult var res BlockResult
err = json.Unmarshal(objmap["result"], &res) err = json.Unmarshal(objmap["result"], &res)
if err != nil { if err == nil {
err = fmt.Errorf("Error unmarshalling json reply: %v", err) result.Result = res
return result, err
} }
result.Result = res
case "getrawtransaction": case "getrawtransaction":
var res TxRawResult var res TxRawResult
err = json.Unmarshal(objmap["result"], &res) err = json.Unmarshal(objmap["result"], &res)
if err != nil { if err == nil {
err = fmt.Errorf("Error unmarshalling json reply: %v", err) result.Result = res
return result, err
} }
result.Result = res
case "decoderawtransaction": case "decoderawtransaction":
var res TxRawDecodeResult var res TxRawDecodeResult
err = json.Unmarshal(objmap["result"], &res) err = json.Unmarshal(objmap["result"], &res)
if err != nil { if err == nil {
err = fmt.Errorf("Error unmarshalling json reply: %v", err) result.Result = res
return result, err
} }
result.Result = res
case "getaddressesbyaccount", "getrawmempool": case "getaddressesbyaccount", "getrawmempool":
var res []string var res []string
err = json.Unmarshal(objmap["result"], &res) err = json.Unmarshal(objmap["result"], &res)
if err != nil { if err == nil {
err = fmt.Errorf("Error unmarshalling json reply: %v", err) result.Result = res
return result, err
} }
result.Result = res
case "getmininginfo": case "getmininginfo":
var res GetMiningInfoResult var res GetMiningInfoResult
err = json.Unmarshal(objmap["result"], &res) err = json.Unmarshal(objmap["result"], &res)
if err != nil { if err == nil {
err = fmt.Errorf("Error unmarshalling json reply: %v", err) result.Result = res
return result, err
} }
result.Result = res
// For commands that return a single item (or no items), we get it with // For commands that return a single item (or no items), we get it with
// the correct concrete type for free (but treat them separately // the correct concrete type for free (but treat them separately
// for clarity). // for clarity).
@ -702,18 +691,14 @@ func readResultCmd(cmd string, message []byte) (Reply, error) {
"getconnetioncount", "getdifficulty", "gethashespersec", "getconnetioncount", "getdifficulty", "gethashespersec",
"setgenerate", "stop", "settxfee": "setgenerate", "stop", "settxfee":
err = json.Unmarshal(message, &result) 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 // For anything else put it in an interface. All the data is still
// there, just a little less convenient to deal with. // there, just a little less convenient to deal with.
default: default:
err = json.Unmarshal(message, &result) err = json.Unmarshal(message, &result)
if err != nil { }
err = fmt.Errorf("Error unmarshalling json reply: %v", err) if err != nil {
return result, err 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. // Only want the error field when there is an actual error to report.
if jsonErr.Code != 0 { if jsonErr.Code != 0 {

View file

@ -1,12 +1,12 @@
github.com/conformal/btcjson/jsonapi.go CreateMessage 100.00% (310/310) 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/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 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/jsonfxns.go GetRaw 100.00% (6/6)
github.com/conformal/btcjson/jsonapi.go jsonWithArgs 100.00% (5/5) 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 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/jsonapi.go RpcCommand 66.67% (18/27)
github.com/conformal/btcjson --------------- 96.64% (431/446) github.com/conformal/btcjson --------------- 97.91% (422/431)