File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -30,3 +30,18 @@ public struct DefaultCodable<Default: DefaultCodableStrategy>: Codable {
30
30
31
31
extension DefaultCodable : Equatable where Default. RawValue: Equatable { }
32
32
extension DefaultCodable : Hashable where Default. RawValue: Hashable { }
33
+
34
+ // MARK: - KeyedDecodingContainer
35
+ public extension KeyedDecodingContainer {
36
+
37
+ /// Default implementation of decoding a DefaultCodable
38
+ ///
39
+ /// Decodes successfully if key is available if not fallsback to the default value provided.
40
+ func decode< P> ( _: DefaultCodable < P > . Type , forKey key: Key ) throws -> DefaultCodable < P > {
41
+ if let value = try decodeIfPresent ( DefaultCodable< P> . self , forKey: key) {
42
+ return value
43
+ } else {
44
+ return DefaultCodable ( wrappedValue: P . defaultValue)
45
+ }
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments