github.com/prebid/prebid-server/v2@v2.18.0/currency/constant_rates.go (about)

     1  package currency
     2  
     3  import (
     4  	"golang.org/x/text/currency"
     5  )
     6  
     7  // ConstantRates doesn't do any currency conversions and accepts only conversions where
     8  // both currencies (from and to) are the same.
     9  // If not the same currencies, it returns an error.
    10  type ConstantRates struct{}
    11  
    12  // NewConstantRates creates a new ConstantRates object holding currencies rates
    13  func NewConstantRates() *ConstantRates {
    14  	return &ConstantRates{}
    15  }
    16  
    17  // GetRate returns 1 if both currencies are the same.
    18  // If not, it will return an error.
    19  func (r *ConstantRates) GetRate(from string, to string) (float64, error) {
    20  	fromUnit, err := currency.ParseISO(from)
    21  	if err != nil {
    22  		return 0, err
    23  	}
    24  	toUnit, err := currency.ParseISO(to)
    25  	if err != nil {
    26  		return 0, err
    27  	}
    28  
    29  	if fromUnit.String() != toUnit.String() {
    30  		return 0, ConversionNotFoundError{FromCur: fromUnit.String(), ToCur: toUnit.String()}
    31  	}
    32  
    33  	return 1, nil
    34  }
    35  
    36  // GetRates returns current rates
    37  func (r *ConstantRates) GetRates() *map[string]map[string]float64 {
    38  	return nil
    39  }