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 custom discriminator property for getDataEnumCodec #2380

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

lorisleiva
Copy link
Contributor

@lorisleiva lorisleiva commented Mar 26, 2024

This PR adds support for custom discriminator properties for getDataEnumCodecs.

// Before.
const codec = getDataEnumCodec([
  ['click', getStructCodec([[['x', u32], ['y', u32]]])],
  ['keyPress', getStructCodec([[['key', u32]]])]
]);

codec.encode({ __kind: 'click', x: 1, y: 2 });
codec.encode({ __kind: 'keyPress', key: 3 });

// After.
const codec = getDataEnumCodec([
  ['click', getStructCodec([[['x', u32], ['y', u32]]])],
  ['keyPress', getStructCodec([[['key', u32]]])]
], { discriminator: 'event' });

codec.encode({ event: 'click', x: 1, y: 2 });
codec.encode({ event: 'keyPress', key: 3 });

Note that, to make this work, this PR updates a few exported types such as GetDataEnumKind or GetDataEnumKindContent which is used by Kinobi. I'll make sure to have a PR ready on Kinobi's side when this is ready to be merged.

Copy link

changeset-bot bot commented Mar 26, 2024

🦋 Changeset detected

Latest commit: a8084d3

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.

nicesteve

['B', {} as Encoder<{ x: number; y: number }>],
]) satisfies Encoder<{ __kind: 'A'; value: string } | { __kind: 'B'; x: number; y: number }>;
}
// It constructs data enums from a list of encoder variants.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Added via Giphy

@lorisleiva
Copy link
Contributor Author

Kinobi PR ready to follow this up at: metaplex-foundation/kinobi#187

Copy link
Contributor Author

lorisleiva commented Mar 27, 2024

Merge activity

@lorisleiva lorisleiva merged commit bf029dd into master Mar 27, 2024
8 checks passed
@lorisleiva lorisleiva deleted the loris/data-enum-custom-discriminator-property branch March 27, 2024 13:34
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