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

doc: fix markdown for the decimal package #202

Merged
merged 1 commit into from
Aug 17, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.

### Fixed

- Markdown of documentation for the decimal subpackage (#201)

## [1.7.0] - 2022-08-02

### Added
Expand Down
11 changes: 6 additions & 5 deletions decimal/bcd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package decimal

// Package decimal implements methods to encode and decode BCD.
//
// BCD (Binary-Coded Decimal) is a sequence of bytes representing decimal
Expand Down Expand Up @@ -26,21 +28,20 @@
//
// The decimal -12.34 will be encoded as 0xd6, 0x01, 0x02, 0x01, 0x23, 0x4d:
//
// | MP_EXT (fixext 4) | MP_DECIMAL | scale | 1 | 2,3 | 4 (minus) |
// | 0xd6 | 0x01 | 0x02 | 0x01 | 0x23 | 0x4d |
// | MP_EXT (fixext 4) | MP_DECIMAL | scale | 1 | 2,3 | 4 (minus) |
// | 0xd6 | 0x01 | 0x02 | 0x01 | 0x23 | 0x4d |
//
// The decimal 0.000000000000000000000000000000000010 will be encoded as
// 0xc7, 0x03, 0x01, 0x24, 0x01, 0x0c:
//
// | MP_EXT (ext 8) | length | MP_DECIMAL | scale | 1 | 0 (plus) |
// | 0xc7 | 0x03 | 0x01 | 0x24 | 0x01 | 0x0c |
// | MP_EXT (ext 8) | length | MP_DECIMAL | scale | 1 | 0 (plus) |
// | 0xc7 | 0x03 | 0x01 | 0x24 | 0x01 | 0x0c |
//
// See also:
//
// * MessagePack extensions https://www.tarantool.io/en/doc/latest/dev_guide/internals/msgpack_extensions/
//
// * An implementation in C language https://github.com/tarantool/decNumber/blob/master/decPacked.c
package decimal

import (
"fmt"
Expand Down
19 changes: 11 additions & 8 deletions decimal/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewDecimalFromString(src string) (result *Decimal, err error) {
return
}

// MarshalMsgpack serializes the Decimal into a MessagePack representation.
func (decNum *Decimal) MarshalMsgpack() ([]byte, error) {
one := decimal.NewFromInt(1)
maxSupportedDecimal := decimal.New(1, DecimalPrecision).Sub(one) // 10^DecimalPrecision - 1
Expand All @@ -74,15 +75,17 @@ func (decNum *Decimal) MarshalMsgpack() ([]byte, error) {
return bcdBuf, nil
}

// Decimal values can be encoded to fixext MessagePack, where buffer
// has a fixed length encoded by first byte, and ext MessagePack, where
// buffer length is not fixed and encoded by a number in a separate
// field:
//
// +--------+-------------------+------------+===============+
// | MP_EXT | length (optional) | MP_DECIMAL | PackedDecimal |
// +--------+-------------------+------------+===============+
// UnmarshalMsgpack deserializes a Decimal value from a MessagePack
// representation.
func (decNum *Decimal) UnmarshalMsgpack(b []byte) error {
// Decimal values can be encoded to fixext MessagePack, where buffer
// has a fixed length encoded by first byte, and ext MessagePack, where
// buffer length is not fixed and encoded by a number in a separate
// field:
//
// +--------+-------------------+------------+===============+
// | MP_EXT | length (optional) | MP_DECIMAL | PackedDecimal |
// +--------+-------------------+------------+===============+
digits, err := decodeStringFromBCD(b)
if err != nil {
return fmt.Errorf("msgpack: can't decode string from BCD buffer (%x): %w", b, err)
Expand Down