Skip to content

Commit 3cc0104

Browse files
committed
[Update] APIParameter adds handling of AnyObject
Also changed to prioritize whether the value conforms to the `Encodable` protocol to optimize performance
1 parent b822290 commit 3cc0104

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

Sources/RequestInfo/APIParameter.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ extension Array: APIParameter {
2626
(self as [Any?])
2727
.lazy
2828
.compactMap {
29+
if let value = $0 as? Encodable { return value }
2930
if let value = $0 as? APIParameter { return value.toParameters }
30-
return $0 as? Encodable
31+
return mapAnyObjectToEncodable($0 as? AnyObject)
3132
}
3233
.map { AnyEncodable($0) }
3334
}
@@ -39,9 +40,28 @@ extension Dictionary: APIParameter where Key == String {
3940
public var toParameters: Encodable {
4041
(self as [String: Any?])
4142
.compactMapValues {
43+
if let value = $0 as? Encodable { return value }
4244
if let value = $0 as? APIParameter { return value.toParameters }
43-
return $0 as? Encodable
45+
return mapAnyObjectToEncodable($0 as? AnyObject)
4446
}
4547
.mapValues { AnyEncodable($0) }
4648
}
4749
}
50+
51+
// MARK: - Tools
52+
53+
fileprivate extension APIParameter {
54+
func mapAnyObjectToEncodable(_ value: AnyObject?) -> Encodable? {
55+
guard let _value = value else { return nil }
56+
57+
if let result = _value as? String { return result }
58+
if let result = _value as? Int { return result }
59+
if let result = _value as? Double { return result }
60+
if let result = _value as? Bool { return result }
61+
if let result = _value as? Data { return result }
62+
if let result = _value as? [String: Any] { return result.toParameters }
63+
if let result = _value as? [Any] { return result.toParameters }
64+
65+
return nil
66+
}
67+
}

0 commit comments

Comments
 (0)