From 92294e9b1445a4d38ecf46bcf03b780ca34b6d07 Mon Sep 17 00:00:00 2001 From: Eric Lagergren Date: Wed, 9 Oct 2019 11:40:23 -0700 Subject: [PATCH] init --- encode.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/encode.go b/encode.go index 88a322cd..a5a1cc8c 100644 --- a/encode.go +++ b/encode.go @@ -47,7 +47,8 @@ func encode(parameterStatus *parameterStatus, x interface{}, pgtypOid oid.Oid) [ return strconv.AppendBool(nil, v) case time.Time: return formatTs(v) - + case decomposer: + // TODO: https://github.com/sfackler/rust-postgres/issues/119#issuecomment-166669557 default: errorf("encode: unknown type for %T", v) } @@ -593,3 +594,33 @@ func (nt NullTime) Value() (driver.Value, error) { } return nt.Time, nil } + +// decomposer composes or decomposes a decimal value to and from individual parts. +// There are four separate parts: a boolean negative flag, a form byte with three possible states +// (finite=0, infinite=1, NaN=2), a base-2 big-endian integer +// coefficient (also known as a significand) as a []byte, and an int32 exponent. +// These are composed into a final value as "decimal = (neg) (form=finite) coefficient * 10 ^ exponent". +// A zero length coefficient is a zero value. +// If the form is not finite the coefficient and scale should be ignored. +// The negative parameter may be set to true for any form, although implementations are not required +// to respect the negative parameter in the non-finite form. +// +// Implementations may choose to signal a negative zero or negative NaN, but implementations +// that do not support these may also ignore the negative zero or negative NaN without error. +// If an implementation does not support Infinity it may be converted into a NaN without error. +// If a value is set that is larger then what is supported by an implementation is attempted to +// be set, an error must be returned. +// Implementations must return an error if a NaN or Infinity is attempted to be set while neither +// are supported. +type decomposer interface { + // Decompose returns the internal decimal state into parts. + // If the provided buf has sufficient capacity, buf may be returned as the coefficient with + // the value set and length set as appropriate. + Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32) + + // Compose sets the internal decimal value from parts. If the value cannot be + // represented then an error should be returned. + // The coefficent should not be modified. Successive calls to compose with + // the same arguments should result in the same decimal value. + Compose(form byte, negative bool, coefficient []byte, exponent int32) error +}