Skip to content

Commit 36a4f45

Browse files
authored
feat: add decimal custom type (#2112)
1 parent cd741f9 commit 36a4f45

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

scw/custom_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,15 @@ func EncodeJSONObject(v JSONObject, escape EscapeMode) (string, error) {
448448

449449
panic(fmt.Sprintf("EncodeJSONObject called with unknown EscapeMode, %v", escape))
450450
}
451+
452+
// Decimal is a representation of a decimal value, such as 2.5.
453+
// Comparable to language-native decimal formats, such as Java's BigDecimal or
454+
// Python's decimal.Decimal.
455+
// Lookup protobuf google.type.Decimal for details.
456+
type Decimal string
457+
458+
var _ fmt.Stringer = (*Decimal)(nil)
459+
460+
func (d Decimal) String() string {
461+
return string(d)
462+
}

scw/custom_types_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,3 +829,14 @@ func TestJSONObject_MarshalJSON(t *testing.T) {
829829
})
830830
}
831831
}
832+
833+
func TestDecimal(t *testing.T) {
834+
d := Decimal("1.22")
835+
testhelpers.Equals(t, "1.22", d.String())
836+
837+
dPtr := new(Decimal)
838+
testhelpers.Equals(t, "", dPtr.String())
839+
840+
*dPtr = "1.22"
841+
testhelpers.Equals(t, "1.22", dPtr.String())
842+
}

0 commit comments

Comments
 (0)