Support RawRepresentable enums (#14144)

* Support raw representable enums

* Update samples
This commit is contained in:
Ladd Van Tol 2022-11-30 01:35:07 -08:00 committed by GitHub
parent 77e06466db
commit 12fd115af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 256 additions and 304 deletions

View File

@ -25,13 +25,10 @@ import Vapor{{/useVapor}}
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -51,13 +48,19 @@ import Vapor{{/useVapor}}
} }
} }
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValueToPathItem(_ source: Any) -> Any { {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -70,10 +73,7 @@ import Vapor{{/useVapor}}
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -85,7 +85,7 @@ import Vapor{{/useVapor}}
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -102,16 +102,13 @@ import Vapor{{/useVapor}}
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ internal struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ internal struct APIHelper {
} }
} }
internal static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
internal static func mapValueToPathItem(_ source: Any) -> Any { internal static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ internal struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ internal struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ internal struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }

View File

@ -24,13 +24,10 @@ public struct APIHelper {
return source.reduce(into: [String: String]()) { result, item in return source.reduce(into: [String: String]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
result[item.key] = collection result[item.key] = collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} else if let value: Any = item.value { } else if let value: Any = item.value {
result[item.key] = "\(value)" result[item.key] = convertAnyToString(value)
} }
} }
} }
@ -50,13 +47,19 @@ public struct APIHelper {
} }
} }
public static func convertAnyToString(_ value: Any?) -> String? {
guard let value = value else { return nil }
if let value = value as? any RawRepresentable {
return "\(value.rawValue)"
} else {
return "\(value)"
}
}
public static func mapValueToPathItem(_ source: Any) -> Any { public static func mapValueToPathItem(_ source: Any) -> Any {
if let collection = source as? [Any?] { if let collection = source as? [Any?] {
return collection return collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.joined(separator: ",") .joined(separator: ",")
} }
return source return source
@ -69,10 +72,7 @@ public struct APIHelper {
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value.wrappedValue as? [Any?] { if let collection = item.value.wrappedValue as? [Any?] {
let collectionValues: [String] = collection.compactMap { value in let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
if !item.value.isExplode { if !item.value.isExplode {
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
@ -84,7 +84,7 @@ public struct APIHelper {
} }
} else if let value = item.value.wrappedValue { } else if let value = item.value.wrappedValue {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }
@ -101,16 +101,13 @@ public struct APIHelper {
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
if let collection = item.value as? [Any?] { if let collection = item.value as? [Any?] {
collection collection
.compactMap { value in .compactMap { value in convertAnyToString(value) }
guard let value = value else { return nil }
return "\(value)"
}
.forEach { value in .forEach { value in
result.append(URLQueryItem(name: item.key, value: value)) result.append(URLQueryItem(name: item.key, value: value))
} }
} else if let value = item.value { } else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)")) result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
} }
} }