From 7cb265098f4af38412b655601ac2b095193bfa69 Mon Sep 17 00:00:00 2001 From: Travis Date: Tue, 31 Mar 2020 12:43:09 -0500 Subject: [PATCH] yaml marshal/unmarshal for pql.Decimal --- pql/decimal.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pql/decimal.go b/pql/decimal.go index 6a18019df..186fff037 100644 --- a/pql/decimal.go +++ b/pql/decimal.go @@ -455,3 +455,38 @@ func (d *Decimal) UnmarshalJSON(data []byte) error { func (d Decimal) MarshalJSON() ([]byte, error) { return []byte(d.String()), nil } + +// UnmarshalYAML is a custom unmarshaller for the Decimal +// type. +func (d *Decimal) UnmarshalYAML(unmarshal func(interface{}) error) error { + var data string + if err := unmarshal(&data); err != nil { + return err + } + + o, err := ParseDecimal(data) + if err != nil { + return errors.Wrapf(err, "parsing decimal: %s", data) + } + d.Value = o.Value + d.Scale = o.Scale + + return nil +} + +// MarshalYAML is a custom marshaller for the Decimal type. +func (d Decimal) MarshalYAML() (interface{}, error) { + // TODO: I don't love that this results in a quoted string + // in the yaml document: + // + // min: "-100.05" + // + // It would be nice if we could get that to result in: + // + // min: -100.05 + // + // Note that we _can_ do that by casting the output as + // float64 (for certain cases), but the whole point of + // Decimal is to avoid using float64. + return d.String(), nil +}