16
16
17
17
import FirebaseFirestore
18
18
19
- /// Wraps around a `Optional` such that it explicitly sets the corresponding document field
20
- /// to Null, instead of not setting the field at all.
19
+ #if swift(>=5.1)
20
+
21
+ /// Wraps an `Optional` field in a `Codable` object such that when the field has
22
+ /// a `nil` value it will encode to a null value in Firestore. Normally,
23
+ /// optional fields are omitted from the encoded document.
24
+ ///
25
+ /// This is useful for ensuring a field is present in a Firestore document, even
26
+ /// when there is no associated value.
27
+ @propertyWrapper
28
+ public struct ExplicitNull < Value> {
29
+ var value : Value ?
30
+
31
+ public init ( wrappedValue value: Value ? ) {
32
+ self . value = value
33
+ }
34
+
35
+ public var wrappedValue : Value ? {
36
+ get { value }
37
+ set { value = newValue }
38
+ }
39
+ }
40
+
41
+ extension ExplicitNull : Equatable where Value: Equatable { }
42
+
43
+ extension ExplicitNull : Encodable where Value: Encodable {
44
+ public func encode( to encoder: Encoder ) throws {
45
+ var container = encoder. singleValueContainer ( )
46
+ if let value = value {
47
+ try container. encode ( value)
48
+ } else {
49
+ try container. encodeNil ( )
50
+ }
51
+ }
52
+ }
53
+
54
+ extension ExplicitNull : Decodable where Value: Decodable {
55
+ public init ( from decoder: Decoder ) throws {
56
+ let container = try decoder. singleValueContainer ( )
57
+ if container. decodeNil ( ) {
58
+ value = nil
59
+ } else {
60
+ value = try container. decode ( Value . self)
61
+ }
62
+ }
63
+ }
64
+
65
+ #endif // swift(>=5.1)
66
+
67
+ /// A compatibility version of `ExplicitNull` that does not use property
68
+ /// wrappers, suitable for use in older versions of Swift.
21
69
///
22
- /// When encoded into a Firestore document by `Firestore.Encoder`, an `Optional` field with
23
- /// `nil` value will be skipped, so the resulting document simply will not have the field.
70
+ /// Wraps an `Optional` field in a `Codable` object such that when the field has
71
+ /// a `nil` value it will encode to a null value in Firestore. Normally,
72
+ /// optional fields are omitted from the encoded document.
24
73
///
25
- /// When setting the field to `Null` instead of skipping it is desired, `ExplicitNull` can be
26
- /// used instead of `Optional`.
27
- public enum ExplicitNull < Wrapped> {
74
+ /// This is useful for ensuring a field is present in a Firestore document, even
75
+ /// when there is no associated value.
76
+ @available ( swift, deprecated: 5.1 )
77
+ public enum Swift4ExplicitNull < Wrapped> {
28
78
case none
29
79
case some( Wrapped )
30
80
@@ -49,9 +99,11 @@ public enum ExplicitNull<Wrapped> {
49
99
}
50
100
}
51
101
52
- extension ExplicitNull : Equatable where Wrapped: Equatable { }
102
+ @available ( swift, deprecated: 5.1 )
103
+ extension Swift4ExplicitNull : Equatable where Wrapped: Equatable { }
53
104
54
- extension ExplicitNull : Encodable where Wrapped: Encodable {
105
+ @available ( swift, deprecated: 5.1 )
106
+ extension Swift4ExplicitNull : Encodable where Wrapped: Encodable {
55
107
public func encode( to encoder: Encoder ) throws {
56
108
var container = encoder. singleValueContainer ( )
57
109
switch self {
@@ -63,7 +115,8 @@ extension ExplicitNull: Encodable where Wrapped: Encodable {
63
115
}
64
116
}
65
117
66
- extension ExplicitNull : Decodable where Wrapped: Decodable {
118
+ @available ( swift, deprecated: 5.1 )
119
+ extension Swift4ExplicitNull : Decodable where Wrapped: Decodable {
67
120
public init ( from decoder: Decoder ) throws {
68
121
let container = try decoder. singleValueContainer ( )
69
122
if container. decodeNil ( ) {
0 commit comments