Files
gocryptotrader/currency/currencies.go
Ryan O'Hara-Reid e99adca86f encoding/json: Add custom JSON package with build tag support for Sonic (#1623)
* tag optional sonic and allow full library conversion

* Add workflow and disallow arm and darwin usage

* Add basic hotswap benchmark

* linter: fix

* use bash

* linter: fix?

* Fix whoopsie, add to make file, also add mention in features list.

* test enforcement

* actually read documentation see if this works

* linter: fix

* linter: fix

* sonic: bump tagged version

* encoding/json: drop build tag arch and os filters

* encoding/json: consolidate tests

* encoding/json: log build tag usage

* rm superfluous builds

* glorious/nits: add template change and regen docs

* glorious/nits: update commentary on nolint directive

* glorious/nits: rm init func and log results in main.go

* Test to actually pull flag in

* linter: fix

* thrasher: nits

* gk: nits 4 goflags goooooooooo!

* gk: nits rn

* make sonic default json implementation

* screen 386

* linter: fix

* Add commentary

* glorious: nits Makefile not working

* gk: nits

* gk: nits whoops

* whoopsirino

* mention 32bit systems won't be sonic

* gk: super-duper nit of extremes

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
2025-02-20 16:05:55 +11:00

101 lines
2.1 KiB
Go

package currency
import (
"strings"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
// NewCurrenciesFromStringArray returns a Currencies object from strings
func NewCurrenciesFromStringArray(currencies []string) Currencies {
list := make(Currencies, 0, len(currencies))
for i := range currencies {
if currencies[i] == "" {
continue
}
list = append(list, NewCode(currencies[i]))
}
return list
}
// Currencies define a range of supported currency codes
type Currencies []Code
// Add adds a currency to the list if it doesn't exist
func (c Currencies) Add(a Code) Currencies {
if !c.Contains(a) {
c = append(c, a)
}
return c
}
// Strings returns an array of currency strings
func (c Currencies) Strings() []string {
list := make([]string, len(c))
for i := range c {
list[i] = c[i].String()
}
return list
}
// Contains checks to see if a currency code is contained in the currency list
func (c Currencies) Contains(check Code) bool {
for i := range c {
if c[i].Equal(check) {
return true
}
}
return false
}
// Join returns a comma serparated string
func (c Currencies) Join() string {
return strings.Join(c.Strings(), ",")
}
// UnmarshalJSON conforms type to the umarshaler interface
func (c *Currencies) UnmarshalJSON(d []byte) error {
var configCurrencies string
err := json.Unmarshal(d, &configCurrencies)
if err != nil {
return err
}
curr := strings.Split(configCurrencies, ",")
allTheCurrencies := make(Currencies, len(curr))
for i := range curr {
allTheCurrencies[i] = NewCode(curr[i])
}
*c = allTheCurrencies
return nil
}
// MarshalJSON conforms type to the marshaler interface
func (c Currencies) MarshalJSON() ([]byte, error) {
return json.Marshal(c.Join())
}
// Match returns if the full list equals the supplied list
func (c Currencies) Match(other Currencies) bool {
if len(c) != len(other) {
return false
}
match:
for x := range c {
for y := range other {
if c[x].Equal(other[y]) {
continue match
}
}
return false
}
return true
}
// HasData checks to see if Currencies type has actual currencies
func (c Currencies) HasData() bool {
return len(c) != 0
}