Fix listtransactions/gettransaction result structs.

The following changes were made to ListTransactionsResult (which
models the long result format used by listtransactions,
listsinceblock, etc.):

  - Fee made optional (float64 -> *float64 + omitempty)
  - BlockIndex made optional (int64 + omitempty -> *int64 + omitempty)
  - InvolvesWatchOnly added (bool + omitempty)
  - Vout added (uint32)

The following changes were made to GetTransactionDetailsResult (which
models the short result format of listtransactions):

  - InvolvesWatchOnly added (bool + omitempty)
  - Fee added (*float64 + omitempty)
  - Vout added (uint32)

The combination of pointer types and the omitempty struct tag allow
excluding the field from the JSON object, or including it with the
zero value.  This is useful in particular for the fee fields, which
should be included whenever the category is "send" even if the fee is
zero.  Other optional fields which are only added to the result object
with non-zero values (such as includeswatchonly) can be reduced to
simply an omitempty tag without the pointer type.
This commit is contained in:
Josh Rickmar 2015-05-06 11:43:02 -04:00
parent 717a9f25b5
commit 788c316879
3 changed files with 43 additions and 25 deletions

View file

@ -60,6 +60,14 @@ func Uint64(v uint64) *uint64 {
return p return p
} }
// Float64 is a helper routine that allocates a new float64 value to store v and
// returns a pointer to it. This is useful when assigning optional parameters.
func Float64(v float64) *float64 {
p := new(float64)
*p = v
return p
}
// String is a helper routine that allocates a new string value to store v and // String is a helper routine that allocates a new string value to store v and
// returns a pointer to it. This is useful when assigning optional parameters. // returns a pointer to it. This is useful when assigning optional parameters.
func String(v string) *string { func String(v string) *string {

View file

@ -5,12 +5,18 @@
package btcjson package btcjson
// GetTransactionDetailsResult models the details data from the gettransaction command. // GetTransactionDetailsResult models the details data from the gettransaction command.
//
// This models the "short" version of the ListTransactionsResult type, which
// excludes fields common to the transaction. These common fields are instead
// part of the GetTransactionResult.
type GetTransactionDetailsResult struct { type GetTransactionDetailsResult struct {
Account string `json:"account"` Account string `json:"account"`
Address string `json:"address,omitempty"` Address string `json:"address,omitempty"`
Category string `json:"category"` Amount float64 `json:"amount"`
Amount float64 `json:"amount"` Category string `json:"category"`
Fee float64 `json:"fee,omitempty"` InvolvesWatchOnly bool `json:"involveswatchonly,omitempty"`
Fee *float64 `json:"fee,omitempty"`
Vout uint32 `json:"vout"`
} }
// GetTransactionResult models the data from the gettransaction command. // GetTransactionResult models the data from the gettransaction command.
@ -52,22 +58,24 @@ type InfoWalletResult struct {
// ListTransactionsResult models the data from the listtransactions command. // ListTransactionsResult models the data from the listtransactions command.
type ListTransactionsResult struct { type ListTransactionsResult struct {
Account string `json:"account"` Account string `json:"account"`
Address string `json:"address,omitempty"` Address string `json:"address,omitempty"`
Category string `json:"category"` Amount float64 `json:"amount"`
Amount float64 `json:"amount"` BlockHash string `json:"blockhash,omitempty"`
Fee float64 `json:"fee"` BlockIndex *int64 `json:"blockindex,omitempty"`
Confirmations int64 `json:"confirmations"` BlockTime int64 `json:"blocktime,omitempty"`
Generated bool `json:"generated,omitempty"` Category string `json:"category"`
BlockHash string `json:"blockhash,omitempty"` Confirmations int64 `json:"confirmations"`
BlockIndex int64 `json:"blockindex,omitempty"` Fee *float64 `json:"fee,omitempty"`
BlockTime int64 `json:"blocktime,omitempty"` Generated bool `json:"generated,omitempty"`
TxID string `json:"txid"` InvolvesWatchOnly bool `json:"involveswatchonly,omitempty"`
WalletConflicts []string `json:"walletconflicts"` Time int64 `json:"time"`
Time int64 `json:"time"` TimeReceived int64 `json:"timereceived"`
TimeReceived int64 `json:"timereceived"` TxID string `json:"txid"`
Comment string `json:"comment,omitempty"` Vout uint32 `json:"vout"`
OtherAccount string `json:"otheraccount"` WalletConflicts []string `json:"walletconflicts"`
Comment string `json:"comment,omitempty"`
OtherAccount string `json:"otheraccount,omitempty"`
} }
// ListReceivedByAccountResult models the data from the listreceivedbyaccount // ListReceivedByAccountResult models the data from the listreceivedbyaccount

View file

@ -72,7 +72,7 @@ func TestWalletSvrWsNtfns(t *testing.T) {
{ {
name: "newtx", name: "newtx",
newNtfn: func() (interface{}, error) { newNtfn: func() (interface{}, error) {
return btcjson.NewCmd("newtx", "acct", `{"account":"acct","address":"1Address","category":"send","amount":1.5,"fee":0.0001,"confirmations":1,"txid":"456","walletconflicts":[],"time":12345678,"timereceived":12345876,"otheraccount":"otheracct"}`) return btcjson.NewCmd("newtx", "acct", `{"account":"acct","address":"1Address","category":"send","amount":1.5,"fee":0.0001,"confirmations":1,"txid":"456","walletconflicts":[],"time":12345678,"timereceived":12345876,"vout":789,"otheraccount":"otheracct"}`)
}, },
staticNtfn: func() interface{} { staticNtfn: func() interface{} {
result := btcjson.ListTransactionsResult{ result := btcjson.ListTransactionsResult{
@ -80,17 +80,18 @@ func TestWalletSvrWsNtfns(t *testing.T) {
Address: "1Address", Address: "1Address",
Category: "send", Category: "send",
Amount: 1.5, Amount: 1.5,
Fee: 0.0001, Fee: btcjson.Float64(0.0001),
Confirmations: 1, Confirmations: 1,
TxID: "456", TxID: "456",
WalletConflicts: []string{}, WalletConflicts: []string{},
Time: 12345678, Time: 12345678,
TimeReceived: 12345876, TimeReceived: 12345876,
Vout: 789,
OtherAccount: "otheracct", OtherAccount: "otheracct",
} }
return btcjson.NewNewTxNtfn("acct", result) return btcjson.NewNewTxNtfn("acct", result)
}, },
marshalled: `{"jsonrpc":"1.0","method":"newtx","params":["acct",{"account":"acct","address":"1Address","category":"send","amount":1.5,"fee":0.0001,"confirmations":1,"txid":"456","walletconflicts":[],"time":12345678,"timereceived":12345876,"otheraccount":"otheracct"}],"id":null}`, marshalled: `{"jsonrpc":"1.0","method":"newtx","params":["acct",{"account":"acct","address":"1Address","amount":1.5,"category":"send","confirmations":1,"fee":0.0001,"time":12345678,"timereceived":12345876,"txid":"456","vout":789,"walletconflicts":[],"otheraccount":"otheracct"}],"id":null}`,
unmarshalled: &btcjson.NewTxNtfn{ unmarshalled: &btcjson.NewTxNtfn{
Account: "acct", Account: "acct",
Details: btcjson.ListTransactionsResult{ Details: btcjson.ListTransactionsResult{
@ -98,12 +99,13 @@ func TestWalletSvrWsNtfns(t *testing.T) {
Address: "1Address", Address: "1Address",
Category: "send", Category: "send",
Amount: 1.5, Amount: 1.5,
Fee: 0.0001, Fee: btcjson.Float64(0.0001),
Confirmations: 1, Confirmations: 1,
TxID: "456", TxID: "456",
WalletConflicts: []string{}, WalletConflicts: []string{},
Time: 12345678, Time: 12345678,
TimeReceived: 12345876, TimeReceived: 12345876,
Vout: 789,
OtherAccount: "otheracct", OtherAccount: "otheracct",
}, },
}, },