1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/exchangerates/settings.go
Toon Schoenmakers c8a14e7685 Keep an initial order of the currencies so they won't be in a different order after refreshing (#752)
This purely happend due to maps not keeping the order items are in. This way
it at least won't change throughout the runtime of wtfutil. Ideally it would
use and keep the order as specified by the user in the configuration but
right now there is no way to enforce this.
2019-11-16 19:47:52 -08:00

50 lines
1.2 KiB
Go

// Package exchangerates
package exchangerates
import (
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
const (
defaultFocusable = false
defaultTitle = "Exchange rates"
)
// Settings defines the configuration properties for this module
type Settings struct {
common *cfg.Common
rates map[string][]string `help:"Defines what currency rates we want to know about`
order []string
}
// NewSettingsFromYAML creates a new settings instance from a YAML config block
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
rates: map[string][]string{},
order: []string{},
}
raw := ymlConfig.UMap("rates", map[string]interface{}{})
for key, value := range raw {
settings.order = append(settings.order, key)
settings.rates[key] = []string{}
switch value.(type) {
case string:
settings.rates[key] = []string{value.(string)}
case []interface{}:
for _, currency := range value.([]interface{}) {
str, ok := currency.(string)
if ok {
settings.rates[key] = append(settings.rates[key], str)
}
}
}
}
return &settings
}