From 6fb9ad928aad7f6b6ede389e26e9008222a640b1 Mon Sep 17 00:00:00 2001 From: hewigovens <360470+hewigovens@users.noreply.github.com> Date: Fri, 19 Feb 2021 21:16:39 +0900 Subject: [PATCH] Refactor TxPage (#49) * make TxPage explicit struct * add empty page * new from Txs --- types/marshal.go | 22 +++------------------- types/marshal_test.go | 10 +++++----- types/tx.go | 31 ++++++++++++++++++++++++------- types/tx_test.go | 16 ++++++++-------- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/types/marshal.go b/types/marshal.go index d4673a0..344b46d 100644 --- a/types/marshal.go +++ b/types/marshal.go @@ -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) { diff --git a/types/marshal_test.go b/types/marshal_test.go index 8c71a00..a25cc2b 100644 --- a/types/marshal_test.go +++ b/types/marshal_test.go @@ -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) { diff --git a/types/tx.go b/types/tx.go index 36b9c9e..067f273 100644 --- a/types/tx.go +++ b/types/tx.go @@ -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 @@ -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) } @@ -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 = "" @@ -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: diff --git a/types/tx_test.go b/types/tx_test.go index 28d33bc..4eb0765 100644 --- a/types/tx_test.go +++ b/types/tx_test.go @@ -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) @@ -701,20 +701,20 @@ 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", }, @@ -722,12 +722,12 @@ func TestTxPage_FilterTransactionsByMemo(t *testing.T) { }, { name: "Disallow memo", - txs: TxPage{ + txs: Txs{ { Memo: "test", }, }, - want: TxPage{ + want: Txs{ { Memo: "", },