Repository navigation
[2.x] Add formatters for booleans, bytes, enums and public keys - #1217
lorisleiva merged 1 commit into
Conversation
|
trevor-cortex
left a comment
There was a problem hiding this comment.
Summary
Adds formatBoolean, formatBytes, formatEnum and formatPublicKey to @codama/dynamic-codecs, gives every formatter the uniform (decoded, options) signature, introduces the formatAddress option, and replaces the null return of formatDateTime/formatDuration on bad ticksPerSecond with a new CODAMA_ERROR__INVALID_TICKS_PER_SECOND. getCodecFromBytesEncoding is extracted to bytes.ts so both codecs.ts and format.ts can use it.
The implementation is straightforward and the tests cover the interesting paths (title-cased identifiers vs. string vs. textNode labels, variant data excluded, base58-decoded bytes with leading zeros re-encoded exactly, empty bytes, custom address formatters). Docs and JSDoc are consistent with the new behaviour. Error code 18 follows the precedent set by 17 for dynamic-codecs errors in the general range, and the CodamaErrorCode union / context / messages are all updated.
Things to watch
- Import ordering in
format.ts:getTextNodeContentis inserted afterisNode, which breaks the case-insensitive sort the rest of the file follows.pnpm lint:fixshould sort it out, but worth doing before merge so CI doesn't trip on it. - Changeset: the diff doesn't include one, and this is user-facing for both
@codama/dynamic-codecs(new exports, new option,null→ throw) and@codama/errors(new code). If the2.xbranch still expects per-PR changesets, this needs one (two concerns, so probably two files per CONTRIBUTING). If the seeded major changeset already covers everything on this branch, ignore this. - Behaviour change:
formatDateTime/formatDurationnow throw instead of returningnull. Callers that were checking fornullwill need updating — fine for a major era, just flagging it for anyone reading the changelog later.
Notes for subsequent reviewers
formatBytesalways renders hex, even forutf8-decoded bytes. That's a deliberate and documented choice (README + JSDoc explain the lossy utf8 round-trip), not an oversight.formatPublicKeycastsdecoded.value as AddressbecauseDecodedPublicKeyTypeNodetypes its value asstring. Narrowing that decoded type toAddresscould be a nice follow-up but is out of scope here.
9de841f to
8ccd675
Compare
trevor-cortex
left a comment
There was a problem hiding this comment.
Re-review
All three nits from the previous pass are addressed:
getTextNodeContentis now in case-insensitive sort order in the@codama/nodesimport.formatBytes.test.tsgains autf8round-trip case (hex('6869')→['utf8', 'hi']→'0x6869'), pinning the one encoding where formatting can be lossy.formatPublicKey.test.tsusesaddress(USDC)and hoists the shareddecoded, which also reads better now that all three cases share the same input.
Nothing else changed in the source, error registry, or README. Still no changeset in the diff — if the 2.x stack relies on a seeded major changeset that's fine, otherwise this still needs one for @codama/dynamic-codecs and @codama/errors.
Approving.
8ccd675 to
9d0f436
Compare
dbc49ce to
7a17377
Compare
9d0f436 to
f248caa
Compare
7a17377 to
6bcd8c7
Compare
f248caa to
bd6992d
Compare
6bcd8c7 to
0d78a47
Compare
bd6992d to
a4e66a2
Compare
0d78a47 to
50bd2bc
Compare
a4e66a2 to
4b6216e
Compare
1a602d4 to
ca4dea7
Compare
b612a56 to
f524ac1
Compare
a938ada to
99dd664
Compare
f524ac1 to
385c7a6
Compare
99dd664 to
faa3e5d
Compare
385c7a6 to
7557a83
Compare
faa3e5d to
775a40c
Compare
7a72c81 to
aba69a3
Compare
775a40c to
9c4cc3c
Compare
aba69a3 to
79bc588
Compare
dd87bae to
9e05227
Compare
79bc588 to
1ae87a5
Compare
1ae87a5 to
d09bf71
Compare

This PR adds formatters for the remaining leaf values of decoded nodes, and makes every formatter share the same
(decoded, options)signature.formatAddressoption formats addresses however the caller needs, e.g. to name them from a trusted token list or address book, or to truncate them. It defaults to the address itself.FormatOptions, even those that use none of them yet, so any formatter can stand in for another.formatDateTimeandformatDurationnow throw the newCODAMA_ERROR__INVALID_TICKS_PER_SECONDwhenticksPerSecondis not a positive integer, instead of returningnull.formatIntegeris now the only formatter returningnull, for amounts whosedecimalscannot be resolved.