2024-01-05 17:12:33 +00:00
|
|
|
// Package currencies provides a shared definition of currencies. This uses a global
|
|
|
|
// variable to hold the currencies.
|
|
|
|
package currencies
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
_ "embed"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2024-01-05 17:33:20 +00:00
|
|
|
"slices"
|
2024-01-05 17:12:33 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed currencies.json
|
|
|
|
var defaults []byte
|
|
|
|
|
|
|
|
type CollectorFunc func() ([]Currency, error)
|
|
|
|
|
|
|
|
func CollectJSON(reader io.Reader) CollectorFunc {
|
|
|
|
return func() ([]Currency, error) {
|
|
|
|
var currencies []Currency
|
|
|
|
err := json.NewDecoder(reader).Decode(¤cies)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return currencies, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CollectDefaults() CollectorFunc {
|
|
|
|
return CollectJSON(bytes.NewReader(defaults))
|
|
|
|
}
|
|
|
|
|
|
|
|
func CollectionCurrencies(collectors ...CollectorFunc) ([]Currency, error) {
|
|
|
|
out := make([]Currency, 0, len(collectors))
|
|
|
|
for i := range collectors {
|
|
|
|
c, err := collectors[i]()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, c...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Currency struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
Local string `json:"local"`
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CurrencyRegistry struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
registry map[string]Currency
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCurrencyService(currencies []Currency) *CurrencyRegistry {
|
|
|
|
registry := make(map[string]Currency, len(currencies))
|
|
|
|
for i := range currencies {
|
|
|
|
registry[currencies[i].Code] = currencies[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &CurrencyRegistry{
|
|
|
|
registry: registry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:26:43 +00:00
|
|
|
func (cs *CurrencyRegistry) Slice() []Currency {
|
2024-01-05 17:12:33 +00:00
|
|
|
cs.mu.RLock()
|
|
|
|
defer cs.mu.RUnlock()
|
2024-01-05 17:33:20 +00:00
|
|
|
|
|
|
|
keys := make([]string, 0, len(cs.registry))
|
2024-01-05 17:12:33 +00:00
|
|
|
for key := range cs.registry {
|
2024-01-05 17:33:20 +00:00
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.Sort(keys)
|
|
|
|
|
|
|
|
out := make([]Currency, 0, len(cs.registry))
|
|
|
|
for i := range keys {
|
|
|
|
out = append(out, cs.registry[keys[i]])
|
2024-01-05 17:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *CurrencyRegistry) IsSupported(code string) bool {
|
|
|
|
cs.mu.RLock()
|
|
|
|
defer cs.mu.RUnlock()
|
|
|
|
_, ok := cs.registry[code]
|
|
|
|
return ok
|
|
|
|
}
|