Money: Rename cents to minor units, ISO 4217 exponent-aware formatting - #89
Merged
Conversation
ISO 4217 minor-unit exponents are not always 2 (JPY has 0, BHD has 3). The cents naming invites a divide-by-100 bug the day a non-exponent-2 currency lands in the whitelist. Field, accessor, JSON, wire format, DB column and docs now say minor units, and formatting derives from a per-currency exponent table.
Review follow-ups: migration 000004 now rewrites PriceCents to PriceMinorUnits in unpublished product.created payloads so consumers never read a silent zero price, and Money.UnmarshalJSON fails loudly on the old cents field instead of decoding it as zero.
The migration rewrites all stored payloads, so no old-format JSON can exist. Code stays free of legacy awareness.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
"Cents" is only the correct word for currencies with two decimal places. ISO 4217 defines a minor-unit exponent per currency: JPY, KRW, CLP and ISK have 0 (there is no sub-yen), BHD, JOD, KWD, OMR and TND have 3. A field named
centsinvites whoever adds JPY to the whitelist to divide by 100 and turn ¥5000 into ¥50.This came directly out of reader feedback on the Money article. Changes:
Money.cents→Money.minorUnits, accessorCents()→MinorUnits(). No deprecated alias: clean break, all callers updated.String()formats from the exponent table instead of a hardcoded 100. New test pins JPY-style (exponent 0) and BHD-style (exponent 3) formatting.price_cents→price_minor_units(JSON, DTOs, OpenAPI spec). Breaking API change, intentional — the template has no API consumers to migrate.price_cents→price_minor_units; sqlc regenerated.How to test
The new
TestMoney_String_ExponentAwarecovers the exponent-0 and exponent-3 formatting paths.Checklist
make testpasses (needs Docker)make lintpasses