Documentation
¶
Overview ¶
Package types provides common types used across Ledger.
Package types provides common types used across Ledger.
Index ¶
- Variables
- type Entity
- type Money
- func (m Money) Abs() Money
- func (m Money) Add(other Money) Money
- func (m Money) CheckedAdd(other Money) (Money, error)
- func (m Money) CheckedMultiply(qty int64) (Money, error)
- func (m Money) CheckedPercent(pct int) (Money, error)
- func (m Money) CheckedSubtract(other Money) (Money, error)
- func (m Money) Divide(divisor int64) Money
- func (m Money) Equal(other Money) bool
- func (m Money) FormatMajor() string
- func (m Money) GreaterThan(other Money) bool
- func (m Money) IsNegative() bool
- func (m Money) IsPositive() bool
- func (m Money) IsZero() bool
- func (m Money) LessThan(other Money) bool
- func (m Money) MarshalJSON() ([]byte, error)
- func (m Money) Max(other Money) Money
- func (m Money) Min(other Money) Money
- func (m Money) Multiply(qty int64) Money
- func (m Money) Negate() Money
- func (m Money) Percent(pct int) Money
- func (m Money) String() string
- func (m Money) Subtract(other Money) Money
Constants ¶
This section is empty.
Variables ¶
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 (Entity) IsStale ¶
IsStale returns true if the entity hasn't been updated in the specified duration.
func (Entity) LastModified ¶
LastModified returns how long ago the entity was last updated.
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 (Money) CheckedAdd ¶
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 ¶
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 ¶
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 ¶
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) FormatMajor ¶
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 ¶
GreaterThan returns true if this Money is greater than other. Panics if currencies don't match.
func (Money) IsNegative ¶
IsNegative returns true if the amount is less than zero.
func (Money) IsPositive ¶
IsPositive returns true if the amount is greater than zero.
func (Money) LessThan ¶
LessThan returns true if this Money is less than other. Panics if currencies don't match.
func (Money) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (Money) Percent ¶
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.