Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix how the ContractId and TxId branded types are printed in the documentation #185

Merged
merged 2 commits into from
Feb 23, 2024

Conversation

hrajchert
Copy link
Collaborator

@hrajchert hrajchert commented Feb 22, 2024

In the PR#184 I added the preserveBrand function that helped with the uses of branded types inside recursive data types, but the typing was a little bit off, and it changed the generated documentation from this
Screenshot 2024-02-22 at 12 59 17

to this
Screenshot 2024-02-22 at 10 46 51

This PR:

  • fixes that issue
  • creates a branded type for TxId

and moreover it improves the generated output to this:

Screenshot 2024-02-22 at 12 49 01

Note that in order to preserve the type alias (at least in typescript version 4.9.5) we need to define ContractId like this

export type ContractId = t.Branded<string, ContractIdBrand>;

instead of

export type ContractId = t.TypeOf<typeof ContractIdGuard>;

Summary by CodeRabbit

  • Refactor
    • Updated function signatures and type usage across multiple files for enhanced type safety and consistency.
  • New Features
    • Introduced branded types and guards (TxIdGuard, ContractIdBrand, TxIdBrand) to ensure more robust type checking and validation.
  • Bug Fixes
    • Fixed inconsistencies in type usage for transaction IDs across the application, aligning them with the new type guards.

Copy link
Contributor

coderabbitai bot commented Feb 22, 2024

Walkthrough

The changes focus on enhancing type safety and consistency by introducing type guards like TxIdGuard and branding types such as ContractIdBrand and TxIdBrand. These updates aim to ensure precise type checks and clear code semantics. The modifications range from adjusting function signatures to refining types and updating imports to align with the new type safety measures.

Changes

Files Change Summary
packages/adapter/src/io-ts.ts Updated preservedBrand function to use generic types and improve type checks.
packages/runtime/.../rest/src/contract/transaction/details.ts
packages/runtime/.../rest/src/contract/transaction/endpoints/collection.ts
packages/runtime/.../rest/src/contract/transaction/endpoints/singleton.ts
packages/runtime/.../rest/src/contract/transaction/header.ts
Updated to use TxIdGuard for improved type safety in transaction-related files.
packages/runtime/core/src/contract/id.ts
packages/runtime/core/src/payout/index.ts
packages/runtime/core/src/tx/id.ts
Introduced and utilized ContractIdBrand, TxIdBrand, and TxIdGuard; updated functions to include txId.

