-
Notifications
You must be signed in to change notification settings - Fork 869
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): consolidate Option-like codecs #2715
Conversation
🦋 Changeset detectedLatest commit: d20edaf The changes in this PR will be included in the next version bump. This PR includes changesets to release 36 packages
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 |
This stack of pull requests is managed by Graphite. Learn more about stacking. Join @lorisleiva and the rest of your teammates on Graphite |
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.
Nice, this looks like a cleaner API to me!
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.
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. |
This PR consolidates the
getNullableCodec
andgetOptionCodec
with theirZeroable
counterparts and adds more configurations.Namely, the
prefix
option can now be set tonull
and thefixed
option was replaced with thenoneValue
option which can be set to"zeroes"
forZeroable
codecs or a custom byte array for custom representations of none values. This means thegetZeroableNullableCodec
andgetZeroableOptionCodec
functions were removed in favor of the new options.As a result, this PR makes it possible to create nullable codecs that have no
prefix
nornoneValue
. In this case, the existence of the nullable item is indicated by the presence of any remaining bytes left to decode.Also note that it is now possible for custom
noneValue
byte arrays to be of any length — previously, it had to match the fixed-size of the nullable item. This means theSOLANA_ERROR__CODECS__EXPECTED_ZERO_VALUE_TO_MATCH_ITEM_FIXED_SIZE
error is no longer used. I did not remove it because it is illegal but thought I needed to mention that here.Here is a recap of all supported scenarios, using a
u16
codec as an example:encode(42)
/encode(null)
noneValue
(default)noneValue: "zeroes"
noneValue
(0xff
)u8
prefix (default)0x012a00
/0x00
0x012a00
/0x000000
0x012a00
/0x00ff
prefix
(u16
)0x01002a00
/0x0000
0x01002a00
/0x00000000
0x01002a00
/0x0000ff
prefix
0x2a00
/0x
0x2a00
/0x0000
0x2a00
/0xff
Reciprocal changes were made with
getOptionCodec
.Why?
As I needed the
getNullableCodec(x, { prefix: null })
variant, I could either add yet another Option-like codec pair such asgetRemainderNullableCodec
andgetRemainderOptionCodec
OR I could realise that actually, this is a simple 2x3 configuration matrix and refactor thegetNullableCodec
andgetOptionCodec
accordingly. I opted for the latter.