mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-25 18:15:40 +00:00
basic currency service for loading at runtime
This commit is contained in:
parent
d920e1e215
commit
dbf1373e0c
2 changed files with 88 additions and 0 deletions
87
backend/internal/core/currencies/currencies.go
Normal file
87
backend/internal/core/currencies/currencies.go
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
// 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"
|
||||||
|
"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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cs *CurrencyRegistry) GetCurrencies() []Currency {
|
||||||
|
cs.mu.RLock()
|
||||||
|
defer cs.mu.RUnlock()
|
||||||
|
out := make([]Currency, 0, len(cs.registry))
|
||||||
|
for key := range cs.registry {
|
||||||
|
out = append(out, cs.registry[key])
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cs *CurrencyRegistry) IsSupported(code string) bool {
|
||||||
|
cs.mu.RLock()
|
||||||
|
defer cs.mu.RUnlock()
|
||||||
|
_, ok := cs.registry[code]
|
||||||
|
return ok
|
||||||
|
}
|
1
backend/internal/core/currencies/currencies.json
Normal file
1
backend/internal/core/currencies/currencies.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
[]
|
Loading…
Reference in a new issue