Repository: trustwallet/blockatlas
Pull request #874 (2020-02-14 23:41:59)

use built-in sort for txPage
Use built-in sort package to sort `txPage` object


Patch:
diff --git a/api/handlers.go b/api/handlers.go
index 03ba0e4..322b218 100644
--- a/api/handlers.go
+++ b/api/handlers.go
@@ -9,6 +9,7 @@ import (
 	"github.com/trustwallet/blockatlas/pkg/ginutils"
 	"github.com/trustwallet/blockatlas/pkg/metrics"
 	"net/http"
+	"sort"
 )
 
 // @Summary Get Transactions
@@ -102,7 +103,7 @@ func makeTxRoute(router gin.IRouter, api blockatlas.Platform, path string) {
 		if len(page) > blockatlas.TxPerPage {
 			page = page[0:blockatlas.TxPerPage]
 		}
-		page.Sort()
+		sort.Sort(page)
 		ginutils.RenderSuccess(c, &page)
 	})
 }
diff --git a/pkg/blockatlas/marshal.go b/pkg/blockatlas/marshal.go
index 17fae4e..f2b1183 100644
--- a/pkg/blockatlas/marshal.go
+++ b/pkg/blockatlas/marshal.go
@@ -2,11 +2,9 @@ package blockatlas
 
 import (
 	"encoding/json"
-	"github.com/spf13/cast"
 	"github.com/trustwallet/blockatlas/pkg/errors"
 	"github.com/trustwallet/blockatlas/pkg/numbers"
 	"regexp"
-	"sort"
 	"strings"
 )
 
@@ -114,13 +112,9 @@ func (a *Amount) MarshalJSON() ([]byte, error) {
 }
 
 // Sort sorts the response by date, descending
-func (r *TxPage) Sort() {
-	sort.Slice(*r, func(i, j int) bool {
-		ti := cast.ToUint64((*r)[i].Date)
-		tj := cast.ToUint64((*r)[j].Date)
-		return ti >= tj
-	})
-}
+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) {
@@ -129,7 +123,7 @@ func (r *TxPage) MarshalJSON() ([]byte, error) {
 		Docs   []Tx `json:"docs"`
 		Status bool `json:"status"`
 	}
-	page.Docs = []Tx(*r)
+	page.Docs = *r
 	if page.Docs == nil {
 		page.Docs = make([]Tx, 0)
 	}
diff --git a/platform/bitcoin/transaction.go b/platform/bitcoin/transaction.go
index ee3f509..2388dc1 100644
--- a/platform/bitcoin/transaction.go
+++ b/platform/bitcoin/transaction.go
@@ -7,6 +7,7 @@ import (
 	"github.com/trustwallet/blockatlas/pkg/blockatlas"
 	"github.com/trustwallet/blockatlas/pkg/numbers"
 	"net/http"
+	"sort"
 )
 
 // @Summary Get xpub transactions
@@ -32,7 +33,7 @@ func (p *Platform) handleAddressRoute(c *gin.Context) {
 	address := c.Param("address")
 	txs, ok := p.getTxsByAddress(address)
 	txPage := blockatlas.TxPage(txs)
-	txPage.Sort()
+	sort.Sort(txPage)
 	if ok != nil {
 		c.JSON(http.StatusInternalServerError, ok)
 		return
@@ -44,7 +45,7 @@ func (p *Platform) handleXpubRoute(c *gin.Context) {
 	xpub := c.Param("key")
 	txs, ok := p.getTxsByXPub(xpub)
 	txPage := blockatlas.TxPage(txs)
-	txPage.Sort()
+	sort.Sort(txPage)
 	if ok != nil {
 		c.JSON(http.StatusInternalServerError, ok)
 		return
diff --git a/platform/ethereum/transaction.go b/platform/ethereum/transaction.go
index 5b27f19..882fa83 100644
--- a/platform/ethereum/transaction.go
+++ b/platform/ethereum/transaction.go
@@ -8,6 +8,7 @@ import (
 	"github.com/trustwallet/blockatlas/pkg/logger"
 	"math/big"
 	"net/http"
+	"sort"
 )
 
 func (p *Platform) RegisterRoutes(router gin.IRouter) {
@@ -38,7 +39,7 @@ func (p *Platform) getTransactions(c *gin.Context) {
 	}
 
 	page := blockatlas.TxPage(txs)
-	page.Sort()
+	sort.Sort(page)
 	c.JSON(http.StatusOK, &page)
 }
 


Test patch:
diff --git a/pkg/blockatlas/marshal_test.go b/pkg/blockatlas/marshal_test.go
index 1cfdfb2..68566e1 100644
--- a/pkg/blockatlas/marshal_test.go
+++ b/pkg/blockatlas/marshal_test.go
@@ -3,10 +3,11 @@ package blockatlas
 import (
 	"bytes"
 	"encoding/json"
+	"github.com/stretchr/testify/assert"
+	"github.com/trustwallet/blockatlas/coin"
 	"reflect"
+	"sort"
 	"testing"
-
-	"github.com/trustwallet/blockatlas/coin"
 )
 
 var txJSON = []byte(`{
@@ -76,3 +77,21 @@ func TestTx_MarshalJSON(t *testing.T) {
 	// Compare expected and output JSON
 	bytes.Equal(got, txJSON)
 }
+
+func TestSortTxPage(t *testing.T) {
+	tests := []struct {
+		name string
+		page TxPage
+		want TxPage
+	}{
+		{"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}}},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			sort.Sort(tt.page)
+			assert.Equal(t, tt.want, tt.page)
+		})
+	}
+}