feat: add toindefarray struct tag option for indefinite-length CBOR arrays#756
feat: add toindefarray struct tag option for indefinite-length CBOR arrays#756tdnguyenND wants to merge 1 commit intofxamacker:masterfrom
Conversation
…rrays
Add support for encoding Go structs as CBOR indefinite-length arrays
(major type 4, header 0x9f, break 0xff) using the struct tag option
`toindefarray`.
Usage:
type MyStruct struct {
_ struct{} `cbor:",toindefarray"`
Field1 []byte
Field2 int
}
This encodes as: 9f <field1> <field2> ff
Instead of: 82 <field1> <field2> (definite-length with toarray)
Decoding is fully supported since the decoder already handles both
definite and indefinite-length arrays for toarray structs.
Files changed:
- cache.go: Add toIndefArray flag, hasToIndefArrayOption(),
getEncodingStructToIndefArrayType()
- encode.go: Add encodeStructToIndefArray(), wire into getEncodeFuncInternal()
- toindefarray_test.go: Tests for basic and nested encoding/decoding
|
hi @fxamacker can you have a look at this PR? |
Hi @tdnguyenND, thanks for opening this PR! This PR should include the PR checklist form that is automatically added to PRs (see CONTRIBUTING). I can try to take a look at the request next weekend (April 11 or 12) if I have the specs and more context. Please open an issue to add this feature with link(s) to the Cardano CBOR specification, etc. (CDDL notation would be ideal if available). Thanks! |
|
Thanks again for submitting this PR to add a feature. For non-security issues, I reply on weekends because this CBOR library has been self-funded for years (weekends & public holidays & deferred Christmases), with my weekdays used for paid work. I didn’t receive full specs or related CDDL yet, so I reviewed your PR without that context. I’m inclined not to include the requested feature because:
Also, there are security considerations with using indefinite-length values (unrelated to this library). In general, please consider avoiding indefinite-length if your specifications allow definite-length. I'm closing this PR, but please feel free to open an issue to discuss adding the feature if I missed anything. |
Summary
Add support for encoding Go structs as CBOR indefinite-length arrays using the struct tag option
toindefarray.Usage
This encodes as:
9f <field1> <field2> ff(indefinite-length array)Instead of:
82 <field1> <field2>(definite-length withtoarray)Motivation
CBOR indefinite-length arrays (
0x9f ... 0xff) are used in various protocols (e.g. Cardano smart contract datums). Currently there is no way to encode Go structs directly to this format — only definite-length arrays viatoarray.The decoder already handles both definite and indefinite-length arrays for
toarraystructs, so this change only adds the encoding path.Changes
toIndefArrayflag to struct types,hasToIndefArrayOption(),getEncodingStructToIndefArrayType()encodeStructToIndefArray()which writes0x9fheader + fields +0xffbreak code, wired intogetEncodeFuncInternal()Decoding
No decoder changes needed — the existing decoder handles both definite and indefinite-length arrays for
toarraystructs viagetHeadWithIndefiniteLengthFlag().Tests
All existing tests pass. New tests cover:
0x9fheader,0xffbreak)