Skip to content

Add runtime rates and registry-aware decoding for AnyMoney - #16

Merged
khatibomar merged 4 commits into
mainfrom
runtime-rates-and-registries
Oct 10, 2026
Merged

khatibomar merged 4 commits into
mainfrom
runtime-rates-and-registries

Conversation

@khatibomar

Copy link
Copy Markdown
Owner

Fix two errors in docs/rounding-and-overflow.md: the product uses 192 bits, and ErrZeroDenominator does not exist.
Add AnyRate and AnyMoney.Convert, so AnyMoney converts with a rate whose currencies are known only at run time.
Add ParseAnyMoneyIn, UnmarshalJSONIn and ScanColumnsIn, which find the currency code in a given registry.

The rounding page said that the product amount × n uses 128 bits. It
uses 192 bits, so it cannot overflow before the division.

The page also named ErrZeroDenominator, which does not exist. Div and
NewFactor return ErrDivisionByZero for a zero divisor.

Signed-off-by: ayn <ayn.khatib@gmail.com>
Convert works only with Money[T] and Rate[Base, Quote], so a program
that gets the currencies of a rate at run time, for example from a rate
feed, could not convert an AnyMoney.

AnyRate holds the base and the quote currencies with the exact rate.
NewAnyRate and ParseAnyRate use the same rules as NewRate and ParseRate.
AnyMoney.Convert returns ErrCurrencyMismatch if the value is not in the
base currency of the rate. Rate.Any and AsRate change between the two
rate types.

Signed-off-by: ayn <ayn.khatib@gmail.com>
ParseAnyMoney, AnyMoney.UnmarshalJSON and AnyMoney.ScanColumns find the
currency code only in currency.Default(). A program that keeps custom
currencies in a separate registry, for example for each tenant, had to
decode the code and the amount itself.

ParseAnyMoneyIn, UnmarshalJSONIn and ScanColumnsIn take the registry.
They return ErrUnknownCurrency if the registry is nil or does not have
the code. The functions without a registry use currency.Default(), as
before.

Signed-off-by: ayn <ayn.khatib@gmail.com>
Nothing calls int128.mulPow10 since Money.Scan stopped accepting int64,
so the lint job fails with "func int128.mulPow10 is unused".

Signed-off-by: ayn <ayn.khatib@gmail.com>
@khatibomar
khatibomar merged commit d3831d7 into main Oct 10, 2026
7 checks passed
@khatibomar
khatibomar deleted the runtime-rates-and-registries branch October 10, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant