Skip to content

Commit 62909cf

Browse files
authored
Create dictionary with compactMap (#10027)
1 parent 5a270d8 commit 62909cf

File tree

1 file changed

+16
-44
lines changed

1 file changed

+16
-44
lines changed

FirebaseStorage/Sources/StorageMetadata.swift

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -107,50 +107,22 @@ import FirebaseStorageInternal
107107
* @return A Dictionary that represents the contents of the metadata.
108108
*/
109109
@objc open func dictionaryRepresentation() -> [String: AnyHashable] {
110-
var dictionary: [String: AnyHashable] = [:]
111-
if let bucket = bucket {
112-
dictionary["bucket"] = bucket
113-
}
114-
if let cacheControl = cacheControl {
115-
dictionary["cacheControl"] = cacheControl
116-
}
117-
if let contentDisposition = contentDisposition {
118-
dictionary["contentDisposition"] = contentDisposition
119-
}
120-
if let contentEncoding = contentEncoding {
121-
dictionary["contentEncoding"] = contentEncoding
122-
}
123-
if let contentLanguage = contentLanguage {
124-
dictionary["contentLanguage"] = contentLanguage
125-
}
126-
if let contentType = contentType {
127-
dictionary["contentType"] = contentType
128-
}
129-
if let md5Hash = md5Hash {
130-
dictionary["md5Hash"] = md5Hash
131-
}
132-
if let customMetadata = customMetadata {
133-
dictionary["metadata"] = customMetadata
134-
}
135-
if size != 0 {
136-
dictionary["size"] = size
137-
}
138-
if generation != 0 {
139-
dictionary["generation"] = "\(generation)"
140-
}
141-
if metageneration != 0 {
142-
dictionary["metageneration"] = "\(metageneration)"
143-
}
144-
if let timeCreated = timeCreated {
145-
dictionary["timeCreated"] = StorageMetadata.RFC3339StringFromDate(timeCreated)
146-
}
147-
if let updated = updated {
148-
dictionary["updated"] = StorageMetadata.RFC3339StringFromDate(updated)
149-
}
150-
if let path = path {
151-
dictionary["name"] = path
152-
}
153-
return dictionary
110+
let stringFromDate = StorageMetadata.RFC3339StringFromDate
111+
return [
112+
"bucket": bucket,
113+
"cacheControl": cacheControl,
114+
"contentDisposition": contentDisposition,
115+
"contentEncoding": contentEncoding,
116+
"contentLanguage": contentLanguage,
117+
"contentType": contentType,
118+
"md5Hash": md5Hash,
119+
"size": size != 0 ? size : nil,
120+
"generation": generation != 0 ? "\(generation)" : nil,
121+
"metageneration": metageneration != 0 ? "\(metageneration)" : nil,
122+
"timeCreated": timeCreated.map(stringFromDate),
123+
"updated": updated.map(stringFromDate),
124+
"name": path,
125+
].compactMapValues { $0 }.merging(["metadata": customMetadata]) { current, _ in current }
154126
}
155127

156128
/**

0 commit comments

Comments
 (0)