From 6227b2bcdbe537da8b60734efd92e6e8535a1ea8 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Fri, 19 Apr 2019 11:37:55 -0700 Subject: [PATCH] WTF-400 Remove last of wtf.Config from CryptoLive --- modules/cryptoexchanges/bittrex/settings.go | 2 +- .../cryptolive/price/settings.go | 30 ++++++++---- .../cryptolive/price/widget.go | 16 ++---- .../cryptolive/toplist/settings.go | 49 +++++++++++++++---- .../cryptolive/toplist/widget.go | 17 +++---- 5 files changed, 73 insertions(+), 41 deletions(-) diff --git a/modules/cryptoexchanges/bittrex/settings.go b/modules/cryptoexchanges/bittrex/settings.go index 3ba3f791..78f7aeb8 100644 --- a/modules/cryptoexchanges/bittrex/settings.go +++ b/modules/cryptoexchanges/bittrex/settings.go @@ -57,7 +57,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config) *Settings { market: coercedVal["market"].([]interface{}), } - settings.currencies[key] = currency + settings.summary.currencies[key] = currency } return &settings diff --git a/modules/cryptoexchanges/cryptolive/price/settings.go b/modules/cryptoexchanges/cryptolive/price/settings.go index 1e1c66bf..3f39dca6 100644 --- a/modules/cryptoexchanges/cryptolive/price/settings.go +++ b/modules/cryptoexchanges/cryptolive/price/settings.go @@ -5,7 +5,7 @@ import ( "github.com/wtfutil/wtf/cfg" ) -const configKey = "price" +const configKey = "cryptolive" type colors struct { from struct { @@ -29,23 +29,22 @@ type colors struct { } } +type currency struct { + displayName string + to []interface{} +} + type Settings struct { colors common *cfg.Common - currencies map[string]interface{} - top map[string]interface{} + currencies map[string]*currency } func NewSettingsFromYAML(name string, ymlConfig *config.Config) *Settings { localConfig, _ := ymlConfig.Get("wtf.mods." + configKey) - currencies, _ := localConfig.Map("currencies") - top, _ := localConfig.Map("top") - settings := Settings{ - common: cfg.NewCommonSettingsFromYAML(name, configKey, ymlConfig), - currencies: currencies, - top: top, + common: cfg.NewCommonSettingsFromYAML(name, configKey, ymlConfig), } settings.colors.from.name = localConfig.UString("colors.from.name") @@ -61,5 +60,18 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config) *Settings { settings.colors.top.to.field = localConfig.UString("colors.top.to.field") settings.colors.top.to.value = localConfig.UString("colors.top.to.value") + settings.currencies = make(map[string]*currency) + + for key, val := range localConfig.UMap("currencies") { + coercedVal := val.(map[string]interface{}) + + currency := ¤cy{ + displayName: coercedVal["displayName"].(string), + to: coercedVal["to"].([]interface{}), + } + + settings.currencies[key] = currency + } + return &settings } diff --git a/modules/cryptoexchanges/cryptolive/price/widget.go b/modules/cryptoexchanges/cryptolive/price/widget.go index 42fc5ae9..faada9c1 100644 --- a/modules/cryptoexchanges/cryptolive/price/widget.go +++ b/modules/cryptoexchanges/cryptolive/price/widget.go @@ -6,8 +6,6 @@ import ( "net/http" "sync" "time" - - "github.com/wtfutil/wtf/wtf" ) var baseURL = "https://min-api.cryptocompare.com/data/price" @@ -37,12 +35,10 @@ func NewWidget(settings *Settings) *Widget { func (widget *Widget) setList() { widget.list = &list{} - for currency := range widget.settings.currencies { - displayName, _ := wtf.Config.String("wtf.mods.cryptolive.currencies." + currency + ".displayName") - toList := getToList(currency) - widget.list.addItem(currency, displayName, toList) + for symbol, currency := range widget.settings.currencies { + toList := widget.getToList(symbol) + widget.list.addItem(symbol, currency.displayName, toList) } - } /* -------------------- Exported Functions -------------------- */ @@ -91,12 +87,10 @@ func (widget *Widget) display() { widget.Result = fmt.Sprintf("\n%s", str) } -func getToList(fromName string) []*toCurrency { - toNames, _ := wtf.Config.List("wtf.mods.cryptolive.currencies." + fromName + ".to") - +func (widget *Widget) getToList(symbol string) []*toCurrency { var toList []*toCurrency - for _, to := range toNames { + for _, to := range widget.settings.currencies[symbol].to { toList = append(toList, &toCurrency{ name: to.(string), price: 0, diff --git a/modules/cryptoexchanges/cryptolive/toplist/settings.go b/modules/cryptoexchanges/cryptolive/toplist/settings.go index 97d23eea..8c250c44 100644 --- a/modules/cryptoexchanges/cryptolive/toplist/settings.go +++ b/modules/cryptoexchanges/cryptolive/toplist/settings.go @@ -5,7 +5,7 @@ import ( "github.com/wtfutil/wtf/cfg" ) -const configKey = "toplist" +const configKey = "cryptolive" type colors struct { from struct { @@ -29,23 +29,24 @@ type colors struct { } } +type currency struct { + displayName string + limit int + to []interface{} +} + type Settings struct { colors common *cfg.Common - currencies map[string]interface{} - top map[string]interface{} + currencies map[string]*currency + top map[string]*currency } func NewSettingsFromYAML(name string, ymlConfig *config.Config) *Settings { localConfig, _ := ymlConfig.Get("wtf.mods." + configKey) - currencies, _ := localConfig.Map("currencies") - top, _ := localConfig.Map("top") - settings := Settings{ - common: cfg.NewCommonSettingsFromYAML(name, configKey, ymlConfig), - currencies: currencies, - top: top, + common: cfg.NewCommonSettingsFromYAML(name, configKey, ymlConfig), } settings.colors.from.name = localConfig.UString("colors.from.name") @@ -61,5 +62,35 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config) *Settings { settings.colors.top.to.field = localConfig.UString("colors.top.to.field") settings.colors.top.to.value = localConfig.UString("colors.top.to.value") + settings.currencies = make(map[string]*currency) + + for key, val := range localConfig.UMap("currencies") { + coercedVal := val.(map[string]interface{}) + + limit, _ := coercedVal["limit"].(int) + + currency := ¤cy{ + displayName: coercedVal["displayName"].(string), + limit: limit, + to: coercedVal["to"].([]interface{}), + } + + settings.currencies[key] = currency + } + + for key, val := range localConfig.UMap("top") { + coercedVal := val.(map[string]interface{}) + + limit, _ := coercedVal["limit"].(int) + + currency := ¤cy{ + displayName: coercedVal["displayName"].(string), + limit: limit, + to: coercedVal["to"].([]interface{}), + } + + settings.currencies[key] = currency + } + return &settings } diff --git a/modules/cryptoexchanges/cryptolive/toplist/widget.go b/modules/cryptoexchanges/cryptolive/toplist/widget.go index b06a3b24..58eafd3f 100644 --- a/modules/cryptoexchanges/cryptolive/toplist/widget.go +++ b/modules/cryptoexchanges/cryptolive/toplist/widget.go @@ -7,8 +7,6 @@ import ( "os" "sync" "time" - - "github.com/wtfutil/wtf/wtf" ) var baseURL = "https://min-api.cryptocompare.com/data/top/exchanges" @@ -36,19 +34,16 @@ func NewWidget(settings *Settings) *Widget { } func (widget *Widget) setList() { - for fromCurrency := range widget.settings.top { - displayName := wtf.Config.UString("wtf.mods.cryptolive.top."+fromCurrency+".displayName", "") - limit := wtf.Config.UInt("wtf.mods.cryptolive.top."+fromCurrency+".limit", 1) - widget.list.addItem(fromCurrency, displayName, limit, makeToList(fromCurrency, limit)) + for symbol, currency := range widget.settings.top { + toList := widget.makeToList(symbol, currency.limit) + widget.list.addItem(symbol, currency.displayName, currency.limit, toList) } } -func makeToList(fCurrencyName string, limit int) (list []*tCurrency) { - toList, _ := wtf.Config.List("wtf.mods.cryptolive.top." + fCurrencyName + ".to") - - for _, toCurrency := range toList { +func (widget *Widget) makeToList(symbol string, limit int) (list []*tCurrency) { + for _, to := range widget.settings.top[symbol].to { list = append(list, &tCurrency{ - name: toCurrency.(string), + name: to.(string), info: make([]tInfo, limit), }) }