Codable, but with Super power made custom Codable behavior easy.

Overview

SuperCodable

From Foundation

struct AStudent: Codable {
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.aID = try container.decode(String.self, forKey: .aID)
        self.aName = try container.decode(String.self, forKey: .aName)
        let gradeDecoded = try container.decode(Double.self, forKey: .aGrade)
        self.AGrede = Int(gradeDecoded)
    }

    enum CodingKeys: String, CodingKey {
        case aName = "name"
        case aGrade = "grade"
        case aID = "id"
    }

    var aID: String
    var aName: String
    var AGrede: Int

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(aID, forKey: .aID)
        try container.encode(Double(AGrede), forKey: .aGrade)
        try container.encode(aName, forKey: .aName)
    }
}

To SuperCodable

{ (double) -> Int in Int(double) } toEncoder: { (int) -> Double in Double(int) } ">
struct Student: SuperCodable {
    @Keyed
    var id: String
    
    @Keyed("name") 
    var aName: String
    
    @KeyedTransform("grade", doubleTransform)
    var AGrade: Int
}

let doubleTransform = SCTransformOf<Int, Double> {
    (double) -> Int in
    Int(double)
} toEncoder: { (int) -> Double in
    Double(int)
}

Feature

  • Working with Nested Foundation.Codable property

Known side effect

  • SuperDecoable must construct from nothing init()
  • @Keyed var id:Int will do O(n) calculation on underlaying wrapper _VARIABLE_NAME into key VARIABLE_NAME. Be ware of variable name takes too long

Known Disability

  • Every property in a SuperCodable should a DecodableKey / EncodableKey, otherwise the property(which should be Codable) will simply ignored during the Codable process.

Why:

Basically Mirror can't mutating the object value during the init(from decoder:) throws, since we create the object from self.init()

Other notes

  • Inspired by: https://medium.com/trueid-developers/combined-propertywrapper-with-codable-swift-368dc4aa2703

  • Try to merge @KeyedTransform into @Keyed, but it required @Keyed var id: String to be @Keyed() var id: String, with extra () 🧐

  • Swift should auto generate STRUCT.init(....) for you, but if you using @Keyed var id: String without default value, it will generate init(id: Keyed), by giving default value @Keyed var id: String = "" should solve this problem.

Know Issues

  • @Keyed var id:String? will cause fatalError on force unwrapping Keyed.value?, you can using @OptionalKeyed to make it works.
  • OptionalKeyed may / may not a good name, I am thinking of make the easy to change, maybe KeyedOptional is EASY change? 🤔
You might also like...
A simple project which shows how to pull off custom view controller transitions.

Custom View Controller Transitions This project explains and shows how to make custom view controller transitions in the most simple way possible. Eac

Custom MacBook login screen and pam modules using multipeer connectivity and usb hardware checks with iOS app for sign in.

Custom MacBook login screen and pam modules using multipeer connectivity and usb hardware checks with iOS app for sign in.

A custom calculator for deg to rad conversion & the other way round
A custom calculator for deg to rad conversion & the other way round

Lilium Features A custom calculator for deg to rad conversion & the other way round. How to use Slide up the dock and you should see Lilium. An activa

Create an easy to peek SwiftUI View to showcase your own data, catalog, images, or anything you'd like.
Create an easy to peek SwiftUI View to showcase your own data, catalog, images, or anything you'd like.

Create an easy to peek SwiftUI View to showcase your own data, catalog, images, or anything you'd like.

🛠 A flexible and easy template created to speed up the development of your iOS application based on the MVP pattern.
🛠 A flexible and easy template created to speed up the development of your iOS application based on the MVP pattern.

Model-View-Presenter template A flexible and easy template created to speed up the development of your iOS application based on the MVP pattern. This

SandboxKit - Framework that makes it easy to launch a single Scene of your application

SandboxKit This framework makes debugging more efficient in your application. Sandbox is the name of a structure that improves the efficiency of debug

Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t
Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t

Strucure: I used MVVM with Colusre binging modular architecture : Also I used openweathermap to get all information for current weather as it's easy t

Easy quiz just for reviewing structures and MVC
Easy quiz just for reviewing structures and MVC

Quizzler Our Goal The goal of this tutorial is to take you one step further in your journey of becoming an app developer. We are going to introduce yo

High-quality source code, easy implementation

High-quality source code, easy implementation

Comments
  • Fixed typos, removed redundant parentheses and dot

    Fixed typos, removed redundant parentheses and dot

    Basically Mirror can't matating the object value during the . init(from decoder:) throws, since we create the object from self.init()

    我從文意猜測這邊應該是 mutating ,如果有誤再跟我說~

    Thanks

    opened by AlexandreHJE 0
Releases(1.2.0)
Owner
Tsungyu Yu
iOS Developer, Dvorak user. keto eater, and a liver.
Tsungyu Yu
A corresponding package to RxKotlin Plus, but for Swift and iOS

A corresponding package to RxKotlin Plus, but for Swift and iOS

Lightning Kite 0 Dec 15, 2021
Sonic language: Heavily inspired by Swift, but compiles to C so you can use it anywhere.

Sonic Sonic programming language: Heavily inspired by Swift, but compiles to C so you can use it anywhere. Brought to you by Chris Hulbert and Andres

Sonic Language 27 Apr 8, 2022
🧸 Xcode Playground management made easy

Toybox Toybox made easy to manage Xcode Playgrounds. You can create and open Xcode Playground from CLI. $ toybox create Snippet # Generate ~/.toybox/S

Kohki Miki 130 Nov 8, 2022
Xcode Plugin helps you find missing methods in your class header, protocols, and super class, also makes fast inserting.

FastStub-Xcode Life is short, why waste it on meaningless typing? What is it? A code generating feature borrowed from Android Studio. FastStub automat

mrpeak 509 Jun 29, 2022
Powerful property wrapper to back codable properties.

BackedCodable Powerful property wrapper to back codable properties. Why Swift's Codable is a great language feature but easily becomes verbose and req

Jérôme Alves 472 Dec 18, 2022
SafeDecoder - a swift package that set defaults when Codable fails to decode a field

SafeDecoder is a swift package that set defaults when Codable fails to decode a field. SafeDecoder supports configurable default values, See SafeDecoder.Configuration.

GodL 4 Mar 21, 2022
An Apple Watch remake of the Poketch from Pokemon Diamond and Pearl made with SwiftUI

Apple Watch Poketch What is it? It's an Apple Watch remake of the "Poketch" from Pokemon Diamond and Pearl made with SwiftUI! Check out the YouTube vi

André Arko 1 Nov 19, 2021
Dice-rolling app made in Swift

Dicee Our Goal The objective of this tutorial is to introduce you to the core programming concepts that will form the foundation of most of the apps y

null 0 Nov 30, 2021
OnBoardSwiftUI-Food - Food App Onboarding screen made with SwiftUI

OnBoardSwiftUI-Food Food App Onboarding screen made with SwiftUI. App Details Lo

null 1 Apr 20, 2022
ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects.

ConfettiKit is a custom framework used to add Confetti on your iOS/iPadOS projects. The kit provides variety of customisations inorder to design a confetti which matches your project's UI. ConfettiKit makes your work of adding Confetti on your project with just one line of code.

Gokul Nair 14 Sep 27, 2022