@@ -20,46 +20,45 @@ import Foundation
20
20
@propertyWrapper
21
21
public struct NullableProperty < Wrapped> {
22
22
23
- public var wrappedValue : Wrapped ?
23
+ public var wrappedValue : Wrapped ?
24
24
25
- public init ( wrappedValue: Wrapped ? ) {
26
- self . wrappedValue = wrappedValue
27
- }
25
+ public init ( wrappedValue: Wrapped ? ) {
26
+ self . wrappedValue = wrappedValue
27
+ }
28
28
}
29
29
30
30
extension NullableProperty : Encodable where Wrapped: Encodable {
31
31
32
- public func encode( to encoder: Encoder ) throws {
33
- var container = encoder. singleValueContainer ( )
34
- switch wrappedValue {
35
- case . some( let value) :
36
- // Standard value
37
- try container. encode ( value)
38
-
39
- case . none:
40
- // Empty value, encode delete op command
41
- try container. encode ( Delete ( ) )
42
- }
43
- }
32
+ public func encode( to encoder: Encoder ) throws {
33
+ var container = encoder. singleValueContainer ( )
34
+ switch wrappedValue {
35
+ case . some( let value) :
36
+ try container. encode ( value)
37
+
38
+ case . none:
39
+ try container. encode ( Delete ( ) )
40
+ }
41
+ }
44
42
}
45
43
46
44
extension NullableProperty : Decodable where Wrapped: Decodable {
47
45
48
- public init ( from decoder: Decoder ) throws {
49
- let container = try decoder. singleValueContainer ( )
50
- // Values coming from server won't have the delete operation, so we can just check if it's a null value and then decode normally.
51
- if !container. decodeNil ( ) {
52
- wrappedValue = try container. decode ( Wrapped . self)
53
- }
54
- }
46
+ public init ( from decoder: Decoder ) throws {
47
+ let container = try decoder. singleValueContainer ( )
48
+ if !container. decodeNil ( ) {
49
+ wrappedValue = try container. decode ( Wrapped . self)
50
+ }
51
+ }
55
52
}
56
53
57
54
extension NullableProperty : Hashable where Wrapped: Hashable { }
58
55
extension NullableProperty : Equatable where Wrapped: Equatable { }
59
56
60
57
extension KeyedDecodingContainer {
61
58
62
- public func decode< Wrapped> ( _ type: NullableProperty < Wrapped > . Type , forKey key: KeyedDecodingContainer < K > . Key ) throws -> NullableProperty < Wrapped > where Wrapped: Decodable {
63
- return try decodeIfPresent ( NullableProperty< Wrapped> . self , forKey: key) ?? NullableProperty < Wrapped > ( wrappedValue: nil )
64
- }
59
+ public func decode< Wrapped> ( _ type: NullableProperty < Wrapped > . Type , forKey key: KeyedDecodingContainer < K > . Key )
60
+ throws -> NullableProperty < Wrapped > where Wrapped: Decodable {
61
+ return try decodeIfPresent ( NullableProperty< Wrapped> . self , forKey: key) ??
62
+ NullableProperty < Wrapped > ( wrappedValue: nil )
63
+ }
65
64
}
0 commit comments