Skip to content

Instantly share code, notes, and snippets.

@mmuszynski
Created June 24, 2020 18:42
Show Gist options
  • Save mmuszynski/aa0afc00b00ca2ac090e2a01e6426014 to your computer and use it in GitHub Desktop.
Save mmuszynski/aa0afc00b00ca2ac090e2a01e6426014 to your computer and use it in GitHub Desktop.
extension Published: Codable where Value: Codable {
enum CodingKeys: CodingKey {
case value
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let value = try container.decode(Value.self, forKey: .value)
self.init(initialValue: value)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
var mutableSelf = self
let _ = mutableSelf.projectedValue.sink(receiveValue: {
try! container.encode($0, forKey: .value)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment