Skip to content

Commit c97d43b

Browse files
committed
Add GoString method for Decimal and NullDecimal (#270)
1 parent fa3b22f commit c97d43b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

decimal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,12 @@ func (d *Decimal) GobDecode(data []byte) error {
14541454
return d.UnmarshalBinary(data)
14551455
}
14561456

1457+
// GoString implements fmt.GoStringer and formats %#v to be printed in Go
1458+
// source code.
1459+
func (d *Decimal) GoString() string {
1460+
return `decimal.RequireFromString("` + d.String() + `")`
1461+
}
1462+
14571463
// StringScaled first scales the decimal then calls .String() on it.
14581464
// NOTE: buggy, unintuitive, and DEPRECATED! Use StringFixed instead.
14591465
func (d Decimal) StringScaled(exp int32) string {
@@ -1682,6 +1688,15 @@ func (d NullDecimal) MarshalText() (text []byte, err error) {
16821688
return d.Decimal.MarshalText()
16831689
}
16841690

1691+
// GoString implements fmt.GoStringer and formats %#v to be printed in Go
1692+
// source code.
1693+
func (d NullDecimal) GoString() string {
1694+
if !d.Valid {
1695+
return "decimal.NullDecimal{Valid: false}"
1696+
}
1697+
return `decimal.NewNullDecimal(` + d.Decimal.GoString() + `)`
1698+
}
1699+
16851700
// Trig functions
16861701

16871702
// Atan returns the arctangent, in radians, of x.

0 commit comments

Comments
 (0)