From f06278b2668c7d5d5bbbc11cd3a34a158b418a02 Mon Sep 17 00:00:00 2001 From: Didier Garcia Date: Wed, 17 Jan 2024 09:16:29 -0500 Subject: [PATCH] =?UTF-8?q?Fix=20optional=20wrapping=20issue=20in=20mapToS?= =?UTF-8?q?trings=20function=20by=20explicitly=E2=80=A6=20(#24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix optional wrapping issue in mapToStrings function by explicitedly allowing Any? values then unwrapping them. --- .../SegmentFirebase/FirebaseDestination.swift | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Sources/SegmentFirebase/FirebaseDestination.swift b/Sources/SegmentFirebase/FirebaseDestination.swift index 6f93fc6..2740d1b 100644 --- a/Sources/SegmentFirebase/FirebaseDestination.swift +++ b/Sources/SegmentFirebase/FirebaseDestination.swift @@ -201,13 +201,19 @@ extension FirebaseDestination { } // Makes sure all traits are string based for Firebase API - func mapToStrings(_ mapDictionary: [String: Any], finalize: (String, String) -> Void) { + func mapToStrings(_ mapDictionary: [String: Any?], finalize: (String, String) -> Void) { for (key, data) in mapDictionary { - var dataString = data as? String ?? "\(data)" - let keyString = key.replacingOccurrences(of: " ", with: "_") - dataString = dataString.trimmingCharacters(in: .whitespacesAndNewlines) - finalize(keyString, dataString) + + // Since dictionary values can be Optional we have to unwrap them + // before encoding so that we don't encode them as "Optional(*)" + // Note: nil values are NOT encoded. + if let d = data { + var dataString = d as? String ?? "\(d)" + let keyString = key.replacingOccurrences(of: " ", with: "_") + dataString = dataString.trimmingCharacters(in: .whitespacesAndNewlines) + finalize(keyString, dataString) + } } } }