Skip to content

Commit 163534b

Browse files
author
wayn3h0
committed
feat: make Money exchangeable & add Lookup funcs
Update Types: 1. Money: make it exchangeable for different currencies. New Types: 1. ExchangeableCurrency: Currency with exchange rate. New Functions: 1. LookupCountry 2. LookupLanguage 3. LookupCurrency 3. LookupCulture BREAKING CHANGES: use Lookup functions instead of Get functions. Removed Get functions: 1. GetCountry 2. GetLanguage 3. GetCurrency 4. GetCulture
1 parent 89ab063 commit 163534b

File tree

7 files changed

+235
-151
lines changed

7 files changed

+235
-151
lines changed

country.go

+19-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ type Country struct {
1414
Aliases *MultiLanguageStringArray
1515
}
1616

17+
// Eaual reports whether two countries are same.
18+
// It compares the alpha-2 code.
19+
func (x *Country) Equal(y *Country) bool {
20+
return strings.EqualFold(x.Alpha2Code, y.Alpha2Code)
21+
}
22+
1723
// Countries represents a sorcountryTable collection of Country.
1824
type Countries []*Country
1925

@@ -92,7 +98,7 @@ func init() {
9298
Alpha3Code: alpha3Code,
9399
NumericCode: numericCode,
94100
Name: NewMultiLanguageString(),
95-
Aliases: NewMultiLanguageStringArray("|"),
101+
Aliases: NewMultiLanguageStringArray(";"),
96102
}
97103

98104
countryTableAlpha2[alpha2Code] = country
@@ -107,44 +113,42 @@ func AllCountries() Countries {
107113
return countryList
108114
}
109115

