Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Feature Request] Encodable Default Value Handling #80

Unanswered
tatewake asked this question in Q&A
[Feature Request] Encodable Default Value Handling #80
Apr 27, 2024 * 2 comments * 1 reply
Return to top
Discussion options

tatewake
Apr 27, 2024

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.

You must be logged in to vote

Replies: 2 comments 1 reply

Comment options

soumyamahunt
Apr 29, 2024
Maintainer

@tatewake have you looked into the the HelperCoder functionality provided to implement what you want?

You must be logged in to vote
0 replies
Comment options

tatewake
Apr 30, 2024
Author

Ah, I have not, I'll look into it.

You must be logged in to vote
1 reply
Comment options

soumyamahunt May 1, 2024
Maintainer

I have moved this to discussion for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
enhancement New feature or request
2 participants
Converted from issue

This discussion was converted from issue #77 on May 01, 2024 19:18.