MetaCodable icon indicating copy to clipboard operation
MetaCodable copied to clipboard

[Feature Request] IgnoreEncoding depends on other properties

Open hstdt opened this issue 1 year ago • 5 comments

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.

hstdt avatar Jun 13 '24 09:06 hstdt

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 avatar Sep 30 '24 04:09 soumyamahunt

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

hstdt avatar Sep 30 '24 06:09 hstdt

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 avatar Sep 30 '24 08:09 soumyamahunt

@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 avatar Oct 14 '24 09:10 hstdt

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

hstdt avatar Oct 22 '24 09:10 hstdt