From e188845dc13b6a5241f59e6f025913f1282eb224 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Sat, 23 Jun 2018 01:13:11 -0700 Subject: [PATCH] store favorites by name instead of by symbol --- cointop/cointop.go | 37 +++++++++++++++------ cointop/config.go | 17 ++++++++-- cointop/favorites.go | 6 ++-- cointop/refresh.go | 1 - cointop/table.go | 13 +++++++- pkg/api/impl/coinmarketcap/coinmarketcap.go | 4 +-- 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/cointop/cointop.go b/cointop/cointop.go index dd961b1..ef97234 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -42,7 +42,6 @@ type Cointop struct { api api.Interface allcoins []*coin coins []*coin - allcoinssymbolmap map[string]*coin allcoinsslugmap map[string]*coin page int perpage int @@ -56,6 +55,8 @@ type Cointop struct { searchfield *gocui.View searchfieldviewname string searchfieldvisible bool + // DEPRECATED: favorites by 'symbol' is deprecated because of collisions. + favoritesbysymbol map[string]bool favorites map[string]bool filterByFavorites bool savemux sync.Mutex @@ -77,16 +78,18 @@ func New() *Cointop { debug = true } ct := &Cointop{ - api: api.NewCMC(), - refreshticker: time.NewTicker(1 * time.Minute), - sortby: "rank", - sortdesc: false, - page: 0, - perpage: 100, - forcerefresh: make(chan bool), - maxtablewidth: 175, - actionsmap: actionsMap(), - shortcutkeys: defaultShortcuts(), + api: api.NewCMC(), + refreshticker: time.NewTicker(1 * time.Minute), + sortby: "rank", + sortdesc: false, + page: 0, + perpage: 100, + forcerefresh: make(chan bool), + maxtablewidth: 175, + actionsmap: actionsMap(), + shortcutkeys: defaultShortcuts(), + // DEPRECATED: favorites by 'symbol' is deprecated because of collisions. + favoritesbysymbol: map[string]bool{}, favorites: map[string]bool{}, cache: cache.New(1*time.Minute, 2*time.Minute), debug: debug, @@ -150,6 +153,18 @@ func New() *Cointop { fcache.Get(coinscachekey, &allcoinsslugmap) ct.cache.Set(coinscachekey, allcoinsslugmap, 10*time.Second) + // DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility. + // Here we're doing a lookup based on symbol and setting the favorite to the coin name instead of coin symbol. + for i := range allcoinsslugmap { + coin := allcoinsslugmap[i] + for k := range ct.favoritesbysymbol { + if coin.Symbol == k { + ct.favorites[coin.Name] = true + delete(ct.favoritesbysymbol, k) + } + } + } + var globaldata []float64 chartcachekey := strings.ToLower(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.selectedchartrange, " ", "", -1))) fcache.Get(chartcachekey, &globaldata) diff --git a/cointop/config.go b/cointop/config.go index 33eabb7..6b2d5cb 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -48,11 +48,19 @@ func (ct *Cointop) setupConfig() error { func (ct *Cointop) loadFavoritesFromConfig() error { for k, arr := range ct.config.Favorites { + // DEPRECATED: favorites by 'symbol' is deprecated because of collisions. if k == "symbols" { for _, ifc := range arr { v, ok := ifc.(string) if ok { - ct.favorites[strings.ToUpper(v)] = true + ct.favoritesbysymbol[strings.ToUpper(v)] = true + } + } + } else if k == "names" { + for _, ifc := range arr { + v, ok := ifc.(string) + if ok { + ct.favorites[v] = true } } } @@ -134,12 +142,15 @@ func (ct *Cointop) configToToml() ([]byte, error) { var favorites []interface{} for k, ok := range ct.favorites { if ok { - var i interface{} = strings.ToUpper(k) + var i interface{} = k favorites = append(favorites, i) } } + var favoritesbysymbol []interface{} favoritesIfcs := map[string][]interface{}{ - "symbols": favorites, + // DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility. + "symbols": favoritesbysymbol, + "names": favorites, } var currencyIfc interface{} = ct.currencyconversion diff --git a/cointop/favorites.go b/cointop/favorites.go index ac5f847..e0a5266 100644 --- a/cointop/favorites.go +++ b/cointop/favorites.go @@ -5,12 +5,12 @@ func (ct *Cointop) toggleFavorite() error { if coin == nil { return nil } - _, ok := ct.favorites[coin.Symbol] + _, ok := ct.favorites[coin.Name] if ok { - delete(ct.favorites, coin.Symbol) + delete(ct.favorites, coin.Name) coin.Favorite = false } else { - ct.favorites[coin.Symbol] = true + ct.favorites[coin.Name] = true coin.Favorite = true } ct.updateTable() diff --git a/cointop/refresh.go b/cointop/refresh.go index 82a4cee..4403d40 100644 --- a/cointop/refresh.go +++ b/cointop/refresh.go @@ -15,7 +15,6 @@ func (ct *Cointop) refreshAll() error { defer ct.refreshmux.Unlock() ct.setRefreshStatus() ct.cache.Delete("allcoinsslugmap") - ct.cache.Delete("allcoinssymbolmap") ct.cache.Delete("market") ct.updateCoins() ct.updateTable() diff --git a/cointop/table.go b/cointop/table.go index 3ff1d53..9be2178 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -105,7 +105,7 @@ func (ct *Cointop) updateTable() error { for i := range ct.allcoinsslugmap { v := ct.allcoinsslugmap[i] - if ct.favorites[v.Symbol] { + if ct.favorites[v.Name] { v.Favorite = true } } @@ -199,3 +199,14 @@ func (ct *Cointop) allCoins() []*coin { return ct.allcoins } + +func (ct *Cointop) coinBySymbol(symbol string) *coin { + for i := range ct.allcoins { + coin := ct.allcoins[i] + if coin.Symbol == symbol { + return coin + } + } + + return nil +} diff --git a/pkg/api/impl/coinmarketcap/coinmarketcap.go b/pkg/api/impl/coinmarketcap/coinmarketcap.go index 50441c3..5651119 100644 --- a/pkg/api/impl/coinmarketcap/coinmarketcap.go +++ b/pkg/api/impl/coinmarketcap/coinmarketcap.go @@ -46,7 +46,7 @@ func getLimitedCoinData(convert string, offset int) (map[string]apitypes.Coin, e return ret, err } for _, v := range coins { - ret[v.Symbol] = apitypes.Coin{ + ret[v.Name] = apitypes.Coin{ ID: v.Slug, Name: v.Name, Symbol: v.Symbol, @@ -79,7 +79,7 @@ func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, erro pricestr = fmt.Sprintf("%.5f", price) } price, _ = strconv.ParseFloat(pricestr, 64) - ret[v.Symbol] = apitypes.Coin{ + ret[v.Name] = apitypes.Coin{ ID: strings.ToLower(v.Name), Name: v.Name, Symbol: v.Symbol,