This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 931
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): support custom discriminator property for get…
…DataEnumCodec (#2380) This PR adds support for custom discriminator properties for `getDataEnumCodecs`. ```ts // 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.
- Loading branch information
1 parent
3c33220
commit bf029dd
Showing
5 changed files
with
211 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
'@solana/codecs-data-structures': patch | ||
--- | ||
|
||
DataEnum codecs now support custom discriminator properties | ||
|
||
```ts | ||
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 }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.