Skip to content

[2.x] Add formatters for booleans, bytes, enums and public keys - #1217

Merged
lorisleiva merged 1 commit into
mainfrom
10-08-add_formatters_for_booleans_bytes_enums_and_public_keys
Oct 9, 2026
Merged

lorisleiva merged 1 commit into
mainfrom
10-08-add_formatters_for_booleans_bytes_enums_and_public_keys

Conversation

@lorisleiva

@lorisleiva lorisleiva commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

This PR adds formatters for the remaining leaf values of decoded nodes, and makes every formatter share the same (decoded, options) signature.

formatBoolean(decoded); // "true"
formatBytes(decoded); // "0x0102", whatever encoding the bytes were decoded with
formatEnum(decoded); // the variant's label, otherwise its identifier in title case, e.g. "Move To"
formatPublicKey(decoded, { formatAddress: address => names.get(address) ?? address }); // "USDC"
  • Addresses: the new formatAddress option 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.
  • Uniform signatures: every formatter now accepts FormatOptions, even those that use none of them yet, so any formatter can stand in for another.
  • Invalid ticks: formatDateTime and formatDuration now throw the new CODAMA_ERROR__INVALID_TICKS_PER_SECOND when ticksPerSecond is not a positive integer, instead of returning null. formatInteger is now the only formatter returning null, for amounts whose decimals cannot be resolved.

@lorisleiva lorisleiva changed the title Add formatters for booleans, bytes, enums and public keys [2.x] Add formatters for booleans, bytes, enums and public keys Oct 8, 2026
@changeset-bot

changeset-bot Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: d09bf71

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@lorisleiva

Copy link
Copy Markdown
Member Author

@trevor-cortex

@trevor-cortex trevor-cortex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: getTextNodeContent is inserted after isNode, which breaks the case-insensitive sort the rest of the file follows. pnpm lint:fix should 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 the 2.x branch 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/formatDuration now throw instead of returning null. Callers that were checking for null will need updating — fine for a major era, just flagging it for anyone reading the changelog later.

Notes for subsequent reviewers

  • formatBytes always renders hex, even for utf8-decoded bytes. That's a deliberate and documented choice (README + JSDoc explain the lossy utf8 round-trip), not an oversight.
  • formatPublicKey casts decoded.value as Address because DecodedPublicKeyTypeNode types its value as string. Narrowing that decoded type to Address could be a nice follow-up but is out of scope here.

Comment thread packages/dynamic-codecs/src/format.ts Outdated
Comment thread packages/dynamic-codecs/test/format/formatBytes.test.ts
Comment thread packages/dynamic-codecs/test/format/formatPublicKey.test.ts Outdated
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from 9de841f to 8ccd675 Compare October 8, 2026 14:22

@trevor-cortex trevor-cortex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review

All three nits from the previous pass are addressed:

  • getTextNodeContent is now in case-insensitive sort order in the @codama/nodes import.
  • formatBytes.test.ts gains a utf8 round-trip case (hex('6869') → ['utf8', 'hi'] → '0x6869'), pinning the one encoding where formatting can be lossy.
  • formatPublicKey.test.ts uses address(USDC) and hoists the shared decoded, 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.

@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from 8ccd675 to 9d0f436 Compare October 9, 2026 09:08
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch from dbc49ce to 7a17377 Compare October 9, 2026 09:08
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from 9d0f436 to f248caa Compare October 9, 2026 09:11
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch from 7a17377 to 6bcd8c7 Compare October 9, 2026 09:11
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from f248caa to bd6992d Compare October 9, 2026 09:12
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch from 6bcd8c7 to 0d78a47 Compare October 9, 2026 09:12
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from bd6992d to a4e66a2 Compare October 9, 2026 09:13
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch from 0d78a47 to 50bd2bc Compare October 9, 2026 09:13
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from a4e66a2 to 4b6216e Compare October 9, 2026 09:14
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch 2 times, most recently from 1a602d4 to ca4dea7 Compare October 9, 2026 09:15
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch 2 times, most recently from b612a56 to f524ac1 Compare October 9, 2026 09:16
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch 2 times, most recently from a938ada to 99dd664 Compare October 9, 2026 09:17
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from f524ac1 to 385c7a6 Compare October 9, 2026 09:17
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch from 99dd664 to faa3e5d Compare October 9, 2026 09:17
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from 385c7a6 to 7557a83 Compare October 9, 2026 09:17
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch from faa3e5d to 775a40c Compare October 9, 2026 09:18
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch 2 times, most recently from 7a72c81 to aba69a3 Compare October 9, 2026 09:18
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch from 775a40c to 9c4cc3c Compare October 9, 2026 09:18
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from aba69a3 to 79bc588 Compare October 9, 2026 09:19
@lorisleiva
lorisleiva force-pushed the 10-07-return_decoded_nodes_from_dynamic_parsers branch 2 times, most recently from dd87bae to 9e05227 Compare October 9, 2026 09:19
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from 79bc588 to 1ae87a5 Compare October 9, 2026 09:19
Base automatically changed from 10-07-return_decoded_nodes_from_dynamic_parsers to main October 9, 2026 09:20
@lorisleiva
lorisleiva force-pushed the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch from 1ae87a5 to d09bf71 Compare October 9, 2026 09:20
@lorisleiva
lorisleiva marked this pull request as ready for review October 9, 2026 09:20
@lorisleiva
lorisleiva merged commit 617b8d2 into main Oct 9, 2026
0 of 2 checks passed
@lorisleiva
lorisleiva deleted the 10-08-add_formatters_for_booleans_bytes_enums_and_public_keys branch October 9, 2026 09:20
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.

2 participants