Skip to content

Generate exact Decimal128 and Money fields - #81

Merged
crimson-knight merged 1 commit into
mainfrom
g5/decimal128-money-fields
Nov 27, 2026
Merged

crimson-knight merged 1 commit into
mainfrom
g5/decimal128-money-fields

Conversation

@crimson-knight

Copy link
Copy Markdown
Member

Relay G5 (zero-native-library), amber_cli item. Draft, do not merge yet: it generates code against Grant's Grant::Decimal128 / Grant::Money columns, which are being built in parallel in crimson-knight/grant (g5/decimal128-money-columns). The relay merges after an end-to-end app test against both.

What changes

A generated decimal field used to be Float64 over DECIMAL(10,2) and could not be read back on PostgreSQL (ColumnTypeMismatchError) or on SQLite for whole values.

Field Model pg mysql sqlite
price:decimal column price : Grant::Decimal128?, precision: 18, scale: 4 NUMERIC(18,4) DECIMAL(18,4) INTEGER
'price:decimal{38,10}' ..., precision: 38, scale: 10 NUMERIC(38,10) DECIMAL(38,10) TEXT
total:money column total : Grant::Money?, precision: 18, scale: 4 + total_currency VARCHAR(3) + VARCHAR(3) + TEXT
total:money{USD} / 'total:money{18,2,USD}' ..., currency: "USD" no currency column

Precision 1..38, scale 0..precision; out-of-range options stop the generator with a message. Braces with a comma must be quoted (the shell expands them); an expanded price:decimal38 gets a hint saying so.

  • Schemas declare amounts as String and a method validator answers 422 for text that is not an exact decimal or an ISO 4217 code.
  • Scaffold controllers read the posted form text (Amber's form parser turns 19.99 into Float64, which keeps only about 15 digits); API controllers read the JSON string.
  • Forms: number input with step from the scale, plus a currency input for money.
  • Generated specs use exact strings ("19.99") and a round-trip check.

Verification

  • crystal-alpha spec: 856 examples, 4 failures, 5 errors. All 9 are amber-lsp binary/stdlib lookup specs that fail identically on unmodified main locally (no built amber-lsp binary).
  • A fresh amber new app with scaffold Product price:decimal total:money name:string and api Invoice 'amount:decimal{38,10}:required' 'fee:money{EUR}' 'total:money{18,2}:required', built against Amber 2.0.0-beta.16 and a snapshot of the in-progress Grant branch: type-checks, and its 19 generated specs pass on SQLite. Scratch specs also showed 18 digits through the HTML form, 38 digits through the JSON API, and 422 for price=abc.

A generated decimal field was a Float64 column over DECIMAL(10,2), which
could not be read back on PostgreSQL (ColumnTypeMismatchError) or on
SQLite for whole values. decimal now generates an exact Grant::Decimal128
column and the new money type a Grant::Money column.

- price:decimal -> column price : Grant::Decimal128?, precision: 18, scale: 4;
  'price:decimal{38,10}' sets precision (1..38) and scale (0..precision).
- total:money -> column total : Grant::Money?, precision: 18, scale: 4 plus a
  total_currency column; total:money{USD} or 'total:money{18,2,USD}' fixes the
  currency (currency: "USD") with no companion column.
- Migrations match Grant's storage: pg NUMERIC(p,s), mysql DECIMAL(p,s),
  sqlite INTEGER up to 18 digits and TEXT from 19 to 38; currency VARCHAR(3)
  (TEXT on sqlite).
- Schemas take amounts as String and check them with a method validator
  (422 for text that is not an exact decimal or ISO 4217 code). Scaffolds
  read the posted form text, since Amber's form parser reads numbers as
  Float64; APIs read the JSON string.
- Forms use a number input stepped to the scale plus a currency input;
  generated specs use exact strings such as "19.99" and check the round trip.
- Help text and documentation list the new syntax.
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