Last active
July 4, 2024 14:55
-
-
Save IanKeen/e5db10e66d617e5fe89135a9f125cc39 to your computer and use it in GitHub Desktop.
PropertyWrapper: Decode default values. when they are `null`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
public protocol DefaultValue { | |
associatedtype Value: Codable | |
static var value: Value { get } | |
} | |
@propertyWrapper | |
public struct Default<Default: DefaultValue>: Codable { | |
public var wrappedValue: Default.Value | |
public init(wrappedValue: Default.Value) { | |
self.wrappedValue = wrappedValue | |
} | |
public init(value: Default.Value?) { | |
self.wrappedValue = value ?? Default.value | |
} | |
public func encode(to encoder: Encoder) throws { | |
try wrappedValue.encode(to: encoder) | |
} | |
} | |
extension Default: Equatable where Default.Value: Equatable { | |
public static func ==(lhs: Self, rhs: Self) -> Bool { | |
return lhs.wrappedValue == rhs.wrappedValue | |
} | |
} | |
extension KeyedDecodingContainer { | |
public func decode<T: DefaultValue>(_ type: Default<T>.Type, forKey key: KeyedDecodingContainer.Key) throws -> Default<T> { | |
return Default<T>(value: try decodeIfPresent(T.Value.self, forKey: key)) | |
} | |
} | |
public struct True: DefaultValue { | |
public static let value = true | |
} | |
public struct False: DefaultValue { | |
public static let value = false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Empty<T: Emptyable & Codable>: DefaultValue { | |
public static var value: T { .init() } | |
} | |
public protocol Emptyable { | |
init() | |
var isEmpty: Bool { get } | |
} | |
extension String: Emptyable { } | |
extension Array: Emptyable { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: