feat: Add Template Drafter for org.accordproject.money@1.0.0.PreciseAmount - #173
feat: Add Template Drafter for org.accordproject.money@1.0.0.PreciseAmount#173ujjwalv01 wants to merge 2 commits into
org.accordproject.money@1.0.0.PreciseAmount#173Conversation
33b3e42 to
32a755a
Compare
|
Hey @ujjwalv01 , thanks for this pr. I added a few comments. |
There was a problem hiding this comment.
Pull request overview
Adds drafting support for the new org.accordproject.money@1.0.0.PreciseAmount type so TemplateMark variables render as human-readable monetary strings instead of raw serialized objects.
Changes:
- Introduces a
PreciseAmountdrafter with default rendering and optional format-string rendering. - Registers the new drafter in the drafting registry (
getDrafter). - Adds unit tests and a new TemplateMark snapshot entry for
preciseamount.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/drafting/PreciseAmount/index.ts | New PreciseAmount drafting implementation (default + format-string modes). |
| src/drafting/index.ts | Registers the PreciseAmount drafter in getDrafter(). |
| test/PreciseAmountDrafter.test.ts | Adds unit tests for PreciseAmount drafting. |
| test/snapshots/TemplateMarkInterpreter.test.ts.snap | Adds snapshot output for a preciseamount template rendering case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
fed727e to
6c8e134
Compare
Signed-off-by: Ujjwal Verma <ujjwalverma010305@gmail.com>
6c8e134 to
03eb473
Compare
Signed-off-by: Ujjwal Verma <ujjwalverma010305@gmail.com>
|
Thanks for the detailed Copilot review! Here's a summary of what was addressed: Suggestion 1 : - Zero-padding test coverage Suggestion 2 - Missing fixture files Suggestion 3 - Additionally, two more test cases were proactively added:
All 8 unit tests pass. |
|
Thanks for iterating on the Copilot feedback — the default-rendering path is solid, and the string-based 1. Version dispatch — the drafter is pinned to an exact version (highest priority)
case 'org.accordproject.money@1.0.0.PreciseAmount': return preciseAmountDrafter;The moment the money model ships a Rather than pinning to import { ModelUtil } from '@accordproject/concerto-core';
function drafterKey(fqn: string): string {
if (ModelUtil.isPrimitiveType(fqn)) {
return fqn; // Boolean, String, Integer, ...
}
const ns = ModelUtil.getNamespace(fqn); // org.accordproject.money@1.1.0
const name = ModelUtil.getShortName(fqn); // PreciseAmount
const { name: nsName, version } = ModelUtil.parseNamespace(ns);
const major = version ? version.split('.')[0] : '';
return `${nsName}@${major}.${name}`; // org.accordproject.money@1.PreciseAmount
}The cases then become version-line keys — 2. Format path still round-trips through
3. Duplication with
4. Minor: trailing whitespace inside Generated by Claude Code |
Closes #172
Description
This PR introduces a dedicated template drafter for the new
org.accordproject.money@1.0.0.PreciseAmounttype, allowing templates to render arbitrary-precision monetary amounts as human-readable text.The
PreciseAmountmodel separates the value into an exactBigIntegerstring (unscaledValue) and a decimalscale(carried onunit) to avoid IEEE-754 double-precision drift. This drafter correctly reconstructs the decimal string representation directly from theunscaledValuewithout ever converting to a JavaScriptNumberorFloatduring the default rendering phase, ensuring that precision is fully maintained at any magnitude.Key Implementation Details
1. Arbitrary-Precision String Reconstruction
A helper function
reconstructDecimalStringwas introduced. It uses pure string manipulation to insert the decimal point based on theunit.scale, safely handling leading zeroes and negative values without precision loss.2. Formatting Support
The drafter supports two modes:
reconstructDecimalString(...) + ' ' + unit.code(e.g.1234.00 USD).{{payment as "K 0,0.00 CCC"}}, the drafter re-uses the existingdraftDoubleFormatutility to replaceK(currency symbol) andCCC(currency code) tokens, ensuring backward compatibility with existing monetary formatting conventions.3. Drafter Registration
The new drafter is registered in
src/drafting/index.ts:Testing
test/PreciseAmountDrafter.test.tscovering:unscaledValuesscale = 0(no decimal point)test/templates/good/preciseamount/containing amodel.cto,data.json, andtemplate.mdwhich successfully passed snapshot tests viaTemplateMarkInterpreter.Files Changed
src/drafting/PreciseAmount/index.ts(new) — drafter implementationsrc/drafting/index.ts(modified) — drafter registrationtest/PreciseAmountDrafter.test.ts(new) — unit teststest/templates/good/preciseamount/(new) — E2E fixture (model.cto,data.json,template.md)