From c017c29e6d8c5a3c75fea9a5be1796288f0a412c Mon Sep 17 00:00:00 2001 From: Jeff Mitchell <134713709+jeffmitchell-sas@users.noreply.github.com> Date: Fri, 26 Apr 2024 19:19:46 -0400 Subject: [PATCH] Fix math/big.Int Does not Marshal (#194) * fix mashalling of math/big.Int * Revert BigIntType. It is not needed. --- ion/marshal.go | 11 ++++++++++- ion/marshal_test.go | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ion/marshal.go b/ion/marshal.go index 49a87b2..f74990e 100644 --- a/ion/marshal.go +++ b/ion/marshal.go @@ -384,6 +384,9 @@ func (m *Encoder) encodeStruct(v reflect.Value) error { if t == decimalType { return m.encodeDecimal(v) } + if t == bigIntType { + return m.encodeBigInt(v) + } if err := m.w.BeginStruct(); err != nil { return err @@ -445,12 +448,18 @@ func (m *Encoder) encodeTimeDate(v reflect.Value) error { return m.w.WriteTimestamp(timestamp) } -// EncodeDecimal encodes an ion.Decimal to the output writer as an Ion decimal. +// encodeDecimal encodes an ion.Decimal to the output writer as an Ion decimal. func (m *Encoder) encodeDecimal(v reflect.Value) error { d := v.Addr().Interface().(*Decimal) return m.w.WriteDecimal(d) } +// encodeBigInt encodes a math/big.Int to the output writer as a big.Int. +func (m *Encoder) encodeBigInt(v reflect.Value) error { + b := v.Addr().Interface().(*big.Int) + return m.w.WriteBigInt(b) +} + func (m *Encoder) encodeWithAnnotation(v reflect.Value, fields []field) error { original := v for _, field := range fields { diff --git a/ion/marshal_test.go b/ion/marshal_test.go index 3508a96..c8060d2 100644 --- a/ion/marshal_test.go +++ b/ion/marshal_test.go @@ -18,6 +18,7 @@ package ion import ( "bytes" "math" + "math/big" "strings" "testing" "time" @@ -42,6 +43,10 @@ func TestMarshalText(t *testing.T) { test(byte(42), "42") test(-42, "-42") test(uint64(math.MaxUint64), "18446744073709551615") + bigIntPos, _ := new(big.Int).SetString("123456789012345678901234567890", 10) + test(bigIntPos, "123456789012345678901234567890") + bigIntNeg, _ := new(big.Int).SetString("-123456789012345678901234567890", 10) + test(bigIntNeg, "-123456789012345678901234567890") test(math.MinInt64, "-9223372036854775808") test(42.0, "4.2e+1")