getPatternMatchCodec
Returns a codec that selects which variant codec to use based on pattern matching.
This codec evaluates values and byte arrays against a series of predicate functions in order, using the first matching codec for encoding or decoding.
Type Parameters
| Type Parameter |
|---|
TPatterns extends readonly ( | readonly [(value) => value is any, (bytes) => boolean, Codec<any, any>] | readonly [(value) => boolean, (bytes) => boolean, Codec<any, any>])[] |
Parameters
| Parameter | Type | Description |
|---|---|---|
patterns | TPatterns & readonly PatternMatchCodecEntry<GetEncoderTypeFromVariants<GetPatternMatchCodecs<TPatterns>>, GetEncoderTypeFromVariants<GetPatternMatchCodecs<TPatterns>>, GetEncoderTypeFromVariants<GetPatternMatchCodecs<TPatterns>>>[] | An array of [valuePredicate, bytesPredicate, codec] triples. Predicates are tested in order and the first match determines the codec used. During encoding, valuePredicate receives the value to encode. During decoding, bytesPredicate receives the byte array. |
Returns
GetUnionCodecType<GetPatternMatchCodecs<TPatterns>>
A codec that selects the appropriate variant based on the matched pattern.
Throws
Throws a SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_VALUE error if a value being encoded does not match any of the specified patterns.
Throws
Throws a SOLANA_ERROR__CODECS__INVALID_PATTERN_MATCH_BYTES error if a byte array being decoded does not match any of the specified patterns.
Example
Encoding and decoding using pattern matching.