{"id":16699726,"url":"https://github.com/gonzalezreal/defaultcodable","last_synced_at":"2025-04-06T18:17:16.924Z","repository":{"id":63910952,"uuid":"238965245","full_name":"gonzalezreal/DefaultCodable","owner":"gonzalezreal","description":"A convenient way to handle default values with Swift Codable types","archived":false,"fork":false,"pushed_at":"2021-06-12T17:08:37.000Z","size":23,"stargazers_count":357,"open_issues_count":0,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-30T17:09:13.735Z","etag":null,"topics":["codable","defaults","property-wrapper","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gonzalezreal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-07T15:59:25.000Z","updated_at":"2025-02-27T15:43:21.000Z","dependencies_parsed_at":"2022-11-29T07:13:21.668Z","dependency_job_id":null,"html_url":"https://github.com/gonzalezreal/DefaultCodable","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalezreal%2FDefaultCodable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalezreal%2FDefaultCodable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalezreal%2FDefaultCodable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gonzalezreal%2FDefaultCodable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gonzalezreal","download_url":"https://codeload.github.com/gonzalezreal/DefaultCodable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247526768,"owners_count":20953143,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codable","defaults","property-wrapper","swift"],"created_at":"2024-10-12T18:08:03.388Z","updated_at":"2025-04-06T18:17:16.901Z","avatar_url":"https://github.com/gonzalezreal.png","language":"Swift","readme":"# DefaultCodable\n![Swift 5.1](https://img.shields.io/badge/Swift-5.1-orange.svg)\n[![Swift Package Manager](https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat)](https://swift.org/package-manager)\n[![Twitter: @gonzalezreal](https://img.shields.io/badge/twitter-@gonzalezreal-blue.svg?style=flat)](https://twitter.com/gonzalezreal)\n\n**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**.\n\n## Usage\nConsider a hypothetical model for Apple products., in which only the property `name` is *required*.\n\n```swift\nenum ProductType: String, Codable, CaseIterable {\n  case phone, pad, mac, accesory\n}\n\nstruct Product: Codable {\n  var name: String\n  var description: String?\n  var isAvailable: Bool?\n  var type: ProductType?\n}\n```\n\nUsing the `@Default` property wrapper, we can provide default values for the properties not required and thus get rid of the optionals in our model.\n\n```swift\nstruct Product: Codable {\n  var name: String\n  \n  @Default\u003cEmpty\u003e\n  var description: String\n  \n  @Default\u003cTrue\u003e\n  var isAvailable: Bool\n  \n  @Default\u003cFirstCase\u003e\n  var type: ProductType\n}\n```\n\nWith that in place, we can safely decode the following JSON into a `Product` type.\n\n```json\n{\n  \"name\": \"iPhone 11 Pro\"\n}\n```\n\nThe resulting `Product` instance is using the default values for those properties not present in the JSON.\n\n```\n▿ Product\n- name : \"iPhone 11 Pro\"\n- description : \"\"\n- isAvailable : true\n- type : ProductType.phone\n```\n\nIf 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.\n\nThe `@Default` property wrapper takes a `DefaultValueProvider` as a parameter. This type provides the default value when a value is not present or is `nil`.\n\n```swift\nprotocol DefaultValueProvider {\n  associatedtype Value: Equatable \u0026 Codable\n  \n  static var `default`: Value { get }\n}\n```\n\n**DefaultCodable** provides the following implementations for your convenience:\n\n### `Empty`\nIt provides an empty instance of a `String`, `Array` or any type that implements `RangeReplaceableCollection`.\n\n### `EmptyDictionary`\nIt provides an empty instance of a `Dictionary`.\n\n### `True` and `False`\nProvide `true` and `false` respectively for `Bool` properties.\n\n### `Zero` and `One`\nProvide `0` and `1` respectively for `Int` properties.\n\n### `FirstCase`\nIt provides the first case of an `enum` type as the default value. The `enum` must implement the `CaseIterable` protocol.\n\n### `ZeroDouble`\nProvide `0` for `Double` properties.\n\n## Default values for custom types\nYour custom type must implement the `DefaultValueProvider` protocol to be compatible with the `@Default` property wrapper.\n\nConsider the following type that models a role in a conversation:\n\n```swift\nstruct Role: Codable, Equatable, Hashable, RawRepresentable {\n  let rawValue: String\n\n  init?(rawValue: String) {\n    self.rawValue = rawValue\n  }\n\n  static let user = Role(rawValue: \"user\")!\n  static let bot = Role(rawValue: \"bot\")!\n}\n```\n\nIf we want the default role to be `user`, we can implement `DefaultValueProvider` as follows:\n\n```swift\nextension Role: DefaultValueProvider {\n  static let `default` = user\n}\n```\n\nWith that in place, we can use the `@Default` property wrapper in any type that has a property of type `Role`:\n\n```swift\nstruct ChannelAccount: Codable {\n  var name: String\n  \n  @Default\u003cRole\u003e\n  var role: Role\n}\n```\n\n## Installation\n**Using the Swift Package Manager**\n\nAdd **DefaultCodable** as a dependency to your `Package.swift` file. For more information, see the [Swift Package Manager documentation](https://github.com/apple/swift-package-manager/tree/master/Documentation).\n\n```\n.package(url: \"https://github.com/gonzalezreal/DefaultCodable\", from: \"1.0.0\")\n```\n\n## Help \u0026 Feedback\n- [Open an issue](https://github.com/gonzalezreal/DefaultCodable/issues/new) if you need help, if you found a bug, or if you want to discuss a feature request.\n- [Open a PR](https://github.com/gonzalezreal/DefaultCodable/pull/new/master) if you want to make some change to `DefaultCodable`.\n- Contact [@gonzalezreal](https://twitter.com/gonzalezreal) on Twitter.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgonzalezreal%2Fdefaultcodable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgonzalezreal%2Fdefaultcodable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgonzalezreal%2Fdefaultcodable/lists"}