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 binary marshalling of zero value #253

Merged
merged 2 commits into from Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions decimal.go
Expand Up @@ -1353,9 +1353,9 @@ func (d Decimal) MarshalJSON() ([]byte, error) {
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. As a string representation
// is already used when encoding to text, this method stores that string as []byte
func (d *Decimal) UnmarshalBinary(data []byte) error {
// Verify we have at least 5 bytes, 4 for the exponent and at least 1 more
// for the GOB encoded value.
if len(data) < 5 {
// Verify we have at least 4 bytes for the exponent. The GOB encoded value
// can be empty.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just grammar thing. I think may/might would suit better here than can

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, I fixed it.

if len(data) < 4 {
return fmt.Errorf("error decoding binary %v: expected at least 5 bytes, got %d", data, len(data))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we change this error message to ...expected at least 4 bytes...? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that makes a lot of sense ^^'

}

Expand Down
19 changes: 19 additions & 0 deletions decimal_test.go
Expand Up @@ -3002,6 +3002,25 @@ func TestBinary(t *testing.T) {
}
}

func TestBinary_Zero(t *testing.T) {
var d1 Decimal

b, err := d1.MarshalBinary()
if err != nil {
t.Fatalf("error marshalling %v to binary: %v", d1, err)
}

var d2 Decimal
err = (&d2).UnmarshalBinary(b)
if err != nil {
t.Errorf("error unmarshalling from binary: %v", err)
}

if !d1.Equals(d2) {
t.Errorf("expected %v when restoring, got %v", d1, d2)
}
}

func TestBinary_DataTooShort(t *testing.T) {
var d Decimal

Expand Down