Skip to content

Commit

Permalink
Refactor TxPage (#49)
Browse files Browse the repository at this point in the history
* make TxPage explicit struct
* add empty page
* new from Txs
  • Loading branch information
hewigovens authored Feb 19, 2021
1 parent 76482c6 commit 6fb9ad9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
22 changes: 3 additions & 19 deletions types/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,9 @@ func (a *Amount) MarshalJSON() ([]byte, error) {
}

// Sort sorts the response by date, descending
func (txs TxPage) Len() int { return len(txs) }
func (txs TxPage) Less(i, j int) bool { return txs[i].Date > txs[j].Date }
func (txs TxPage) Swap(i, j int) { txs[i], txs[j] = txs[j], txs[i] }

// MarshalJSON returns a wrapped list of transactions in JSON
func (r *TxPage) MarshalJSON() ([]byte, error) {
var page struct {
Total int `json:"total"`
Docs []Tx `json:"docs"`
Status bool `json:"status"`
}
page.Docs = *r
if page.Docs == nil {
page.Docs = make([]Tx, 0)
}
page.Total = len(page.Docs)
page.Status = true
return json.Marshal(page)
}
func (txs Txs) Len() int { return len(txs) }
func (txs Txs) Less(i, j int) bool { return txs[i].Date > txs[j].Date }
func (txs Txs) Swap(i, j int) { txs[i], txs[j] = txs[j], txs[i] }

// MarshalJSON returns a wrapped list of collections in JSON
func (r CollectionPage) MarshalJSON() ([]byte, error) {
Expand Down
10 changes: 5 additions & 5 deletions types/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func TestTx_MarshalJSON(t *testing.T) {
func TestSortTxPage(t *testing.T) {
tests := []struct {
name string
page TxPage
want TxPage
page Txs
want Txs
}{
{"test sort 1", TxPage{{Date: 5}, {Date: 2}, {Date: 1}, {Date: 4}, {Date: 3}}, TxPage{{Date: 5}, {Date: 4}, {Date: 3}, {Date: 2}, {Date: 1}}},
{"test sort 2", TxPage{{Date: 100}, {Date: 2}, {Date: 33}, {Date: 409}}, TxPage{{Date: 409}, {Date: 100}, {Date: 33}, {Date: 2}}},
{"test sort 3", TxPage{{Date: 100}, {Date: 2}, {Date: 100}}, TxPage{{Date: 100}, {Date: 100}, {Date: 2}}},
{"test sort 1", Txs{{Date: 5}, {Date: 2}, {Date: 1}, {Date: 4}, {Date: 3}}, Txs{{Date: 5}, {Date: 4}, {Date: 3}, {Date: 2}, {Date: 1}}},
{"test sort 2", Txs{{Date: 100}, {Date: 2}, {Date: 33}, {Date: 409}}, Txs{{Date: 409}, {Date: 100}, {Date: 33}, {Date: 2}}},
{"test sort 3", Txs{{Date: 100}, {Date: 2}, {Date: 100}}, Txs{{Date: 100}, {Date: 100}, {Date: 2}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
31 changes: 24 additions & 7 deletions types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ type (
Txs []Tx `json:"txs"`
}

// TxPage is a page of transactions
TxPage []Tx

TxPage struct {
Total int `json:"total"`
Docs []Tx `json:"docs"`
Status bool `json:"status"`
}
// Amount is a positive decimal integer string.
// It is written in the smallest possible unit (e.g. Wei, Satoshis)
Amount string
Expand Down Expand Up @@ -199,6 +201,21 @@ type (
Txs []Tx
)

var (
EmptyTxPage = TxPage{Total: 0, Docs: Txs{}, Status: true}
)

func NewTxPage(txs Txs) TxPage {
if txs == nil {
txs = Txs{}
}
return TxPage{
Total: len(txs),
Docs: txs,
Status: true,
}
}

func (t Token) AssetId() string {
return asset.BuildID(t.Coin, t.TokenID)
}
Expand All @@ -215,8 +232,8 @@ func (txs Txs) FilterUniqueID() Txs {
return list
}

func (txs TxPage) FilterTransactionsByMemo() TxPage {
result := make(TxPage, 0)
func (txs Txs) FilterTransactionsByMemo() Txs {
result := make(Txs, 0)
for _, tx := range txs {
if !AllowMemo(tx.Memo) {
tx.Memo = ""
Expand Down Expand Up @@ -244,8 +261,8 @@ func AllowMemo(memo string) bool {
return err == nil
}

func (txs TxPage) FilterTransactionsByToken(token string) TxPage {
result := make(TxPage, 0)
func (txs Txs) FilterTransactionsByToken(token string) Txs {
result := make(Txs, 0)
for _, tx := range txs {
switch tx.Meta.(type) {
case TokenTransfer:
Expand Down
16 changes: 8 additions & 8 deletions types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ var (
)

func Test_filterTransactionsByToken(t *testing.T) {
var p TxPage
var p Txs
assert.Nil(t, json.Unmarshal([]byte(beforeTransactionsToken), &p))
result := p.FilterTransactionsByToken("BUSD-BD1")
rawResult, err := json.Marshal(result)
Expand Down Expand Up @@ -701,33 +701,33 @@ func Test_AllowMemo(t *testing.T) {
}
}

func TestTxPage_FilterTransactionsByMemo(t *testing.T) {
func TestTxs_FilterTransactionsByMemo(t *testing.T) {
tests := []struct {
name string
txs TxPage
want TxPage
txs Txs
want Txs
}{
{
name: "Allow memo",
txs: TxPage{
txs: Txs{
{
Memo: "123",
},
},
want: TxPage{
want: Txs{
{
Memo: "123",
},
},
},
{
name: "Disallow memo",
txs: TxPage{
txs: Txs{
{
Memo: "test",
},
},
want: TxPage{
want: Txs{
{
Memo: "",
},
Expand Down

0 comments on commit 6fb9ad9

Please sign in to comment.