All Projects → gonzalezreal → Defaultcodable

gonzalezreal / Defaultcodable

Licence: mit
A convenient way to handle default values with Swift Codable types

Programming Languages

swift
15916 projects

Labels

Projects that are alternatives of or similar to Defaultcodable

json2codable
A command line tool to generate a Swift Codable struct from a JSON document
Stars: ✭ 19 (-93.6%)
Mutual labels:  codable
AC-iOS-Codeable-and-UserDefaults
No description or website provided.
Stars: ✭ 16 (-94.61%)
Mutual labels:  codable
SuperCodable
Codable, but with Super power made custom Codable behavior easy.
Stars: ✭ 23 (-92.26%)
Mutual labels:  codable
QuickDB
A Generic CoreData Manager to accept any type of objects. Fastest way for adding a Database to your project.
Stars: ✭ 16 (-94.61%)
Mutual labels:  codable
codable-kit
Conveniences for working with Swift's Codable protocols.
Stars: ✭ 19 (-93.6%)
Mutual labels:  codable
ash
Simple iOS app using MVVM and Codable
Stars: ✭ 24 (-91.92%)
Mutual labels:  codable
TrickerX
An Xcode9+ Extension which make CodingKeys automatically.
Stars: ✭ 90 (-69.7%)
Mutual labels:  codable
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 (-94.61%)
Mutual labels:  codable
FireSnapshot
A useful Firebase-Cloud-Firestore Wrapper with Codable.
Stars: ✭ 56 (-81.14%)
Mutual labels:  codable
Podcasts-UIKit
OUTDATED. A clone of Apple's Podcasts. UIKit version.
Stars: ✭ 145 (-51.18%)
Mutual labels:  codable
WXKDarkSky
A pure-Swift Codable layer over the Dark Sky API.
Stars: ✭ 21 (-92.93%)
Mutual labels:  codable
CodablePersist
Store and Cache Anything Codable
Stars: ✭ 18 (-93.94%)
Mutual labels:  codable
Realm-and-Swift-Codable
How to implement Swift 4 Codable with Realm Database
Stars: ✭ 34 (-88.55%)
Mutual labels:  codable
CodableWrapper
@codec("encoder", "decoder") var cool: Bool = true
Stars: ✭ 143 (-51.85%)
Mutual labels:  codable
PotentCodables
🧪 PotentCodables - A potent set of implementations and extensions to the Swift Codable system
Stars: ✭ 32 (-89.23%)
Mutual labels:  codable
JSONFeed
A Swift encoder and decoder for the JSON Feed format
Stars: ✭ 51 (-82.83%)
Mutual labels:  codable
AlamofireCodable
An Alamofire extension which converts JSON response data into swift objects using Codable
Stars: ✭ 47 (-84.18%)
Mutual labels:  codable
IkigaJSON
A high performance JSON library in Swift
Stars: ✭ 316 (+6.4%)
Mutual labels:  codable
PreciseDecimal
A Decimal type that plays nicely with literals and Decodable ✨
Stars: ✭ 18 (-93.94%)
Mutual labels:  codable
TOMLDecoder
From TOML to Swift Codable types.
Stars: ✭ 52 (-82.49%)
Mutual labels:  codable

DefaultCodable

Swift 5.1 Swift Package Manager Twitter: @gonzalezreal

DefaultCodable is a Swift µpackage that provides a convenient way to define default values in Codable types for properties that are not present or have a nil value.

Usage

Consider a hypothetical model for Apple products., in which only the property name is required.

enum ProductType: String, Codable, CaseIterable {
  case phone, pad, mac, accesory
}

struct Product: Codable {
  var name: String
  var description: String?
  var isAvailable: Bool?
  var type: ProductType?
}

Using the @Default property wrapper, we can provide default values for the properties not required and thus get rid of the optionals in our model.

struct Product: Codable {
  var name: String
  
  @Default<Empty>
  var description: String
  
  @Default<True>
  var isAvailable: Bool
  
  @Default<FirstCase>
  var type: ProductType
}

With that in place, we can safely decode the following JSON into a Product type.

{
  "name": "iPhone 11 Pro"
}

The resulting Product instance is using the default values for those properties not present in the JSON.

▿ Product
- name : "iPhone 11 Pro"
- description : ""
- isAvailable : true
- type : ProductType.phone

If you encode the result back, the resulting JSON will be the same as the one we started with. The @Default property wrapper will not encode the value if it is equal to the default value.

The @Default property wrapper takes a DefaultValueProvider as a parameter. This type provides the default value when a value is not present or is nil.

protocol DefaultValueProvider {
  associatedtype Value: Equatable & Codable
  
  static var `default`: Value { get }
}

DefaultCodable provides the following implementations for your convenience:

Empty

It provides an empty instance of a String, Array or any type that implements RangeReplaceableCollection.

EmptyDictionary

It provides an empty instance of a Dictionary.

True and False

Provide true and false respectively for Bool properties.

Zero and One

Provide 0 and 1 respectively for Int properties.

FirstCase

It provides the first case of an enum type as the default value. The enum must implement the CaseIterable protocol.

ZeroDouble

Provide 0 for Double properties.

Default values for custom types

Your custom type must implement the DefaultValueProvider protocol to be compatible with the @Default property wrapper.

Consider the following type that models a role in a conversation:

struct Role: Codable, Equatable, Hashable, RawRepresentable {
  let rawValue: String

  init?(rawValue: String) {
    self.rawValue = rawValue
  }

  static let user = Role(rawValue: "user")!
  static let bot = Role(rawValue: "bot")!
}

If we want the default role to be user, we can implement DefaultValueProvider as follows:

extension Role: DefaultValueProvider {
  static let `default` = user
}

With that in place, we can use the @Default property wrapper in any type that has a property of type Role:

struct ChannelAccount: Codable {
  var name: String
  
  @Default<Role>
  var role: Role
}

Installation

Using the Swift Package Manager

Add DefaultCodable as a dependency to your Package.swift file. For more information, see the Swift Package Manager documentation.

.package(url: "https://github.com/gonzalezreal/DefaultCodable", from: "1.0.0")

Help & Feedback

  • Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.
  • Open a PR if you want to make some change to DefaultCodable.
  • Contact @gonzalezreal on Twitter.
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].