sort slice before return

This commit is contained in:
Hayden 2024-01-05 11:33:20 -06:00
parent 645155ad10
commit 2240617c8e
No known key found for this signature in database
GPG key ID: 17CF79474E257545
3 changed files with 202 additions and 3 deletions

View file

@ -136,6 +136,9 @@ func (ctrl *V1Controller) HandleBase(ready ReadyFunc, build Build) errchain.Hand
// @Router /v1/currency [GET] // @Router /v1/currency [GET]
func (ctrl *V1Controller) HandleCurrency() errchain.HandlerFunc { func (ctrl *V1Controller) HandleCurrency() errchain.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error { return func(w http.ResponseWriter, r *http.Request) error {
// Set Cache for 10 Minutes
w.Header().Set("Cache-Control", "max-age=600")
return server.JSON(w, http.StatusOK, ctrl.svc.Currencies.Slice()) return server.JSON(w, http.StatusOK, ctrl.svc.Currencies.Slice())
} }
} }

View file

@ -7,6 +7,7 @@ import (
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"io" "io"
"slices"
"sync" "sync"
) )
@ -71,9 +72,17 @@ func NewCurrencyService(currencies []Currency) *CurrencyRegistry {
func (cs *CurrencyRegistry) Slice() []Currency { func (cs *CurrencyRegistry) Slice() []Currency {
cs.mu.RLock() cs.mu.RLock()
defer cs.mu.RUnlock() defer cs.mu.RUnlock()
out := make([]Currency, 0, len(cs.registry))
keys := make([]string, 0, len(cs.registry))
for key := range cs.registry { for key := range cs.registry {
out = append(out, cs.registry[key]) 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]])
} }
return out return out

View file

@ -1 +1,188 @@
[] [
{
"code": "AED",
"local": "United Arab Emirates",
"symbol": "د.إ",
"name": "United Arab Emirates Dirham"
},
{
"code": "AUD",
"local": "Australia",
"symbol": "A$",
"name": "Australian Dollar"
},
{
"code": "BGN",
"local": "bg-BG",
"symbol": "lv",
"name": "Bulgarian lev"
},
{
"code": "BRL",
"local": "Brazil",
"symbol": "R$",
"name": "Brazilian Real"
},
{
"code": "CAD",
"local": "Canada",
"symbol": "C$",
"name": "Canadian Dollar"
},
{
"code": "CHF",
"local": "Switzerland",
"symbol": "CHF",
"name": "Swiss Franc"
},
{
"code": "CZK",
"local": "cs-CZ",
"symbol": "Kč",
"name": "Czech Koruna"
},
{
"code": "DKK",
"local": "da-DK",
"symbol": "kr",
"name": "Danish Krone"
},
{
"code": "EUR",
"local": "Eurozone",
"symbol": "€",
"name": "Euro"
},
{
"code": "GBP",
"local": "United Kingdom",
"symbol": "£",
"name": "British Pound Sterling"
},
{
"code": "HKD",
"local": "Hong Kong",
"symbol": "HK$",
"name": "Hong Kong Dollar"
},
{
"code": "IDR",
"local": "Indonesia",
"symbol": "Rp",
"name": "Indonesian Rupiah"
},
{
"code": "INR",
"local": "India",
"symbol": "₹",
"name": "Indian Rupee"
},
{
"code": "JPY",
"local": "Japan",
"symbol": "¥",
"name": "Japanese Yen"
},
{
"code": "KRW",
"local": "South Korea",
"symbol": "₩",
"name": "South Korean Won"
},
{
"code": "MXN",
"local": "Mexico",
"symbol": "Mex$",
"name": "Mexican Peso"
},
{
"code": "NOK",
"local": "Norway",
"symbol": "kr",
"name": "Norwegian Krone"
},
{
"code": "NZD",
"local": "New Zealand",
"symbol": "NZ$",
"name": "New Zealand Dollar"
},
{
"code": "PLN",
"local": "Poland",
"symbol": "zł",
"name": "Polish Zloty"
},
{
"code": "RMB",
"local": "zh-CN",
"symbol": "¥",
"name": "Chinese Yuan"
},
{
"code": "RON",
"local": "ro-RO",
"symbol": "lei",
"name": "Romanian Leu"
},
{
"code": "RUB",
"local": "Russia",
"symbol": "₽",
"name": "Russian Ruble"
},
{
"code": "SAR",
"local": "Saudi Arabia",
"symbol": "﷼",
"name": "Saudi Riyal"
},
{
"code": "SEK",
"local": "Sweden",
"symbol": "kr",
"name": "Swedish Krona"
},
{
"code": "SGD",
"local": "Singapore",
"symbol": "S$",
"name": "Singapore Dollar"
},
{
"code": "THB",
"local": "Thailand",
"symbol": "฿",
"name": "Thai Baht"
},
{
"code": "TRY",
"local": "Turkey",
"symbol": "₺",
"name": "Turkish Lira"
},
{
"code": "USD",
"local": "United States",
"symbol": "$",
"name": "United States Dollar"
},
{
"code": "XAG",
"local": "Global",
"symbol": "XAG",
"name": "Silver Troy Ounce"
},
{
"code": "XAU",
"local": "Global",
"symbol": "XAU",
"name": "Gold Troy Ounce"
},
{
"code": "ZAR",
"local": "South Africa",
"symbol": "R",
"name": "South African Rand"
}
]