Skip to content

[Feature Request] Encodable Default Value Handling #77

@tatewake

Description

@tatewake

Is your feature request related to a problem? Please describe.

In my scenario, I need to assign default values during decoding if certain fields are missing (which is already supported), but I also want to avoid encoding a field if its value matches the same default.

Consider this Node struct:

@Codable
struct Node: Hashable {
    @Default("")
    var label: String = ""
    @Default(CGPoint.zero)
    var center: CGPoint = .zero
}

For example, if center isn't set, it should default to .zero during decoding. Similarly, for my use case, if it's already set to .zero, there's no need to encode it since it's the expected default.

Describe the solution you'd like
Introduce a new tag, perhaps something like @IgnoreEncodingIfDefault when set alongside @Default(CGPoint.zero) or a standalone @IgnoreEncodingDefault(CGPoint.zero) for this purpose. (I'm not certain what the best naming convention would be, I'd imagine you'd have better ideas.)

I feel this feature would be particularly useful for user-editable configuration files where we would want to omit any "optional" fields, or just keeping human-readable configurations minimal for git diffing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions