Skip to content

feat: add support for optional fields at the end of tuple structs#109

Merged
Stebalien merged 5 commits intomasterfrom
steb/optional-fields
Feb 28, 2025
Merged

feat: add support for optional fields at the end of tuple structs#109
Stebalien merged 5 commits intomasterfrom
steb/optional-fields

Conversation

@Stebalien
Copy link
Collaborator

@Stebalien Stebalien commented Feb 27, 2025

Motivation: This makes it possible to add fields to structs while still being able to decode versions that don't include those fields.

Fields at the end of a tuple-encoded struct can be now marked as cborgen:"optional". When thus marked:

  1. These fields are not required when unmarshaling CBOR into this struct. Instead, they'll be initialized to their respective zero values.
  2. These fields are still written out when marshaling into CBOR.

Marking map-encoded struct fields as optional is legal but has no impact in practice as all map fields are optional. As before, tuple-encoded structs will not omit "omitempty" fields, even when optional.

@Stebalien Stebalien force-pushed the steb/optional-fields branch from 751ff3d to 1e57bba Compare February 27, 2025 21:21
Fields at the end of a tuple-encoded struct can be now marked as
`cborgen:"optional"`. When thus marked:

1. These fields are not required when unmarshaling CBOR into this
struct. Instead, they'll be initialized to their respective zero values.
2. These fields are still written out when marshaling into CBOR.

Marking map-encoded struct fields as optional is legal but has no impact
in practice as all map fields are optional. As before, tuple-encoded
structs will not omit "omitempty" fields, even when optional.
@Stebalien Stebalien force-pushed the steb/optional-fields branch from 1e57bba to ebdc598 Compare February 27, 2025 21:31
@rvagg
Copy link
Contributor

rvagg commented Feb 28, 2025

lgtm, simpler than I imagined it would end up being. Some form of documentation would be nice, I know there's not a lot in here but capturing what you said in your comment there next to the feature itself would be nice if you can; otherwise this is just another obscure function that only certainly people with the magic in their heads understand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants