From 5bc69b6669678bfeacda289af502f960c1c08331 Mon Sep 17 00:00:00 2001 From: Quentin Brosse Date: Thu, 9 Jan 2020 18:51:03 +0100 Subject: [PATCH 1/3] feat(core): add String method to scw.Money --- scw/custom_types.go | 22 +++++++++++++++++++++ scw/custom_types_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/scw/custom_types.go b/scw/custom_types.go index 5d723b370..e22e88787 100644 --- a/scw/custom_types.go +++ b/scw/custom_types.go @@ -82,6 +82,28 @@ func NewMoneyFromFloat(value float64, currency string) *Money { } } +// String returns the string representation of Money. +func (m *Money) String() string { + defaultCurrencySign := "€" + currencySignsByCodes := map[string]string{ + "EUR": defaultCurrencySign, + "USD": "$", + } + + currencySign, currencySignFound := currencySignsByCodes[m.CurrencyCode] + if !currencySignFound { + currencySign = defaultCurrencySign + } + + str := fmt.Sprintf("%s %d", currencySign, m.Units) + + if m.Nanos != 0 { + str += fmt.Sprintf(".%d", m.Nanos) + } + + return str +} + // ToFloat converts a Money object to a float. func (m *Money) ToFloat() float64 { return float64(m.Units) + float64(m.Nanos)/1000000000 diff --git a/scw/custom_types_test.go b/scw/custom_types_test.go index 587ee8c04..dc805f25d 100644 --- a/scw/custom_types_test.go +++ b/scw/custom_types_test.go @@ -11,6 +11,48 @@ import ( "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" ) +func TestMoney_String(t *testing.T) { + cases := []struct { + money Money + want string + }{ + { + money: Money{ + CurrencyCode: "EUR", + Units: 10, + }, + want: "€ 10", + }, + { + money: Money{ + CurrencyCode: "USD", + Units: 10, + Nanos: 1, + }, + want: "$ 10.1", + }, + { + money: Money{ + CurrencyCode: "EUR", + Nanos: 1, + }, + want: "€ 0.1", + }, + { + money: Money{ + CurrencyCode: "?", + }, + want: "€ 0", + }, + } + + for _, c := range cases { + t.Run(c.want, func(t *testing.T) { + testhelpers.Equals(t, c.want, c.money.String()) + }) + } +} + func TestSize_String(t *testing.T) { cases := []struct { size Size From ad124ecae8a856f530acdda61cadcd1b8e9c3a44 Mon Sep 17 00:00:00 2001 From: Quentin Brosse Date: Fri, 10 Jan 2020 10:35:52 +0100 Subject: [PATCH 2/3] address comments --- scw/custom_types.go | 22 +++++++++++++++------- scw/custom_types_test.go | 13 ++++++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/scw/custom_types.go b/scw/custom_types.go index e22e88787..0845c30ce 100644 --- a/scw/custom_types.go +++ b/scw/custom_types.go @@ -6,7 +6,11 @@ import ( "fmt" "io" "net" + "strconv" + "strings" "time" + + "github.com/scaleway/scaleway-sdk-go/logger" ) // ServiceInfo contains API metadata @@ -84,24 +88,28 @@ func NewMoneyFromFloat(value float64, currency string) *Money { // String returns the string representation of Money. func (m *Money) String() string { - defaultCurrencySign := "€" currencySignsByCodes := map[string]string{ - "EUR": defaultCurrencySign, + "EUR": "€", "USD": "$", } currencySign, currencySignFound := currencySignsByCodes[m.CurrencyCode] if !currencySignFound { - currencySign = defaultCurrencySign + logger.Debugf("%s currency code is not supported", m.CurrencyCode) + currencySign = m.CurrencyCode } - str := fmt.Sprintf("%s %d", currencySign, m.Units) - + value := fmt.Sprint(m.Units) if m.Nanos != 0 { - str += fmt.Sprintf(".%d", m.Nanos) + value = strconv.FormatFloat(m.ToFloat(), 'f', 9, 64) + } + + // Trim leading zeros. + if strings.HasSuffix(value, "00") { + value = strings.TrimRight(value, "0") } - return str + return currencySign + " " + value } // ToFloat converts a Money object to a float. diff --git a/scw/custom_types_test.go b/scw/custom_types_test.go index dc805f25d..c5dff32bf 100644 --- a/scw/custom_types_test.go +++ b/scw/custom_types_test.go @@ -29,20 +29,27 @@ func TestMoney_String(t *testing.T) { Units: 10, Nanos: 1, }, - want: "$ 10.1", + want: "$ 10.000000001", }, { money: Money{ CurrencyCode: "EUR", - Nanos: 1, + Nanos: 100000000, }, want: "€ 0.1", }, + { + money: Money{ + CurrencyCode: "EUR", + Nanos: 500000, + }, + want: "€ 0.0005", + }, { money: Money{ CurrencyCode: "?", }, - want: "€ 0", + want: "? 0", }, } From 6847c666aa4070cc6d5a0e10c3faa9ead76c54b3 Mon Sep 17 00:00:00 2001 From: Quentin Brosse Date: Fri, 10 Jan 2020 11:51:20 +0100 Subject: [PATCH 3/3] address comments --- scw/custom_types.go | 14 +++----------- scw/custom_types_test.go | 13 ++++++++++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/scw/custom_types.go b/scw/custom_types.go index 0845c30ce..658e618ec 100644 --- a/scw/custom_types.go +++ b/scw/custom_types.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "net" - "strconv" "strings" "time" @@ -99,17 +98,10 @@ func (m *Money) String() string { currencySign = m.CurrencyCode } - value := fmt.Sprint(m.Units) - if m.Nanos != 0 { - value = strconv.FormatFloat(m.ToFloat(), 'f', 9, 64) - } - - // Trim leading zeros. - if strings.HasSuffix(value, "00") { - value = strings.TrimRight(value, "0") - } + cents := fmt.Sprintf("%09d", m.Nanos) + cents = cents[:2] + strings.TrimRight(cents[2:], "0") - return currencySign + " " + value + return fmt.Sprintf("%s %d.%s", currencySign, m.Units, cents) } // ToFloat converts a Money object to a float. diff --git a/scw/custom_types_test.go b/scw/custom_types_test.go index c5dff32bf..d55c173a9 100644 --- a/scw/custom_types_test.go +++ b/scw/custom_types_test.go @@ -21,7 +21,7 @@ func TestMoney_String(t *testing.T) { CurrencyCode: "EUR", Units: 10, }, - want: "€ 10", + want: "€ 10.00", }, { money: Money{ @@ -36,7 +36,7 @@ func TestMoney_String(t *testing.T) { CurrencyCode: "EUR", Nanos: 100000000, }, - want: "€ 0.1", + want: "€ 0.10", }, { money: Money{ @@ -45,11 +45,18 @@ func TestMoney_String(t *testing.T) { }, want: "€ 0.0005", }, + { + money: Money{ + CurrencyCode: "EUR", + Nanos: 123456789, + }, + want: "€ 0.123456789", + }, { money: Money{ CurrencyCode: "?", }, - want: "? 0", + want: "? 0.00", }, }