Skip to content

Instantly share code, notes, and snippets.

@ha1f
Created January 4, 2021 01:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ha1f/8a6c7f33e3ba2fdf26100aef2d715743 to your computer and use it in GitHub Desktop.
Save ha1f/8a6c7f33e3ba2fdf26100aef2d715743 to your computer and use it in GitHub Desktop.
import Foundation
public protocol EmptyInitializable {
init()
}
extension Int: EmptyInitializable {}
extension String: EmptyInitializable {}
extension Array: EmptyInitializable {}
extension Bool: EmptyInitializable {}
@propertyWrapper
public struct EmptyDecoded<T: Decodable & EmptyInitializable>: Decodable {
public var wrappedValue: T
public init(wrappedValue: T) {
self.wrappedValue = wrappedValue
}
public init(from decoder: Decoder) throws {
guard let container = try? decoder.singleValueContainer(),
let value = try? container.decode(T.self) else {
wrappedValue = T()
return
}
wrappedValue = value
}
}
extension EmptyDecoded: Equatable where T: Equatable {}
extension EmptyDecoded: Hashable where T: Hashable {}
extension KeyedDecodingContainer {
public func decode<T>(_ type: EmptyDecoded<T>.Type, forKey key: K) throws -> EmptyDecoded<T> where T: Decodable & EmptyInitializable {
try EmptyDecoded<T>(from: superDecoder(forKey: key))
}
}
struct Spot: Decodable {
var id: Int
var name: String
@EmptyDecoded
var isGoodPlace: Bool
}
let string = #"{ "id": 300, "name": "AppBrew"}"#
print(try! JSONDecoder().decode(Spot.self, from: string.data(using: .utf8)!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment