forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstant_rates.go
39 lines (32 loc) · 1012 Bytes
/
constant_rates.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
package currency
import (
"golang.org/x/text/currency"
)
// ConstantRates doesn't do any currency conversions and accepts only conversions where
// both currencies (from and to) are the same.
// If not the same currencies, it returns an error.
type ConstantRates struct{}
// NewConstantRates creates a new ConstantRates object holding currencies rates
func NewConstantRates() *ConstantRates {
return &ConstantRates{}
}
// GetRate returns 1 if both currencies are the same.
// If not, it will return an error.
func (r *ConstantRates) GetRate(from string, to string) (float64, error) {
fromUnit, err := currency.ParseISO(from)
if err != nil {
return 0, err
}
toUnit, err := currency.ParseISO(to)
if err != nil {
return 0, err
}
if fromUnit.String() != toUnit.String() {
return 0, ConversionNotFoundError{FromCur: fromUnit.String(), ToCur: toUnit.String()}
}
return 1, nil
}
// GetRates returns current rates
func (r *ConstantRates) GetRates() *map[string]map[string]float64 {
return nil
}