From 3b6c9b6d78452439254d0a71c733212b5858b427 Mon Sep 17 00:00:00 2001 From: "John C. Vernaleo" Date: Tue, 16 Jul 2013 16:01:51 -0400 Subject: [PATCH] Simplify some code and increase test coverage. --- internal_test.go | 1 + jsonapi.go | 49 ++++++++++++++++------------------------------- test_coverage.txt | 6 +++--- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/internal_test.go b/internal_test.go index 01afd6a9..fc8dbc1e 100644 --- a/internal_test.go +++ b/internal_test.go @@ -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}, diff --git a/jsonapi.go b/jsonapi.go index 1a7d5b03..ec66454c 100644 --- a/jsonapi.go +++ b/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 { diff --git a/test_coverage.txt b/test_coverage.txt index ca6c2517..d230675f 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -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)