-
Notifications
You must be signed in to change notification settings - Fork 11
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
Conversation
WalkthroughThe changes focus on enhancing type safety and consistency by introducing type guards like Changes
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this 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
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 theTxId
type. This approach ensures type safety by distinguishingTxId
from regular strings in the type system.- 14-14: The
TxId
type is properly branded using theTxIdBrand
, aligning with the objective to enhance type safety for branded types in the SDK.- 22-22: The
txId
function correctly utilizesunsafeEither
to decode a string into aTxId
, leveraging theTxIdGuard
. This approach simplifies the creation ofTxId
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 theContractId
type is a good practice for enhancing type safety and ensuringContractId
is distinct from other string types.- 25-25: The
ContractId
type is correctly defined as a branded string using theContractIdBrand
, aligning with the objective to improve type safety for branded types in the SDK.- 32-32: The
contractIdToTxId
function is updated to include thetxId
function in its flow, effectively converting aContractId
to aTxId
. This change enhances the function's utility by incorporating the newly introduced branded types. Ensure that thesplit("#")
operation is appropriate for the expected format ofContractId
.packages/runtime/client/rest/src/contract/transaction/header.ts (1)
- 50-50: Updating the
transactionId
field in theTxHeader
interface to useTxIdGuard
instead ofTxId
directly is a positive change for enhancing type safety. This ensures that thetransactionId
field is validated against theTxIdGuard
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 thetxId
function is available for use in converting payout and withdrawal IDs toTxId
.- 18-18: The
payoutIdToTxId
function is correctly updated to include thetxId
function in its flow, effectively converting aPayoutId
to aTxId
. This change aligns with the objective to incorporate branded types effectively.- 30-30: The
withdrawalIdToTxId
function is similarly updated to include thetxId
function, converting aWithdrawalId
to aTxId
. 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 theGetContractTransactionByIdRequestGuard
to useTxIdGuard
is a good practice for ensuring that thetxId
field is validated correctly at runtime, enhancing type safety.- 84-84: Similarly, updating the
transactionId
field in theSubmitContractTransactionRequestGuard
to useTxIdGuard
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 theTransactionDetailsGuard
to useTxIdGuard
instead ofTxId
directly is a positive change for enhancing type safety. This ensures that thetransactionId
field is validated against theTxIdGuard
at runtime.- 112-112: Similarly, updating the
consumingTx
field to useoptionFromNullable(TxIdGuard)
instead of directly usingTxId
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 codecC
instead oft.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 theTxIdGuard
is available for use in validating transaction IDs.- 129-129: Updating the
transactionId
field in theTransactionTextEnvelope
type to useTxIdGuard
instead ofTxId
directly is a good practice for ensuring that thetransactionId
field is validated correctly at runtime, enhancing type safety.
export const TxIdGuard = t.brand( | ||
t.string, | ||
(s): s is t.Branded<string, TxIdBrand> => true, | ||
"TxId" | ||
); |
There was a problem hiding this comment.
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.
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" | |
); |
There was a problem hiding this 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
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
andTxId
. The inclusion of the PR link provides a helpful reference for further details.
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
to this
This PR:
and moreover it improves the generated output to this:
Note that in order to preserve the type alias (at least in typescript version 4.9.5) we need to define ContractId like this
instead of
Summary by CodeRabbit
TxIdGuard
,ContractIdBrand
,TxIdBrand
) to ensure more robust type checking and validation.