-
Notifications
You must be signed in to change notification settings - Fork 908
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 number and symbol discriminator value…
…s for getDataEnumCodec
- Loading branch information
1 parent
bf029dd
commit f29af61
Showing
6 changed files
with
140 additions
and
4 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,28 @@ | ||
--- | ||
'@solana/codecs-data-structures': patch | ||
--- | ||
|
||
DataEnum codecs can now use numbers or symbols as discriminator values | ||
|
||
```ts | ||
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: | ||
|
||
```ts | ||
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 }); | ||
``` |
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
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