Open hstdt opened 5 months ago
PS: It's a little bit strange that pass true to IgnoreEncoding(if:) lead to encoding but not ignore.
Thanks for noticing this, this has been updated.
IgnoreEncoding depends on other properties
@hstdt can you give a practical use-case or example where this would be necessary? I think something like this is possible with HelperCoder
.
@soumyamahunt In my case, many values have default value, I don't want to encode everything to a large json(this can be done by @IgnoreEncoding now). Based on this, some other properties like type or bool can also related to encoding condition, so that i want to get class itself not only value.
@Codable class SomeClass {
var shouldEncodeValue1: Bool
var type: Int
@IgnoreEncoding(if: { ($0 as SomeClass).value != 1 && ($0 as SomeClass).shouldEncodeValue1 && ($0 as SomeClass).type == 0 })
@Default(1)
var value1: Int
@Default(2)
var value2: Int
@Default(3)
var value3: Int
@Default(4)
var value4: Int
...
@Default(10)
var value10: Int
}
In additional, some unused property can be filtered after encode and decode.
Based on this, some other properties like type or bool can also related to encoding condition, so that i want to get class itself not only.
@hstdt can you give a real world use-case for this? I am more curious about how do you decode such fields if there encoding is dependent upon another property.
A real world example would be better.
@soumyamahunt Sorry for the late reply🥲, I was on vacation last week.
I have a SwiftUI app
Content(File/Folder) <-> FileDocument including a info.json <-> FileDocumentStruct
And It's unnecessary to encode everything to FileDocument, so self-manage conditional encoding might be a good idea with less bug. As above mentioned, unused properties can be filted. There are some similarities to DynamicCodable, but it's hard to separate to TextPost/PicturePost/AudioPost, it is just a large content. TextProtocol//PictureProtocol/AudioProtol is better in my situation.
And after unused value filtered, default value will take effect with this pull request
@Codable
public final class SomeClass2 {
public var value1: Bool
@IgnoreEncoding(if: { ($0 as SomeClass2).value1 || ($0 as SomeClass2).value2 == 0 })
@Default(0)
public var value2: Int!
}
Is your feature request related to a problem? Please describe. Nope
Describe the solution you'd like
Describe alternatives you've considered
Additional context PS: It's a little bit strange that pass
true
toIgnoreEncoding(if:)
lead to encoding but not ignore.