Documentation
¶
Index ¶
- Constants
- type AmountAllocation
- type AmountAllocationInput
- type AmountAllocationItem
- type Code
- type CostBasis
- type Currency
- type CurrencyBuilder
- func (b *CurrencyBuilder) Build() (Currency, error)
- func (b *CurrencyBuilder) WithCode(code Code) *CurrencyBuilder
- func (b *CurrencyBuilder) WithDecimalMark(decimalMark string) *CurrencyBuilder
- func (b *CurrencyBuilder) WithName(name string) *CurrencyBuilder
- func (b *CurrencyBuilder) WithPrecision(precision uint32) *CurrencyBuilder
- func (b *CurrencyBuilder) WithSymbol(symbol string) *CurrencyBuilder
- func (b *CurrencyBuilder) WithThousandsSeparator(thousandsSeparator string) *CurrencyBuilder
- type CurrencyCalculator
- type CurrencyDetails
- type CurrencyFormatter
- type CurrencyType
- type CustomCurrency
- func (c *CustomCurrency) AsCustom() (*CustomCurrency, error)
- func (c *CustomCurrency) AsFiat() (*FiatCurrency, error)
- func (c *CustomCurrency) Definition() *currency.Def
- func (c *CustomCurrency) Details() CurrencyDetails
- func (c *CustomCurrency) FormatAmount(amount alpacadecimal.Decimal) string
- func (c *CustomCurrency) IsRoundedToPrecision(amount alpacadecimal.Decimal) bool
- func (c *CustomCurrency) RoundDown(amount alpacadecimal.Decimal) alpacadecimal.Decimal
- func (c *CustomCurrency) RoundToPrecision(amount alpacadecimal.Decimal) alpacadecimal.Decimal
- func (c *CustomCurrency) RoundUp(amount alpacadecimal.Decimal) alpacadecimal.Decimal
- func (c *CustomCurrency) Type() CurrencyType
- func (c *CustomCurrency) Unit() alpacadecimal.Decimal
- func (c *CustomCurrency) Validate() error
- func (c *CustomCurrency) ValidateWith(v ...models.ValidatorFunc[Currency]) error
- type FiatCode
- type FiatCurrency
- func (f *FiatCurrency) AsCustom() (*CustomCurrency, error)
- func (f *FiatCurrency) AsFiat() (*FiatCurrency, error)
- func (f *FiatCurrency) Definition() *currency.Def
- func (f *FiatCurrency) Details() CurrencyDetails
- func (f *FiatCurrency) FormatAmount(amount alpacadecimal.Decimal) string
- func (f *FiatCurrency) GetFiatCode() FiatCode
- func (f *FiatCurrency) IsRoundedToPrecision(amount alpacadecimal.Decimal) bool
- func (f *FiatCurrency) RoundDown(amount alpacadecimal.Decimal) alpacadecimal.Decimal
- func (f *FiatCurrency) RoundToPrecision(amount alpacadecimal.Decimal) alpacadecimal.Decimal
- func (f *FiatCurrency) RoundUp(amount alpacadecimal.Decimal) alpacadecimal.Decimal
- func (f *FiatCurrency) Type() CurrencyType
- func (f *FiatCurrency) Unit() alpacadecimal.Decimal
- func (f *FiatCurrency) Validate() error
- func (f *FiatCurrency) ValidateWith(v ...models.ValidatorFunc[Currency]) error
- type WeightedAllocation
- type WeightedAllocationInput
- type WeightedAllocationItem
Constants ¶
const ( CustomCurrencyCodeMinLength = 4 CustomCurrencyCodeMaxLength = 24 )
const CustomCurrencyMaxPrecision uint32 = 12
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](currency Currency, 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 Code ¶
Code represents a fiat or custom currency code. Code values used directly as Currency values are treated as fiat currencies for backwards compatibility.
func (Code) Type ¶
func (c Code) Type() CurrencyType
type CostBasis ¶
type CostBasis struct {
// FiatCode is the target fiat currency code (e.g. USD, EUR) that the rate converts to.
FiatCode Code `json:"fiat_code"`
// Rate is the exchange rate: one unit of the custom currency equals this many units of FiatCode.
Rate alpacadecimal.Decimal `json:"rate"`
// EffectiveFrom is the start of the period during which this rate applies (inclusive).
EffectiveFrom time.Time `json:"effective_from"`
// EffectiveTo is the end of the period during which this rate applies (exclusive).
// Nil means the rate is open-ended and currently active.
EffectiveTo *time.Time `json:"effective_to,omitempty"`
}
CostBasis defines the exchange rate from a custom currency to a fiat currency over a specific time period. It is used to convert custom currency amounts (e.g. credits, tokens) into monetary values for billing and invoicing.
type Currency ¶
type Currency interface {
models.Validator
CurrencyCalculator
CurrencyFormatter
Type() CurrencyType
Details() CurrencyDetails
AsFiat() (*FiatCurrency, error)
AsCustom() (*CustomCurrency, error)
Definition() *currency.Def
}
type CurrencyBuilder ¶
type CurrencyBuilder struct {
// contains filtered or unexported fields
}
func NewCurrencyBuilder ¶
func NewCurrencyBuilder(currencyType CurrencyType) *CurrencyBuilder
func (*CurrencyBuilder) Build ¶
func (b *CurrencyBuilder) Build() (Currency, error)
func (*CurrencyBuilder) WithCode ¶
func (b *CurrencyBuilder) WithCode(code Code) *CurrencyBuilder
func (*CurrencyBuilder) WithDecimalMark ¶
func (b *CurrencyBuilder) WithDecimalMark(decimalMark string) *CurrencyBuilder
func (*CurrencyBuilder) WithName ¶
func (b *CurrencyBuilder) WithName(name string) *CurrencyBuilder
func (*CurrencyBuilder) WithPrecision ¶
func (b *CurrencyBuilder) WithPrecision(precision uint32) *CurrencyBuilder
func (*CurrencyBuilder) WithSymbol ¶
func (b *CurrencyBuilder) WithSymbol(symbol string) *CurrencyBuilder
func (*CurrencyBuilder) WithThousandsSeparator ¶
func (b *CurrencyBuilder) WithThousandsSeparator(thousandsSeparator string) *CurrencyBuilder
type CurrencyCalculator ¶
type CurrencyCalculator interface {
RoundToPrecision(amount alpacadecimal.Decimal) alpacadecimal.Decimal
IsRoundedToPrecision(amount alpacadecimal.Decimal) bool
RoundUp(amount alpacadecimal.Decimal) alpacadecimal.Decimal
RoundDown(amount alpacadecimal.Decimal) alpacadecimal.Decimal
Unit() alpacadecimal.Decimal
}
type CurrencyDetails ¶
type CurrencyFormatter ¶
type CurrencyFormatter interface {
FormatAmount(amount alpacadecimal.Decimal) string
}
type CurrencyType ¶
type CurrencyType string
const ( CurrencyTypeFiat CurrencyType = "fiat" CurrencyTypeCustom CurrencyType = "custom" )
func (CurrencyType) Validate ¶
func (t CurrencyType) Validate() error
type CustomCurrency ¶
type CustomCurrency struct {
// contains filtered or unexported fields
}
func (*CustomCurrency) AsCustom ¶
func (c *CustomCurrency) AsCustom() (*CustomCurrency, error)
func (*CustomCurrency) AsFiat ¶
func (c *CustomCurrency) AsFiat() (*FiatCurrency, error)
func (*CustomCurrency) Definition ¶
func (c *CustomCurrency) Definition() *currency.Def
func (*CustomCurrency) Details ¶
func (c *CustomCurrency) Details() CurrencyDetails
func (*CustomCurrency) FormatAmount ¶
func (c *CustomCurrency) FormatAmount(amount alpacadecimal.Decimal) string
func (*CustomCurrency) IsRoundedToPrecision ¶
func (c *CustomCurrency) IsRoundedToPrecision(amount alpacadecimal.Decimal) bool
func (*CustomCurrency) RoundDown ¶
func (c *CustomCurrency) RoundDown(amount alpacadecimal.Decimal) alpacadecimal.Decimal
func (*CustomCurrency) RoundToPrecision ¶
func (c *CustomCurrency) RoundToPrecision(amount alpacadecimal.Decimal) alpacadecimal.Decimal
func (*CustomCurrency) RoundUp ¶
func (c *CustomCurrency) RoundUp(amount alpacadecimal.Decimal) alpacadecimal.Decimal
func (*CustomCurrency) Type ¶
func (c *CustomCurrency) Type() CurrencyType
func (*CustomCurrency) Unit ¶
func (c *CustomCurrency) Unit() alpacadecimal.Decimal
func (*CustomCurrency) Validate ¶
func (c *CustomCurrency) Validate() error
func (*CustomCurrency) ValidateWith ¶
func (c *CustomCurrency) ValidateWith(v ...models.ValidatorFunc[Currency]) error
type FiatCode ¶
func (FiatCode) AsFiatCurrency ¶
func (c FiatCode) AsFiatCurrency() (*FiatCurrency, error)
type FiatCurrency ¶
type FiatCurrency struct {
// contains filtered or unexported fields
}
func NewFiatCurrency ¶
func NewFiatCurrency[T ~string](code T) (*FiatCurrency, error)
func (*FiatCurrency) AsCustom ¶
func (f *FiatCurrency) AsCustom() (*CustomCurrency, error)
func (*FiatCurrency) AsFiat ¶
func (f *FiatCurrency) AsFiat() (*FiatCurrency, error)
func (*FiatCurrency) Definition ¶
func (f *FiatCurrency) Definition() *currency.Def
func (*FiatCurrency) Details ¶
func (f *FiatCurrency) Details() CurrencyDetails
func (*FiatCurrency) FormatAmount ¶
func (f *FiatCurrency) FormatAmount(amount alpacadecimal.Decimal) string
func (*FiatCurrency) GetFiatCode ¶
func (f *FiatCurrency) GetFiatCode() FiatCode
func (*FiatCurrency) IsRoundedToPrecision ¶
func (f *FiatCurrency) IsRoundedToPrecision(amount alpacadecimal.Decimal) bool
func (*FiatCurrency) RoundDown ¶
func (f *FiatCurrency) RoundDown(amount alpacadecimal.Decimal) alpacadecimal.Decimal
func (*FiatCurrency) RoundToPrecision ¶
func (f *FiatCurrency) RoundToPrecision(amount alpacadecimal.Decimal) alpacadecimal.Decimal
func (*FiatCurrency) RoundUp ¶
func (f *FiatCurrency) RoundUp(amount alpacadecimal.Decimal) alpacadecimal.Decimal
func (*FiatCurrency) Type ¶
func (f *FiatCurrency) Type() CurrencyType
func (*FiatCurrency) Unit ¶
func (f *FiatCurrency) Unit() alpacadecimal.Decimal
func (*FiatCurrency) Validate ¶
func (f *FiatCurrency) Validate() error
func (*FiatCurrency) ValidateWith ¶
func (f *FiatCurrency) ValidateWith(v ...models.ValidatorFunc[Currency]) 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](currency Currency, 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.