SwiftyLab / MetaCodable

Supercharge Swift's Codable implementations with macros meta-programming.
https://swiftpackageindex.com/SwiftyLab/MetaCodable/main/documentation/metacodable
MIT License
629 stars 23 forks source link

[Feature Request] IgnoreEncoding depends on other properties #91

Open hstdt opened 5 months ago

hstdt commented 5 months ago

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

Describe the solution you'd like CleanShot 2024-06-13 at 17 22 29

Describe alternatives you've considered

@Codable
public final class SomeClass2 {
    public var value1: Bool
    @IgnoreEncoding(if: { ($0 as SomeClass2).value1 })
    public var value2: Int
}

Additional context PS: It's a little bit strange that pass true to IgnoreEncoding(if:) lead to encoding but not ignore.

soumyamahunt commented 1 month 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.

hstdt commented 1 month ago

@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.

soumyamahunt commented 1 month ago

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.

hstdt commented 1 month ago

@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.

hstdt commented 3 weeks ago

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!
}