From 10749cb641c8d3a9e6c273dc8b948a7f3c9bb162 Mon Sep 17 00:00:00 2001 From: Yakov Manshin Date: Tue, 4 Mar 2025 14:32:04 +0100 Subject: [PATCH] Refactored `FunctionsSerializer.encode(_:)` * Fewer reference types * Fewer mutable objects --- .../Internal/FunctionsSerializer.swift | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift b/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift index 53d865da3c6..5fa9d4e3fe2 100644 --- a/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift +++ b/FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift @@ -31,28 +31,23 @@ extension FunctionsSerializer { final class FunctionsSerializer { // MARK: - Internal APIs - func encode(_ object: Any) throws -> AnyObject { + func encode(_ object: Any) throws -> Any { if object is NSNull { - return object as AnyObject + return object } else if object is NSNumber { return try encodeNumber(object as! NSNumber) } else if object is NSString { - return object as AnyObject - } else if object is NSDictionary { - let dict = object as! NSDictionary + return object + } else if let dict = object as? NSDictionary { let encoded = NSMutableDictionary() try dict.forEach { key, value in encoded[key] = try encode(value) } return encoded - } else if object is NSArray { - let array = object as! NSArray - let encoded = NSMutableArray() - try array.forEach { element in - try encoded.add(encode(element)) + } else if let array = object as? NSArray { + return try array.map { element in + try encode(element) } - return encoded - } else { throw Error.unsupportedType(typeName: typeName(of: object)) }