getty-zig / getty

A (de)serialization framework for Zig
https://getty.so
MIT License
183 stars 14 forks source link

Introduce a `skip_if_null` attribute #118

Open ibokuri opened 1 year ago

ibokuri commented 1 year ago

Problem

A common scenario when serializing structs is to skip fields that are null. Currently, you have to write your own serialize function and manually check nullable fields and then skip them, which is quite tedious to do when you have lots of these fields.

Proposal

A skip_if_null attribute can be added that'll be definable by all types that can specify a block. When applied to an optional field of a struct (or an optional variant of a union, etc.), the field/variant will be serialized only when it's value is not null. For deserialization, if the final value we would have assigned to the final field/variant value is null, then we can just skip assigning the field.

Alternatives

No response

Additional Context

No response