110-
// GetCountry returns the country by given keyword (code, name, alias with language).
111-
// It returns nil if the country not found.
112-
func GetCountry(language *Language, keyword string) *Country {
116+
// LookupCountry returns the country by given keyword (code, name, alias with language).
117+
func LookupCountry(language *Language, keyword string) (*Country, bool) {
113118
keyword = strings.TrimSpace(keyword)
114119
if len(keyword) == 0 {
115-
return nil
120+
return nil, false
116121
}
117-
122+
// alpha-2 code
118123
if len(keyword) == 2 {
119124
if v, ok := countryTableAlpha2[strings.ToUpper(keyword)]; ok {
120-
return v
125+
return v, true
121126
}
122127
}
123-
128+
// alpha-3 & numeric code
124129
if len(keyword) == 3 {
125130
if v, ok := countryTableAlpha3[strings.ToUpper(keyword)]; ok {
126-
return v
131+
return v, true
127132
}
128133
if v, ok := countryTableNumeric[strings.ToUpper(keyword)]; ok {
129-
return v
134+
return v, true
130135
}
131136
}
132-
137+
// keyword
133138
if language != nil {
134139
for _, country := range countryList {
135140
// compare name
136141
name := country.Name.Value(language)
137142
if strings.EqualFold(name, keyword) {
138-
return country
143+
return country, true
139144
}
140145

141146
// compare aliases
142147
aliases := country.Aliases.Value(language)
143148
if strings.Contains(strings.ToLower(aliases), strings.ToLower(keyword)) {
144-
return country
149+
return country, true
145150
}
146151
}
147152
}
148-
149-
return nil
153+
return nil, false
150154
}

culture.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ type Culture struct {
1616
Formatter *Formatter
1717
}
1818

19+
// Eaual reports whether two cultures are same.
20+
// It compares the code.
21+
func (x *Culture) Equal(y *Culture) bool {
22+
return strings.EqualFold(x.Code, y.Code)
23+
}
24+
1925
// FormatNumber formats the number to string.
2026
func (c *Culture) FormatNumber(number float64) string {
2127
return c.Formatter.Number.Format(number)
@@ -149,16 +155,13 @@ func init() {
149155
nativeName := cultureNativeNames[code]
150156
countryCode := code[strings.LastIndex(code, "-")+1:]
151157
languageCode := code[0:strings.Index(code, "-")]
152-
country := GetCountry(nil, countryCode)
153-
language := GetLanguage(languageCode)
154-
158+
country, _ := LookupCountry(nil, countryCode)
159+
language, _ := LookupLanguage(languageCode)
155160
var curr *Currency
156161
if currencyCode, ok := cultureCurrencyCodes[code]; ok {
157-
curr = GetCurrency(currencyCode)
162+
curr, _ = LookupCurrency(currencyCode)
158163
}
159-
160164
formatter := cultureFormatters[code]
161-
162165
culture := &Culture{
163166
Code: code,
164167
NativeName: nativeName,
@@ -168,7 +171,6 @@ func init() {
168171
Currency: curr,
169172
Formatter: formatter,
170173
}
171-
172174
cultureTable[strings.ToLower(code)] = culture
173175
cultureList[i] = culture
174176
}
@@ -179,15 +181,13 @@ func AllCultures() Cultures {
179181
return cultureList
180182
}
181183

182-
// GetCulture returns the culture by given code.
183-
// It returns nil if the culture not found.
184-
func GetCulture(code string) *Culture {
184+
// LookupCulture returns the culture by given code.
185+
func LookupCulture(code string) (*Culture, bool) {
185186
code = strings.TrimSpace(code)
186187
if len(code) > 0 {
187188
if cult, ok := cultureTable[strings.ToLower(code)]; ok {
188-
return cult
189+
return cult, true
189190
}
190191
}
191-
192-
return nil
192+
return nil, false
193193
}

currency.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type Currency struct {
1111
Name *MultiLanguageString
1212
}
1313

14+
// Equal reports whether two currencies are same.
15+
// It compares the code.
16+
func (x *Currency) Equal(y *Currency) bool {
17+
return strings.EqualFold(x.Code, y.Code)
18+
}
19+
1420
// Currencies represents a sorcurrencyTable collection of Currency.
1521
type Currencies []*Currency
1622

@@ -93,15 +99,13 @@ func AllCurrencies() Currencies {
9399
return currencyList
94100
}
95101

96-
// GetCurrency returns the currency by given code.
97-
// It returns nil if the currency not found.
98-
func GetCurrency(code string) *Currency {
102+
// LookupCurrency returns the currency by given code.
103+
func LookupCurrency(code string) (*Currency, bool) {
99104
code = strings.TrimSpace(code)
100-
if len(code) > 0 {
105+
if len(code) == 3 {
101106
if curr, ok := currencyTable[strings.ToUpper(code)]; ok {
102-
return curr
107+
return curr, true
103108
}
104109
}
105-
106-
return nil
110+
return nil, false
107111
}

language.go

+8-14
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ type Language struct {
1212
Name *MultiLanguageString
1313
}
1414

15-
// Equal reports whether two language are same.
16-
// It compares the language codes.
17-
func (l *Language) Equal(another *Language) bool {
18-
if another == nil {
19-
return false
20-
}
21-
22-
return strings.EqualFold(l.Code, another.Code)
15+
// Equal reports whether two languages are same.
16+
// It compares the code.
17+
func (x *Language) Equal(y *Language) bool {
18+
return strings.EqualFold(x.Code, y.Code)
2319
}
2420

2521
// Languages represents a sortable collection of Language.
@@ -103,15 +99,13 @@ func AllLanguages() Languages {
10399
return languageList
104100
}
105101

106-
// GetLanguage returns the language by given code.
107-
// It returns nil if the language not found.
108-
func GetLanguage(code string) *Language {
102+
// LookupLanguage returns the language by given code.
103+
func LookupLanguage(code string) (*Language, bool) {
109104
code = strings.TrimSpace(code)
110105
if len(code) > 0 {
111106
if lang, ok := languageTable[strings.ToLower(code)]; ok {
112-
return lang
107+
return lang, true
113108
}
114109
}
115-
116-
return nil
110+
return nil, false
117111
}

0 commit comments

Comments
 (0)