This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
refactor(experimental): consolidate Option-like codecs #2715
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.