Skip to content

Commit

Permalink
feat: add decimal custom type (#2112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Jun 26, 2024
1 parent cd741f9 commit 36a4f45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions scw/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 11 additions & 0 deletions scw/custom_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 36a4f45

Please sign in to comment.