From 36a4f4571c7ad5568df128ca867972f4cd83deb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jules=20Cast=C3=A9ran?= Date: Wed, 26 Jun 2024 10:14:17 +0200 Subject: [PATCH] feat: add decimal custom type (#2112) --- scw/custom_types.go | 12 ++++++++++++ scw/custom_types_test.go | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/scw/custom_types.go b/scw/custom_types.go index 673a598eb..d751e3658 100644 --- a/scw/custom_types.go +++ b/scw/custom_types.go @@ -448,3 +448,15 @@ func EncodeJSONObject(v JSONObject, escape EscapeMode) (string, error) { panic(fmt.Sprintf("EncodeJSONObject called with unknown EscapeMode, %v", escape)) } + +// Decimal is a representation of a decimal value, such as 2.5. +// Comparable to language-native decimal formats, such as Java's BigDecimal or +// Python's decimal.Decimal. +// Lookup protobuf google.type.Decimal for details. +type Decimal string + +var _ fmt.Stringer = (*Decimal)(nil) + +func (d Decimal) String() string { + return string(d) +} diff --git a/scw/custom_types_test.go b/scw/custom_types_test.go index 422f80a0a..c499277f9 100644 --- a/scw/custom_types_test.go +++ b/scw/custom_types_test.go @@ -829,3 +829,14 @@ func TestJSONObject_MarshalJSON(t *testing.T) { }) } } + +func TestDecimal(t *testing.T) { + d := Decimal("1.22") + testhelpers.Equals(t, "1.22", d.String()) + + dPtr := new(Decimal) + testhelpers.Equals(t, "", dPtr.String()) + + *dPtr = "1.22" + testhelpers.Equals(t, "1.22", dPtr.String()) +}