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

StrictSerializedTransformable - SerializedTransformable that doesn't result in Optional types #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

hex2f
Copy link

@hex2f hex2f commented Mar 28, 2022

This is probably far from the best way to do something like this, may even be possible in the original package with the methods provided, but I couldn't find a way to do it. Just thought i'd share this as a PR in case anyone finds it useful and wants to refine the concept to be merged into the main branch. If not, feel free to close c:

Example of StrictSerializedTransformable

  • Like SerializedTransformable but for non-optional types
 class IntToStringTransformer: StrictTransformable {
    static func transformFromJSON(value: Int) -> String {
        return String(value)
    }
    
    static func transformToJSON(value: String) -> Int {
        return Int(value) ?? 0
    }
 }
 // Usage of `StrictSerializedTransformable`
 struct User: Serializable {
     @StrictSerializedTransformable<IntToStringTransformer>
     var id: String
 } 

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

Successfully merging this pull request may close these issues.

1 participant