currencyx

package
v1.0.0-beta.229 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AmountAllocation

type AmountAllocation[T any] struct {
	Key    T
	Amount alpacadecimal.Decimal
}

AmountAllocation is the allocated currency amount for one key.

func AllocateByAmount

func AllocateByAmount[T any](calculator Calculator, input AmountAllocationInput[T]) ([]AmountAllocation[T], error)

AllocateByAmount allocates a currency amount across currency amount buckets using the largest remainder quota method. Each item amount is both its proportional weight and its allocation cap.

type AmountAllocationInput

type AmountAllocationInput[T any] struct {
	Amount alpacadecimal.Decimal
	Items  []AmountAllocationItem[T]

	// CompareKey is used as a deterministic tie-breaker when two buckets have
	// the same fractional remainder. If nil, the original item order is used.
	CompareKey func(left, right T) int
}

AmountAllocationInput defines a proportional allocation across currency amount buckets.

type AmountAllocationItem

type AmountAllocationItem[T any] struct {
	Key    T
	Amount alpacadecimal.Decimal
}

AmountAllocationItem defines one currency amount bucket that can receive a proportional allocation. The amount is both the allocation weight and the maximum amount that can be allocated to the key.

type Calculator

type Calculator struct {
	Currency Code
	Def      *currency.Def
}

TODO: Better name?!

func (Calculator) IsRoundedToPrecision

func (c Calculator) IsRoundedToPrecision(amount alpacadecimal.Decimal) bool

func (Calculator) RoundToPrecision

func (c Calculator) RoundToPrecision(amount alpacadecimal.Decimal) alpacadecimal.Decimal

func (Calculator) Validate

func (c Calculator) Validate() error

type Code

type Code currency.Code

Currency represents a currency code. Three-letter [ISO4217](https://www.iso.org/iso-4217-currency-codes.html) currency code.

func (Code) Calculator

func (c Code) Calculator() (Calculator, error)

Calculator provides a currency calculator object. This allows us to not to resolve def a lot of times, plus we can assume that def is always valid, thus we can avoid a lot of error handling.

func (Code) Validate

func (c Code) Validate() error

type WeightedAllocation

type WeightedAllocation[T any] struct {
	Key    T
	Amount alpacadecimal.Decimal
}

WeightedAllocation is the allocated currency amount for one key.

func AllocateByWeight

func AllocateByWeight[T any](calculator Calculator, input WeightedAllocationInput[T]) ([]WeightedAllocation[T], error)

AllocateByWeight allocates a currency amount across keys using their weights and the largest remainder quota method at the currency precision.

type WeightedAllocationInput

type WeightedAllocationInput[T any] struct {
	Amount alpacadecimal.Decimal
	Items  []WeightedAllocationItem[T]

	// CompareKey is used as a deterministic tie-breaker when two items have
	// the same fractional remainder. If nil, the original item order is used.
	CompareKey func(left, right T) int
}

WeightedAllocationInput defines a proportional currency allocation.

type WeightedAllocationItem

type WeightedAllocationItem[T any] struct {
	Key    T
	Weight alpacadecimal.Decimal
}

WeightedAllocationItem defines one key that can receive a proportional allocation from a currency amount. Weight is dimensionless; it does not need to be a currency amount.

Jump to

Keyboard shortcuts

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