🐰✨
Changes abound, in code we trust,
Types refined, a must, a must!
With guards and brands, we pave the way,
For safer code, day by day.
Hoppin' through the fields of type,
Our code now strong, without the hype.
🌟🐇

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a0d24f3 and d65c6a2.
Files ignored due to path filters (1)
  • typedoc.json is excluded by: !**/*.json
Files selected for processing (8)
  • packages/adapter/src/io-ts.ts (1 hunks)
  • packages/runtime/client/rest/src/contract/transaction/details.ts (3 hunks)
  • packages/runtime/client/rest/src/contract/transaction/endpoints/collection.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/transaction/endpoints/singleton.ts (3 hunks)
  • packages/runtime/client/rest/src/contract/transaction/header.ts (2 hunks)
  • packages/runtime/core/src/contract/id.ts (2 hunks)
  • packages/runtime/core/src/payout/index.ts (3 hunks)
  • packages/runtime/core/src/tx/id.ts (1 hunks)
Additional comments: 17
packages/runtime/core/src/tx/id.ts (3)
  • 4-6: The TxIdBrand interface is correctly defined with a unique symbol to brand the TxId type. This approach ensures type safety by distinguishing TxId from regular strings in the type system.
  • 14-14: The TxId type is properly branded using the TxIdBrand, aligning with the objective to enhance type safety for branded types in the SDK.
  • 22-22: The txId function correctly utilizes unsafeEither to decode a string into a TxId, leveraging the TxIdGuard. This approach simplifies the creation of TxId instances from strings while handling potential decoding errors.
packages/runtime/core/src/contract/id.ts (3)
  • 5-5: The introduction of the ContractIdBrand interface with a unique symbol for branding the ContractId type is a good practice for enhancing type safety and ensuring ContractId is distinct from other string types.
  • 25-25: The ContractId type is correctly defined as a branded string using the ContractIdBrand, aligning with the objective to improve type safety for branded types in the SDK.
  • 32-32: The contractIdToTxId function is updated to include the txId function in its flow, effectively converting a ContractId to a TxId. This change enhances the function's utility by incorporating the newly introduced branded types. Ensure that the split("#") operation is appropriate for the expected format of ContractId.
packages/runtime/client/rest/src/contract/transaction/header.ts (1)
  • 50-50: Updating the transactionId field in the TxHeader interface to use TxIdGuard instead of TxId directly is a positive change for enhancing type safety. This ensures that the transactionId field is validated against the TxIdGuard at runtime.
packages/runtime/core/src/payout/index.ts (3)
  • 7-7: The addition of txId to the import statement from ../tx/id.js is necessary for the subsequent changes in this file, ensuring that the txId function is available for use in converting payout and withdrawal IDs to TxId.
  • 18-18: The payoutIdToTxId function is correctly updated to include the txId function in its flow, effectively converting a PayoutId to a TxId. This change aligns with the objective to incorporate branded types effectively.
  • 30-30: The withdrawalIdToTxId function is similarly updated to include the txId function, converting a WithdrawalId to a TxId. This consistent use of branded types across different modules enhances type safety and clarity.
packages/runtime/client/rest/src/contract/transaction/endpoints/singleton.ts (2)
  • 49-49: Updating the type guard for txId in the GetContractTransactionByIdRequestGuard to use TxIdGuard is a good practice for ensuring that the txId field is validated correctly at runtime, enhancing type safety.
  • 84-84: Similarly, updating the transactionId field in the SubmitContractTransactionRequestGuard to use TxIdGuard ensures consistent and safe handling of transaction IDs across different parts of the SDK.
packages/runtime/client/rest/src/contract/transaction/details.ts (2)
  • 101-101: Updating the transactionId field in the TransactionDetailsGuard to use TxIdGuard instead of TxId directly is a positive change for enhancing type safety. This ensures that the transactionId field is validated against the TxIdGuard at runtime.
  • 112-112: Similarly, updating the consumingTx field to use optionFromNullable(TxIdGuard) instead of directly using TxId ensures that optional consuming transaction IDs are validated correctly, enhancing type safety and consistency.
packages/adapter/src/io-ts.ts (1)
  • 153-171: The update to the preservedBrand function to use generic type parameters (C, N, B) and to operate on a codec C instead of t.Type<A, A, I> is a significant improvement. This change allows for a broader application of the function across different types, enhancing its utility and flexibility in handling branded types. The implementation correctly maintains the branded type's output type, addressing the issue mentioned in the related GitHub issue.
packages/runtime/client/rest/src/contract/transaction/endpoints/collection.ts (2)
  • 30-30: The addition of the TxIdGuard import is necessary for the subsequent change in this file, ensuring that the TxIdGuard is available for use in validating transaction IDs.
  • 129-129: Updating the transactionId field in the TransactionTextEnvelope type to use TxIdGuard instead of TxId directly is a good practice for ensuring that the transactionId field is validated correctly at runtime, enhancing type safety.

Comment on lines +16 to +20
export const TxIdGuard = t.brand(
t.string,
(s): s is t.Branded<string, TxIdBrand> => true,
"TxId"
);
Copy link
Contributor

Choose a reason for hiding this comment

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

The TxIdGuard is defined using t.brand to validate strings as TxId types. The predicate function always returns true, which means it does not perform any runtime validation. Consider implementing a more meaningful validation logic to ensure the input string conforms to the expected format of a transaction ID.

- (s): s is t.Branded<string, TxIdBrand> => true,
+ (s): s is t.Branded<string, TxIdBrand> => /^[0-9a-fA-F]{64}$/.test(s),

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export const TxIdGuard = t.brand(
t.string,
(s): s is t.Branded<string, TxIdBrand> => true,
"TxId"
);
export const TxIdGuard = t.brand(
t.string,
(s): s is t.Branded<string, TxIdBrand> => /^[0-9a-fA-F]{64}$/.test(s),
"TxId"
);

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d65c6a2 and d25e27b.
Files selected for processing (1)
  • changelog.d/20240222_130945_hrajchert_fix_branded_output.md (1 hunks)
Additional comments: 1
changelog.d/20240222_130945_hrajchert_fix_branded_output.md (1)
  • 1-5: The changelog entry clearly and accurately summarizes the changes made in PR-185, including the fix related to the branding of ContractId and TxId. The inclusion of the PR link provides a helpful reference for further details.

@hrajchert hrajchert requested a review from nhenin February 22, 2024 18:06
@nhenin nhenin merged commit cb546a5 into main Feb 23, 2024
4 checks passed
@nhenin nhenin deleted the hrajchert/fix-branded-output branch February 23, 2024 13:44
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