From 161f04f87dddc8fe55e408f603b33065d638f733 Mon Sep 17 00:00:00 2001 From: Chad Skeeters Date: Mon, 4 Nov 2024 08:45:51 -0600 Subject: [PATCH 1/3] Adds MarshalYAML for marshaling yaml Adds MarshalYAMLWithoutQuotes for marshaling Decimal without quotes --- decimal.go | 23 +++++++++++++++++++++++ go.mod | 2 ++ 2 files changed, 25 insertions(+) diff --git a/decimal.go b/decimal.go index a37a2301..3caf0919 100644 --- a/decimal.go +++ b/decimal.go @@ -25,6 +25,7 @@ import ( "regexp" "strconv" "strings" + "gopkg.in/yaml.v3" ) // DivisionPrecision is the number of decimal places in the result when it @@ -65,6 +66,14 @@ var PowPrecisionNegativeExponent = 16 // silently lose precision. var MarshalJSONWithoutQuotes = false +// MarshalYAMLWithoutQuotes should be set to true if you want the decimal to +// be YAML marshaled as a number, instead of as a string. +// WARNING: this is dangerous for decimals with many digits, since a YAML +// unmarshallers may unmarshal a YAML numbers as an IEEE 754 +// double-precision floating point number, which means you can potentially +// silently lose precision. +var MarshalYAMLWithoutQuotes = false + // ExpMaxIterations specifies the maximum number of iterations needed to calculate // precise natural exponent value using ExpHullAbrham method. var ExpMaxIterations = 1000 @@ -1790,6 +1799,20 @@ func (d Decimal) MarshalJSON() ([]byte, error) { return []byte(str), nil } +// MarshalYAML implements the yaml.Marshaler interface. +func (d Decimal) MarshalYAML() (interface{}, error) { + if MarshalYAMLWithoutQuotes { + n := yaml.Node{ + Kind: yaml.ScalarNode, + Style: yaml.TaggedStyle, // yaml_PLAIN_SCALAR_STYLE == TaggedStyle + Value: d.String(), + } + return n, nil + } + + return d.String(), nil +} + // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation // is already used when encoding to text, this method stores that string as []byte func (d *Decimal) UnmarshalBinary(data []byte) error { diff --git a/go.mod b/go.mod index 3d264747..2f15ed28 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/shopspring/decimal go 1.10 + +require gopkg.in/yaml.v3 v3.0.1 // indirect From f20d7c4b90ab4e9b942b0e3e9e394583ea3502ec Mon Sep 17 00:00:00 2001 From: Chad Skeeters Date: Mon, 11 Nov 2024 10:30:00 -0600 Subject: [PATCH 2/3] Updates comment to comply with go doc Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> --- decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decimal.go b/decimal.go index 3caf0919..ad7e446a 100644 --- a/decimal.go +++ b/decimal.go @@ -1799,7 +1799,7 @@ func (d Decimal) MarshalJSON() ([]byte, error) { return []byte(str), nil } -// MarshalYAML implements the yaml.Marshaler interface. +// MarshalYAML implements the [yaml.Marshaler] interface. func (d Decimal) MarshalYAML() (interface{}, error) { if MarshalYAMLWithoutQuotes { n := yaml.Node{ From cd917b4ccd9fff15a02ffbb69dd2f38d11d582d2 Mon Sep 17 00:00:00 2001 From: Chad Skeeters Date: Mon, 11 Nov 2024 10:30:39 -0600 Subject: [PATCH 3/3] Updates MarshalYAML interface to use 'any' Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> --- decimal.go | 2 +- go.mod | 2 +- go.sum | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 go.sum diff --git a/decimal.go b/decimal.go index ad7e446a..35f6e0e7 100644 --- a/decimal.go +++ b/decimal.go @@ -1800,7 +1800,7 @@ func (d Decimal) MarshalJSON() ([]byte, error) { } // MarshalYAML implements the [yaml.Marshaler] interface. -func (d Decimal) MarshalYAML() (interface{}, error) { +func (d Decimal) MarshalYAML() (any, error) { if MarshalYAMLWithoutQuotes { n := yaml.Node{ Kind: yaml.ScalarNode, diff --git a/go.mod b/go.mod index 2f15ed28..a5c1a23f 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/shopspring/decimal -go 1.10 +go 1.18 require gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..4bc03378 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=