Created
June 24, 2020 18:42
-
-
Save mmuszynski/aa0afc00b00ca2ac090e2a01e6426014 to your computer and use it in GitHub Desktop.
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
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