currency.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package unit
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type Currency string
  7. // TODO which of these are supported in the pricing APIs??
  8. // TODO if the configured currency is NOT available, default to USD!
  9. const (
  10. AUD Currency = "AUD"
  11. BRL Currency = "BRL"
  12. CAD Currency = "CAD"
  13. CHF Currency = "CHF"
  14. CNY Currency = "CNY"
  15. DKK Currency = "DKK"
  16. EUR Currency = "EUR"
  17. GBP Currency = "GBP"
  18. IDR Currency = "IDR"
  19. INR Currency = "INR"
  20. JPY Currency = "JPY"
  21. NOK Currency = "NOK"
  22. PLN Currency = "PLN"
  23. SEK Currency = "SEK"
  24. USD Currency = "USD"
  25. )
  26. // validCurrencies is a map of all valid currency codes for quick lookup
  27. var validCurrencies = map[string]Currency{
  28. string(AUD): AUD,
  29. string(BRL): BRL,
  30. string(CAD): CAD,
  31. string(CHF): CHF,
  32. string(CNY): CNY,
  33. string(DKK): DKK,
  34. string(EUR): EUR,
  35. string(GBP): GBP,
  36. string(IDR): IDR,
  37. string(INR): INR,
  38. string(JPY): JPY,
  39. string(NOK): NOK,
  40. string(PLN): PLN,
  41. string(SEK): SEK,
  42. string(USD): USD,
  43. }
  44. // ParseCurrency parses a string into a Currency type.
  45. // It performs case-insensitive matching and returns an error if the string
  46. // does not match any valid currency code.
  47. func ParseCurrency(s string) (Currency, error) {
  48. upper := strings.ToUpper(s)
  49. if currency, ok := validCurrencies[upper]; ok {
  50. return currency, nil
  51. }
  52. return "", fmt.Errorf("invalid currency: %q", s)
  53. }