Accept nil string for account name

This commit is contained in:
Javed Khan 2014-11-18 18:22:10 +05:30
parent b9c8ec92d6
commit 62eb2f2198
2 changed files with 39 additions and 35 deletions

View file

@ -1712,7 +1712,7 @@ func (cmd *GetAddressesByAccountCmd) UnmarshalJSON(b []byte) error {
// unmarshaling of getbalance JSON RPC commands. // unmarshaling of getbalance JSON RPC commands.
type GetBalanceCmd struct { type GetBalanceCmd struct {
id interface{} id interface{}
Account string Account *string
MinConf int MinConf int
} }
@ -1722,7 +1722,7 @@ var _ Cmd = &GetBalanceCmd{}
// NewGetBalanceCmd creates a new GetBalanceCmd. Optionally a string for account // NewGetBalanceCmd creates a new GetBalanceCmd. Optionally a string for account
// and an int for minconf may be provided as arguments. // and an int for minconf may be provided as arguments.
func NewGetBalanceCmd(id interface{}, optArgs ...interface{}) (*GetBalanceCmd, error) { func NewGetBalanceCmd(id interface{}, optArgs ...interface{}) (*GetBalanceCmd, error) {
var account string var account *string
var minconf = 1 var minconf = 1
if len(optArgs) > 2 { if len(optArgs) > 2 {
@ -1733,7 +1733,7 @@ func NewGetBalanceCmd(id interface{}, optArgs ...interface{}) (*GetBalanceCmd, e
if !ok { if !ok {
return nil, errors.New("first optional argument account is not a string") return nil, errors.New("first optional argument account is not a string")
} }
account = a account = &a
} }
if len(optArgs) > 1 { if len(optArgs) > 1 {
@ -1764,7 +1764,7 @@ func (cmd *GetBalanceCmd) Method() string {
// MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface. // MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface.
func (cmd *GetBalanceCmd) MarshalJSON() ([]byte, error) { func (cmd *GetBalanceCmd) MarshalJSON() ([]byte, error) {
params := make([]interface{}, 0, 2) params := make([]interface{}, 0, 2)
if cmd.Account != "" || cmd.MinConf != 1 { if cmd.Account != nil {
params = append(params, cmd.Account) params = append(params, cmd.Account)
} }
if cmd.MinConf != 1 { if cmd.MinConf != 1 {
@ -4709,7 +4709,7 @@ func (cmd *ListSinceBlockCmd) UnmarshalJSON(b []byte) error {
// unmarshaling of listtransactions JSON RPC commands. // unmarshaling of listtransactions JSON RPC commands.
type ListTransactionsCmd struct { type ListTransactionsCmd struct {
id interface{} id interface{}
Account string Account *string
Count int Count int
From int From int
} }
@ -4720,7 +4720,7 @@ var _ Cmd = &ListTransactionsCmd{}
// NewListTransactionsCmd creates a new ListTransactionsCmd. Optionally a // NewListTransactionsCmd creates a new ListTransactionsCmd. Optionally a
// pointer to a TemplateRequest may be provided. // pointer to a TemplateRequest may be provided.
func NewListTransactionsCmd(id interface{}, optArgs ...interface{}) (*ListTransactionsCmd, error) { func NewListTransactionsCmd(id interface{}, optArgs ...interface{}) (*ListTransactionsCmd, error) {
account := "" var account *string
count := 10 count := 10
from := 0 from := 0
@ -4732,7 +4732,7 @@ func NewListTransactionsCmd(id interface{}, optArgs ...interface{}) (*ListTransa
if !ok { if !ok {
return nil, errors.New("first optional argument account is not a string") return nil, errors.New("first optional argument account is not a string")
} }
account = ac account = &ac
} }
if len(optArgs) > 1 { if len(optArgs) > 1 {
cnt, ok := optArgs[1].(int) cnt, ok := optArgs[1].(int)
@ -4771,7 +4771,7 @@ func (cmd *ListTransactionsCmd) Method() string {
// MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface. // MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface.
func (cmd *ListTransactionsCmd) MarshalJSON() ([]byte, error) { func (cmd *ListTransactionsCmd) MarshalJSON() ([]byte, error) {
params := make([]interface{}, 0, 3) params := make([]interface{}, 0, 3)
if cmd.Account != "" || cmd.Count != 10 || cmd.From != 0 { if cmd.Account != nil {
params = append(params, cmd.Account) params = append(params, cmd.Account)
} }
if cmd.Count != 10 || cmd.From != 0 { if cmd.Count != 10 || cmd.From != 0 {

View file

@ -16,6 +16,9 @@ import (
var testID = float64(1) var testID = float64(1)
var defaultAccount = ""
var testAccount = "account"
var jsoncmdtests = []struct { var jsoncmdtests = []struct {
name string name string
cmd string cmd string
@ -33,7 +36,7 @@ var jsoncmdtests = []struct {
id: testID, id: testID,
NRequired: 1, NRequired: 1,
Keys: []string{"foo", "bar"}, Keys: []string{"foo", "bar"},
Account: "", Account: defaultAccount,
}, },
}, },
{ {
@ -247,11 +250,11 @@ var jsoncmdtests = []struct {
cmd: "getaccountaddress", cmd: "getaccountaddress",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewGetAccountAddressCmd(testID, return NewGetAccountAddressCmd(testID,
"account") testAccount)
}, },
result: &GetAccountAddressCmd{ result: &GetAccountAddressCmd{
id: testID, id: testID,
Account: "account", Account: testAccount,
}, },
}, },
{ {
@ -294,11 +297,11 @@ var jsoncmdtests = []struct {
cmd: "getaddressesbyaccount", cmd: "getaddressesbyaccount",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewGetAddressesByAccountCmd(testID, return NewGetAddressesByAccountCmd(testID,
"account") testAccount)
}, },
result: &GetAddressesByAccountCmd{ result: &GetAddressesByAccountCmd{
id: testID, id: testID,
Account: "account", Account: testAccount,
}, },
}, },
{ {
@ -316,11 +319,11 @@ var jsoncmdtests = []struct {
name: "basic + account", name: "basic + account",
cmd: "getbalance", cmd: "getbalance",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewGetBalanceCmd(testID, "account") return NewGetBalanceCmd(testID, testAccount)
}, },
result: &GetBalanceCmd{ result: &GetBalanceCmd{
id: testID, id: testID,
Account: "account", Account: &testAccount,
MinConf: 1, // the default MinConf: 1, // the default
}, },
}, },
@ -328,10 +331,11 @@ var jsoncmdtests = []struct {
name: "basic + minconf", name: "basic + minconf",
cmd: "getbalance", cmd: "getbalance",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewGetBalanceCmd(testID, "", 2) return NewGetBalanceCmd(testID, defaultAccount, 2)
}, },
result: &GetBalanceCmd{ result: &GetBalanceCmd{
id: testID, id: testID,
Account: &defaultAccount,
MinConf: 2, MinConf: 2,
}, },
}, },
@ -339,11 +343,11 @@ var jsoncmdtests = []struct {
name: "basic + account + minconf", name: "basic + account + minconf",
cmd: "getbalance", cmd: "getbalance",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewGetBalanceCmd(testID, "account", 2) return NewGetBalanceCmd(testID, testAccount, 2)
}, },
result: &GetBalanceCmd{ result: &GetBalanceCmd{
id: testID, id: testID,
Account: "account", Account: &testAccount,
MinConf: 2, MinConf: 2,
}, },
}, },
@ -570,11 +574,11 @@ var jsoncmdtests = []struct {
name: "basic", name: "basic",
cmd: "getnewaddress", cmd: "getnewaddress",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewGetNewAddressCmd(testID, "account") return NewGetNewAddressCmd(testID, testAccount)
}, },
result: &GetNewAddressCmd{ result: &GetNewAddressCmd{
id: testID, id: testID,
Account: "account", Account: testAccount,
}, },
}, },
{ {
@ -1050,7 +1054,7 @@ var jsoncmdtests = []struct {
}, },
result: &ListTransactionsCmd{ result: &ListTransactionsCmd{
id: testID, id: testID,
Account: "", Account: nil,
Count: 10, Count: 10,
From: 0, From: 0,
}, },
@ -1059,11 +1063,11 @@ var jsoncmdtests = []struct {
name: "+ 1 optarg", name: "+ 1 optarg",
cmd: "listtransactions", cmd: "listtransactions",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewListTransactionsCmd(testID, "abcde") return NewListTransactionsCmd(testID, testAccount)
}, },
result: &ListTransactionsCmd{ result: &ListTransactionsCmd{
id: testID, id: testID,
Account: "abcde", Account: &testAccount,
Count: 10, Count: 10,
From: 0, From: 0,
}, },
@ -1072,11 +1076,11 @@ var jsoncmdtests = []struct {
name: "+ 2 optargs", name: "+ 2 optargs",
cmd: "listtransactions", cmd: "listtransactions",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewListTransactionsCmd(testID, "abcde", 123) return NewListTransactionsCmd(testID, testAccount, 123)
}, },
result: &ListTransactionsCmd{ result: &ListTransactionsCmd{
id: testID, id: testID,
Account: "abcde", Account: &testAccount,
Count: 123, Count: 123,
From: 0, From: 0,
}, },
@ -1085,11 +1089,11 @@ var jsoncmdtests = []struct {
name: "+ 3 optargs", name: "+ 3 optargs",
cmd: "listtransactions", cmd: "listtransactions",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewListTransactionsCmd(testID, "abcde", 123, 456) return NewListTransactionsCmd(testID, testAccount, 123, 456)
}, },
result: &ListTransactionsCmd{ result: &ListTransactionsCmd{
id: testID, id: testID,
Account: "abcde", Account: &testAccount,
Count: 123, Count: 123,
From: 456, From: 456,
}, },
@ -1224,14 +1228,14 @@ var jsoncmdtests = []struct {
cmd: "sendfrom", cmd: "sendfrom",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewSendFromCmd(testID, return NewSendFromCmd(testID,
"account", testAccount,
"address", "address",
12, 12,
1) 1)
}, },
result: &SendFromCmd{ result: &SendFromCmd{
id: testID, id: testID,
FromAccount: "account", FromAccount: testAccount,
ToAddress: "address", ToAddress: "address",
Amount: 12, Amount: 12,
MinConf: 1, // the default MinConf: 1, // the default
@ -1242,7 +1246,7 @@ var jsoncmdtests = []struct {
cmd: "sendfrom", cmd: "sendfrom",
f: func() (Cmd, error) { f: func() (Cmd, error) {
return NewSendFromCmd(testID, return NewSendFromCmd(testID,
"account", testAccount,
"address", "address",
12, 12,
1, 1,
@ -1251,7 +1255,7 @@ var jsoncmdtests = []struct {
}, },
result: &SendFromCmd{ result: &SendFromCmd{
id: testID, id: testID,
FromAccount: "account", FromAccount: testAccount,
ToAddress: "address", ToAddress: "address",
Amount: 12, Amount: 12,
MinConf: 1, // the default MinConf: 1, // the default
@ -1269,12 +1273,12 @@ var jsoncmdtests = []struct {
"address C": 3000, "address C": 3000,
} }
return NewSendManyCmd(testID, return NewSendManyCmd(testID,
"account", testAccount,
pairs) pairs)
}, },
result: &SendManyCmd{ result: &SendManyCmd{
id: testID, id: testID,
FromAccount: "account", FromAccount: testAccount,
Amounts: map[string]int64{ Amounts: map[string]int64{
"address A": 1000, "address A": 1000,
"address B": 2000, "address B": 2000,
@ -1293,14 +1297,14 @@ var jsoncmdtests = []struct {
"address C": 3000, "address C": 3000,
} }
return NewSendManyCmd(testID, return NewSendManyCmd(testID,
"account", testAccount,
pairs, pairs,
10, 10,
"comment") "comment")
}, },
result: &SendManyCmd{ result: &SendManyCmd{
id: testID, id: testID,
FromAccount: "account", FromAccount: testAccount,
Amounts: map[string]int64{ Amounts: map[string]int64{
"address A": 1000, "address A": 1000,
"address B": 2000, "address B": 2000,