Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (math): binary un-/marshal format for the GDA dec type. #22377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions math/dec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package math

import (
"bytes"
"encoding/binary"
"encoding/json"
"math/big"

Expand Down Expand Up @@ -400,12 +402,54 @@ func (x Dec) Reduce() (Dec, int) {
return y, n
}

// Marshal serializes the decimal value into a byte slice in binary format.
//
// The binary format specification is as follows:
//
// 1. The first byte indicates the sign of the decimal value.
// - The byte value '-' (0x2D) represents a negative value.
// - The byte value '+' (0x2B) represents a positive value.
//
// 2. The next 4 bytes represent the exponent of the decimal value in big-endian format,
// serialized as an unsigned 32-bit integer.
//
// 3. The remaining bytes represent the absolute value of the coefficient in big-endian format.
//
// An error is returned if the serialization process fails. However, in the provided code,
// the error is always `nil`.
func (x Dec) Marshal() ([]byte, error) {
panic("not implemented")
src, _ := x.Reduce()
var buf bytes.Buffer
if src.dec.Negative {
buf.WriteByte('-')
} else {
buf.WriteByte('+')
}
expBz := binary.BigEndian.AppendUint32([]byte{}, uint32(src.dec.Exponent))
buf.Write(expBz)
buf.Write(src.dec.Coeff.Bytes())
return buf.Bytes(), nil
}

// Unmarshal parses a byte slice containing a binary decimal and stores the result in the receiver.
// It returns an error if the byte slice does not represent a valid decimal.
// See Marshal for details on the encoding format.
func (x *Dec) Unmarshal(data []byte) error {
panic("not implemented")
if len(data) < 5 {
return ErrInvalidDec.Wrap("insufficient byte length")
}
coeffNeg := data[0] == '-'
exp := binary.BigEndian.Uint32(data[1:5])
coeff := new(apd.BigInt).SetBytes(data[5:])
apdDec := apd.NewWithBigInt(coeff, int32(exp))
if coeffNeg {
apdDec = apdDec.Neg(apdDec)
}
if apdDec.Form != apd.Finite {
return ErrInvalidDec.Wrap("unknown decimal form")
}
x.dec = *apdDec
return nil
}

// MarshalTo encodes the receiver into the provided byte slice and returns the number of bytes written and any error encountered.
Expand Down
5 changes: 2 additions & 3 deletions math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,6 @@ func must[T any](r T, err error) T {
}

func TestMarshalUnmarshal(t *testing.T) {
t.Skip("not supported, yet")
specs := map[string]struct {
x Dec
exp string
Expand Down Expand Up @@ -1391,8 +1390,8 @@ func TestMarshalUnmarshal(t *testing.T) {
exp: "1E+100000",
},
"1.1e100000": {
x: NewDecWithExp(11, 100_000),
expErr: ErrInvalidDec,
x: NewDecWithExp(11, 100_000),
exp: "1.E+100001",
},
"1.e100000": {
x: NewDecWithExp(1, 100_000),
Expand Down
Loading