types

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package types provides common types used across Ledger.

Package types provides common types used across Ledger.

Index

Constants

This section is empty.

Variables

View Source
var ErrOverflow = errors.New("types: money arithmetic overflow")

ErrOverflow is wrapped by every checked Money operation whose exact result does not fit in an int64. A wrapped amount must never become an invoice.

Functions

This section is empty.

Types

type Entity

type Entity struct {
	CreatedAt time.Time `json:"created_at" bun:"created_at,notnull,default:current_timestamp"`
	UpdatedAt time.Time `json:"updated_at" bun:"updated_at,notnull,default:current_timestamp"`
}

Entity is the base type for all Ledger entities with timestamps. Embed this in your domain types to get automatic timestamp handling.

func NewEntity

func NewEntity() Entity

NewEntity creates a new Entity with current timestamps.

func (Entity) Age

func (e Entity) Age() time.Duration

Age returns how long ago the entity was created.

func (Entity) IsNew

func (e Entity) IsNew() bool

IsNew returns true if the entity was created within the last minute.

func (Entity) IsStale

func (e Entity) IsStale(staleDuration time.Duration) bool

IsStale returns true if the entity hasn't been updated in the specified duration.

func (Entity) LastModified

func (e Entity) LastModified() time.Duration

LastModified returns how long ago the entity was last updated.

func (*Entity) Touch

func (e *Entity) Touch()

Touch updates the UpdatedAt timestamp to now.

type Money

type Money struct {
	Amount   int64  `json:"amount"`   // Smallest unit (cents, pence, etc)
	Currency string `json:"currency"` // ISO 4217 lowercase: "usd", "eur", "gbp"
}

Money represents a monetary value in the smallest currency unit. All arithmetic is integer-only — no floating point.

Examples:

  • USD(4900) = $49.00 (4900 cents)
  • EUR(19900) = €199.00 (19900 cents)
  • GBP(9900) = £99.00 (9900 pence)

func AUD

func AUD(cents int64) Money

AUD creates a Money value in Australian Dollars (cents).

func CAD

func CAD(cents int64) Money

CAD creates a Money value in Canadian Dollars (cents).

func EUR

func EUR(cents int64) Money

EUR creates a Money value in Euros (cents).

func GBP

func GBP(pence int64) Money

GBP creates a Money value in British Pounds (pence).

func JPY

func JPY(yen int64) Money

JPY creates a Money value in Japanese Yen (no decimal).

func Sum

func Sum(values ...Money) Money

Sum calculates the sum of multiple Money values. All must have the same currency.

func USD

func USD(cents int64) Money

USD creates a Money value in US Dollars (cents).

func Zero

func Zero(currency string) Money

Zero returns a zero Money value in the specified currency.

func (Money) Abs

func (m Money) Abs() Money

Abs returns the absolute value.

func (Money) Add

func (m Money) Add(other Money) Money

Add adds two Money values. Panics if currencies don't match.

func (Money) CheckedAdd

func (m Money) CheckedAdd(other Money) (Money, error)

CheckedAdd adds two Money values. It returns an error wrapping ErrOverflow when the exact sum does not fit in an int64, and an error (not a panic) when the currencies differ. Add and Subtract keep their panicking behaviour for callers that have already ruled both out.

func (Money) CheckedMultiply

func (m Money) CheckedMultiply(qty int64) (Money, error)

CheckedMultiply multiplies the Money by a quantity. It returns an error wrapping ErrOverflow when the exact product does not fit in an int64.

func (Money) CheckedPercent

func (m Money) CheckedPercent(pct int) (Money, error)

CheckedPercent returns pct percent of the Money value, truncating toward zero exactly as Percent does. It returns an error wrapping ErrOverflow when Amount*pct does not fit in an int64. Dividing the product by 100 cannot overflow.

func (Money) CheckedSubtract

func (m Money) CheckedSubtract(other Money) (Money, error)

CheckedSubtract subtracts another Money value. It returns an error wrapping ErrOverflow when the exact difference does not fit in an int64, and an error (not a panic) when the currencies differ.

func (Money) Divide

func (m Money) Divide(divisor int64) Money

Divide divides the Money by a divisor. Uses integer division.

func (Money) Equal

func (m Money) Equal(other Money) bool

Equal returns true if both Money values are equal (same amount and currency).

func (Money) FormatMajor

func (m Money) FormatMajor() string

FormatMajor returns the major unit string without currency symbol. For currencies with 2 decimal places: "49.00" for USD(4900). For currencies with 0 decimal places (JPY): "100" for JPY(100).

func (Money) GreaterThan

func (m Money) GreaterThan(other Money) bool

GreaterThan returns true if this Money is greater than other. Panics if currencies don't match.

func (Money) IsNegative

func (m Money) IsNegative() bool

IsNegative returns true if the amount is less than zero.

func (Money) IsPositive

func (m Money) IsPositive() bool

IsPositive returns true if the amount is greater than zero.

func (Money) IsZero

func (m Money) IsZero() bool

IsZero returns true if the amount is zero.

func (Money) LessThan

func (m Money) LessThan(other Money) bool

LessThan returns true if this Money is less than other. Panics if currencies don't match.

func (Money) MarshalJSON

func (m Money) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Money) Max

func (m Money) Max(other Money) Money

Max returns the larger of two Money values. Panics if currencies don't match.

func (Money) Min

func (m Money) Min(other Money) Money

Min returns the smaller of two Money values. Panics if currencies don't match.

func (Money) Multiply

func (m Money) Multiply(qty int64) Money

Multiply multiplies the Money by a quantity.

func (Money) Negate

func (m Money) Negate() Money

Negate returns the negative of the Money value.

func (Money) Percent

func (m Money) Percent(pct int) Money

Percent returns pct percent of the Money value.

Integer-only, truncating toward zero: 10% of 101 cents is 10 cents, and 10% of -101 cents is -10 cents, not -11. Magnitude is never rounded up, whatever the sign. Callers needing the remainder must compute it.

func (Money) String

func (m Money) String() string

String returns a human-readable string with currency symbol. Examples: "$49.00", "€199.00", "£99.00", "¥100"

func (Money) Subtract

func (m Money) Subtract(other Money) Money

Subtract subtracts another Money value. Panics if currencies don't match.

Jump to

Keyboard shortcuts

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