Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] IgnoreEncoding depends on other properties #91

Open
1 task done
hstdt opened this issue Jun 13, 2024 · 5 comments
Open
1 task done

[Feature Request] IgnoreEncoding depends on other properties #91

hstdt opened this issue Jun 13, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@hstdt
Copy link
Contributor

hstdt commented Jun 13, 2024

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 hstdt added the enhancement New feature or request label Jun 13, 2024
@soumyamahunt
Copy link
Contributor

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
Copy link
Contributor Author

hstdt commented Sep 30, 2024

@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
Copy link
Contributor

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
Copy link
Contributor Author

hstdt commented Oct 14, 2024

@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
Copy link
Contributor Author

hstdt commented Oct 22, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants