[Pre-Pitch] Codable Customization using PropertyWrappers

I’ve implemented @Omittable before, it just was not of much use to me without property wrapper composition. The key bit of machinery is

public protocol OmittableType {
    static var nilValue: Self { get }
}

extension KeyedDecodingContainer {
    public func decode<T>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T where T : Decodable, T: OmittableType {
        return try decodeIfPresent(T.self, forKey: key) ?? T.nilValue
    }
}

The rest is contained within a very thin

@propertyWrapper
public struct Omittable<T>: OmittableType { ... }
3 Likes