decimal

package
v3.0.0-...-40211c2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2026 License: BSD-2-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package decimal with support of Tarantool's decimal data type.

Decimal data type supported in Tarantool since 2.2.

Since: 1.7.0

See also:

Example

To enable support of decimal in msgpack with https://github.com/shopspring/decimal, import tarantool/decimal submodule.

server := "127.0.0.1:3013"
dialer := tarantool.NetDialer{
	Address:  server,
	User:     "test",
	Password: "test",
}
opts := tarantool.Opts{
	Timeout: 5 * time.Second,
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
client, err := tarantool.Connect(ctx, dialer, opts)
cancel()
if err != nil {
	log.Fatalf("Failed to connect: %s", err.Error())
}

spaceNo := uint32(524)

number, err := MakeDecimalFromString("-22.804")
if err != nil {
	log.Fatalf("Failed to prepare test decimal: %s", err)
}

data, err := client.Do(tarantool.NewReplaceRequest(spaceNo).
	Tuple([]interface{}{number}),
).Get()
if err != nil {
	log.Fatalf("Decimal replace failed: %s", err)
}

log.Println("Decimal tuple replace")
log.Println("Error", err)
log.Println("Data", data)

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDecimalOverflow = fmt.Errorf("msgpack: decimal number is bigger than"+
		" maximum supported number (10^%d - 1)", decimalPrecision)
	ErrDecimalUnderflow = fmt.Errorf("msgpack: decimal number is lesser than"+
		" minimum supported number (-10^%d - 1)", decimalPrecision)
)

Functions

This section is empty.

Types

type Decimal

type Decimal struct {
	decimal.Decimal
}

func MakeDecimal

func MakeDecimal(decimal decimal.Decimal) Decimal

MakeDecimal creates a new Decimal from a decimal.Decimal.

func MakeDecimalFromString

func MakeDecimalFromString(src string) (Decimal, error)

MakeDecimalFromString creates a new Decimal from a string.

func (Decimal) MarshalMsgpack

func (d Decimal) MarshalMsgpack() ([]byte, error)

MarshalMsgpack implements a custom msgpack marshaler.

func (*Decimal) UnmarshalMsgpack

func (d *Decimal) UnmarshalMsgpack(data []byte) error

UnmarshalMsgpack implements a custom msgpack unmarshaler.

type OptionalDecimal

type OptionalDecimal struct {
	// contains filtered or unexported fields
}

OptionalDecimal represents an optional value of type Decimal. It can either hold a valid Decimal (IsSome == true) or be empty (IsZero == true).

func NoneOptionalDecimal

func NoneOptionalDecimal() OptionalDecimal

NoneOptionalDecimal creates an empty optional OptionalDecimal value. The returned OptionalDecimal will have IsSome() == false and IsZero() == true.

Example:

o := NoneOptionalDecimal()
if o.IsZero() {
    fmt.Println("value is absent")
}

func SomeOptionalDecimal

func SomeOptionalDecimal(value Decimal) OptionalDecimal

SomeOptionalDecimal creates an optional OptionalDecimal with the given Decimal value. The returned OptionalDecimal will have IsSome() == true and IsZero() == false.

func (*OptionalDecimal) DecodeMsgpack

func (o *OptionalDecimal) DecodeMsgpack(decoder *msgpack.Decoder) error

DecodeMsgpack decodes a OptionalDecimal value from MessagePack format. Supports two input types:

  • nil: interpreted as no value (NoneOptionalDecimal)
  • Decimal: interpreted as a present value (SomeOptionalDecimal)

Returns an error if the input type is unsupported or decoding fails.

After successful decoding:

  • on nil: exists = false, value = default zero value
  • on Decimal: exists = true, value = decoded value

func (OptionalDecimal) EncodeMsgpack

func (o OptionalDecimal) EncodeMsgpack(encoder *msgpack.Encoder) error

EncodeMsgpack encodes the OptionalDecimal value using MessagePack format. - If the value is present, it is encoded as Decimal. - If the value is absent (None), it is encoded as nil.

Returns an error if encoding fails.

func (OptionalDecimal) Get

func (o OptionalDecimal) Get() (Decimal, bool)

Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of Decimal, false).

Recommended usage:

	if value, ok := o.Get(); ok {
     // use value
	}

func (OptionalDecimal) IsNil

func (o OptionalDecimal) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (OptionalDecimal) IsSome

func (o OptionalDecimal) IsSome() bool

IsSome returns true if the OptionalDecimal contains a value. This indicates the value is explicitly set (not None).

func (OptionalDecimal) IsZero

func (o OptionalDecimal) IsZero() bool

IsZero returns true if the OptionalDecimal does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.

func (OptionalDecimal) MustGet

func (o OptionalDecimal) MustGet() Decimal

MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).

Use with caution — only when you are certain the value exists.

Panics with: "optional value is not set" if no value is set.

func (OptionalDecimal) Unwrap

func (o OptionalDecimal) Unwrap() Decimal

Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for Decimal.

Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.

func (OptionalDecimal) UnwrapOr

func (o OptionalDecimal) UnwrapOr(defaultValue Decimal) Decimal

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneOptionalDecimal()
v := o.UnwrapOr(someDefaultOptionalDecimal)

func (OptionalDecimal) UnwrapOrElse

func (o OptionalDecimal) UnwrapOrElse(defaultValue func() Decimal) Decimal

UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.

Example:

o := NoneOptionalDecimal()
v := o.UnwrapOrElse(func() Decimal { return computeDefault() })

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL