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

refactor(experimental): support number and symbol discriminator values for getDataEnumCodec #2381

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

lorisleiva
Copy link
Contributor

@lorisleiva lorisleiva commented Mar 26, 2024

This PR enables getDataEnumCodecs to use numbers or symbols as discriminator values.

const codec = getDataEnumCodec([
  [1, getStructCodec([[['one', u32]]])]
  [2, getStructCodec([[['two', u32]]])]
]);

codec.encode({ __kind: 1, one: 42 });
codec.encode({ __kind: 2, two: 42 });

This means you can also use enum values as discriminators, like so:

enum Event { Click, KeyPress }
const codec = getDataEnumCodec([
  [Event.Click, getStructCodec([[['x', u32], ['y', u32]]])],
  [Event.KeyPress, getStructCodec([[['key', u32]]])]
]);

codec.encode({ __kind: Event.Click, x: 1, y: 2 });
codec.encode({ __kind: Event.KeyPress, key: 3 });

Copy link

changeset-bot bot commented Mar 26, 2024

🦋 Changeset detected

Latest commit: f29af61

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 35 packages
Name Type
@solana/codecs-data-structures Patch
@solana/codecs Patch
@solana/transactions Patch
@solana/web3.js-experimental Patch
@solana/sysvars Patch
@solana/compat Patch
@solana/web3.js-legacy-sham Patch
@solana/programs Patch
@solana/rpc-api Patch
@solana/rpc-graphql Patch
@solana/rpc-subscriptions-api Patch
@solana/signers Patch
@solana/transaction-confirmation Patch
@solana/rpc Patch
@solana/rpc-subscriptions Patch
@solana/accounts Patch
@solana/addresses Patch
@solana/assertions Patch
@solana/codecs-core Patch
@solana/codecs-numbers Patch
@solana/codecs-strings Patch
@solana/errors Patch
@solana/functional Patch
@solana/instructions Patch
@solana/keys Patch
@solana/options Patch
@solana/rpc-parsed-types Patch
@solana/rpc-spec-types Patch
@solana/rpc-spec Patch
@solana/rpc-subscriptions-spec Patch
@solana/rpc-subscriptions-transport-websocket Patch
@solana/rpc-transformers Patch
@solana/rpc-transport-http Patch
@solana/rpc-types Patch
@solana/webcrypto-ed25519-polyfill Patch

Not sure what this means? Click here to learn what changesets are.

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

Copy link
Collaborator

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

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

Added via Giphy

packages/codecs-data-structures/src/data-enum.ts Outdated Show resolved Hide resolved
@lorisleiva lorisleiva force-pushed the loris/data-enum-custom-discriminator-value branch from 4f9a7da to 1195521 Compare March 27, 2024 09:17
Copy link
Contributor Author

lorisleiva commented Mar 27, 2024

Merge activity

  • Mar 27, 9:34 AM EDT: @lorisleiva started a stack merge that includes this pull request via Graphite.
  • Mar 27, 9:35 AM EDT: Graphite rebased this pull request as part of a merge.
  • Mar 27, 9:35 AM EDT: @lorisleiva merged this pull request with Graphite.

Base automatically changed from loris/data-enum-custom-discriminator-property to master March 27, 2024 13:34
@lorisleiva lorisleiva force-pushed the loris/data-enum-custom-discriminator-value branch from 1195521 to f29af61 Compare March 27, 2024 13:34
@lorisleiva lorisleiva merged commit 49a764c into master Mar 27, 2024
6 checks passed
@lorisleiva lorisleiva deleted the loris/data-enum-custom-discriminator-value branch March 27, 2024 13:35
Copy link
Contributor

github-actions bot commented Apr 1, 2024

🎉 This PR is included in version 1.91.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants