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

Fix math/big.Int Does not Marshal #194

Merged
merged 2 commits into from
Apr 26, 2024
Merged
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
11 changes: 10 additions & 1 deletion ion/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions ion/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ion
import (
"bytes"
"math"
"math/big"
"strings"
"testing"
"time"
Expand All @@ -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")
Expand Down
Loading