All Projects → ytyubox → SuperCodable

ytyubox / SuperCodable

Licence: other
Codable, but with Super power made custom Codable behavior easy.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to SuperCodable

CodableWrapper
@codec("encoder", "decoder") var cool: Bool = true
Stars: ✭ 143 (+521.74%)
Mutual labels:  codable, propertywrapper
NetworkAgent
This package is meant to make http request of an easy way inspiren in the architecture of Moya package. This package is 100% free of dependencies and works with Combine api + Codable
Stars: ✭ 16 (-30.43%)
Mutual labels:  easy-to-use, codable
SimplePHP
A small query builder project designed to assist daily routines and speed up the process of communicating with the database.
Stars: ✭ 14 (-39.13%)
Mutual labels:  easy-to-use
CNN Own Dataset
CNN example for training your own datasets.
Stars: ✭ 25 (+8.7%)
Mutual labels:  easy-to-use
bulk-email-sender
Send Templatized Dynamic Emails Automatically
Stars: ✭ 30 (+30.43%)
Mutual labels:  easy-to-use
Realm-and-Swift-Codable
How to implement Swift 4 Codable with Realm Database
Stars: ✭ 34 (+47.83%)
Mutual labels:  codable
pbPlots
A plotting library available in many programming languages.
Stars: ✭ 71 (+208.7%)
Mutual labels:  easy-to-use
ash
Simple iOS app using MVVM and Codable
Stars: ✭ 24 (+4.35%)
Mutual labels:  codable
Podcasts-UIKit
OUTDATED. A clone of Apple's Podcasts. UIKit version.
Stars: ✭ 145 (+530.43%)
Mutual labels:  codable
easy-materialize-rtl
Simple way to set RTL for materializecss.com.
Stars: ✭ 20 (-13.04%)
Mutual labels:  easy-to-use
Modesta
🎨 A clean CSS framework made to be dark, responsive and easy to build with.
Stars: ✭ 76 (+230.43%)
Mutual labels:  easy-to-use
CSSTrackr
User tracking system without javascript designed to work as a plug-and-play css file.
Stars: ✭ 13 (-43.48%)
Mutual labels:  easy-to-use
DM-BOT
📧 DM-BOT is discord bot that can record direct messages. One of us! You can also reply to those messages! DM-BOT is easy to use & understand! I decided to use Discord.js, it's literally the best.
Stars: ✭ 31 (+34.78%)
Mutual labels:  easy-to-use
quick-net
This is a top level socket library, making servers and clients EASY!
Stars: ✭ 15 (-34.78%)
Mutual labels:  easy-to-use
PySiQ
A Python Simple Queue system for your apps
Stars: ✭ 23 (+0%)
Mutual labels:  easy-to-use
TOMLDecoder
From TOML to Swift Codable types.
Stars: ✭ 52 (+126.09%)
Mutual labels:  codable
umock-c
A pure C mocking library
Stars: ✭ 29 (+26.09%)
Mutual labels:  easy-to-use
MOE
MOE is an event-driven OS for 8/16/32-bit MCUs. MOE means "Minds Of Embedded system", It’s also the name of my lovely baby daughter 😎
Stars: ✭ 54 (+134.78%)
Mutual labels:  easy-to-use
killswitch-windows
VPN kill switch for windows.
Stars: ✭ 22 (-4.35%)
Mutual labels:  easy-to-use
Nintendo-Switch-JoyCon-Hack
Hardwiring a push button in a JoyCon to grant bootloader access
Stars: ✭ 44 (+91.3%)
Mutual labels:  easy-to-use

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

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

Even random backend type

struct AnyValueJSON: SuperCodable {
    @KeyedTransform(IDTransform)
    var id:Int
}

 let data =
    #"""
    [
        {
            "id": "0",
        },
        {
            "id": 1,
        },
        {
            "id": "abc",
        },
        {
            "id": true,
        },
    ]
    """#.data(using: .utf8)!
let sut = try! JSONDecoder().decode([AnyValueJSON].self, from: data)
XCTAssertEqual(sut.count, 4)
XCTAssertEqual(sut.map(\.id), [0, 1, 0, 1])

Can be found in Tests/SuperCodableTests/AnyValueDecode.swift

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<String>), 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? 🤔
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].