forked from thrasher-corp/gocryptotrader
/
currencies.go
93 lines (80 loc) · 1.92 KB
/
currencies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package currency
import (
"encoding/json"
"strings"
)
// NewCurrenciesFromStringArray returns a Currencies object from strings
func NewCurrenciesFromStringArray(currencies []string) Currencies {
var list 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
// Strings returns an array of currency strings
func (c Currencies) Strings() []string {
var list []string
for _, d := range c {
list = append(list, d.String())
}
return list
}
// Contains checks to see if a currency code is contained in the currency list
func (c Currencies) Contains(cc Code) bool {
for i := range c {
if c[i].Item == cc.Item {
return true
}
}
return false
}
// Join returns a comma serparated string
func (c Currencies) Join() string {
return strings.Join(c.Strings(), ",")
}
// UnmarshalJSON comforms 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
}
var allTheCurrencies Currencies
for _, data := range strings.Split(configCurrencies, ",") {
allTheCurrencies = append(allTheCurrencies, NewCode(data))
}
*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
}
for _, d := range c {
var found bool
for i := range other {
if d == other[i] {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// HasData checks to see if Currencies type has actual currencies
func (c Currencies) HasData() bool {
return len(c) != 0
}