From 0ea9a19ca38a104cbaa7e264993875273436f7cb Mon Sep 17 00:00:00 2001 From: Yakov Manshin Date: Fri, 28 Feb 2025 23:53:36 +0100 Subject: [PATCH] Refactored `FunctionsSerializer.decode(_:)` (#14514) --- FirebaseFunctions/Sources/Functions.swift | 4 +--- .../Sources/Internal/FunctionsSerializer.swift | 7 ++++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/FirebaseFunctions/Sources/Functions.swift b/FirebaseFunctions/Sources/Functions.swift index 51e405b2f39..0af64811e35 100644 --- a/FirebaseFunctions/Sources/Functions.swift +++ b/FirebaseFunctions/Sources/Functions.swift @@ -541,10 +541,8 @@ enum FunctionsConstants { private func callableResult(fromResponseData data: Data) throws -> HTTPSCallableResult { let processedData = try processedData(fromResponseData: data) let json = try responseDataJSON(from: processedData) - // TODO: Refactor `decode(_:)` so it either returns a non-optional object or throws let payload = try serializer.decode(json) - // TODO: Remove `as Any` once `decode(_:)` is refactored - return HTTPSCallableResult(data: payload as Any) + return HTTPSCallableResult(data: payload) } private func processedData(fromResponseData data: Data) throws -> Data { diff --git a/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift b/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift index d7f6ec5ad65..53d865da3c6 100644 --- a/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift +++ b/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift @@ -58,7 +58,7 @@ final class FunctionsSerializer { } } - func decode(_ object: Any) throws -> AnyObject? { + func decode(_ object: Any) throws -> Any { // Return these types as is. PORTING NOTE: Moved from the bottom of the func for readability. if let dict = object as? NSDictionary { if let requestedType = dict["@type"] as? String { @@ -66,8 +66,9 @@ final class FunctionsSerializer { // Seems like we should throw here - but this maintains compatibility. return dict } - let result = try decodeWrappedType(requestedType, value) - if result != nil { return result } + if let result = try decodeWrappedType(requestedType, value) { + return result + } // Treat unknown types as dictionaries, so we don't crash old clients when we add types. }