Skip to content

Commit

Permalink
Refactored FunctionsSerializer.decode(_:) (#14514)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakovmanshin authored Feb 28, 2025
1 parent 1d32f36 commit 0ea9a19
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 1 addition & 3 deletions FirebaseFunctions/Sources/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions FirebaseFunctions/Sources/Internal/FunctionsSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,17 @@ 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 {
guard let value = dict["value"] as? String else {
// 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.
}
Expand Down

0 comments on commit 0ea9a19

Please sign in to comment.