mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-24 21:30:49 +01:00
Adds MarshalYAML for marshaling yaml
Adds MarshalYAMLWithoutQuotes for marshaling Decimal without quotes
This commit is contained in:
parent
a1bdfc355e
commit
161f04f87d
2 changed files with 25 additions and 0 deletions
23
decimal.go
23
decimal.go
|
@ -25,6 +25,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DivisionPrecision is the number of decimal places in the result when it
|
// DivisionPrecision is the number of decimal places in the result when it
|
||||||
|
@ -65,6 +66,14 @@ var PowPrecisionNegativeExponent = 16
|
||||||
// silently lose precision.
|
// silently lose precision.
|
||||||
var MarshalJSONWithoutQuotes = false
|
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
|
// ExpMaxIterations specifies the maximum number of iterations needed to calculate
|
||||||
// precise natural exponent value using ExpHullAbrham method.
|
// precise natural exponent value using ExpHullAbrham method.
|
||||||
var ExpMaxIterations = 1000
|
var ExpMaxIterations = 1000
|
||||||
|
@ -1790,6 +1799,20 @@ func (d Decimal) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(str), nil
|
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
|
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation
|
||||||
// is already used when encoding to text, this method stores that string as []byte
|
// is already used when encoding to text, this method stores that string as []byte
|
||||||
func (d *Decimal) UnmarshalBinary(data []byte) error {
|
func (d *Decimal) UnmarshalBinary(data []byte) error {
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module github.com/shopspring/decimal
|
module github.com/shopspring/decimal
|
||||||
|
|
||||||
go 1.10
|
go 1.10
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|
Loading…
Reference in a new issue