|
| 1 | +// APIHelper.swift |
| 2 | +// |
| 3 | +// Generated by openapi-generator |
| 4 | +// https://openapi-generator.tech |
| 5 | +// |
| 6 | + |
| 7 | +import Foundation{{#useVapor}} |
| 8 | +import Vapor{{/useVapor}} |
| 9 | + |
| 10 | +{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct APIHelper { |
| 11 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNil(_ source: [String: Any?]) -> [String: Any]? { |
| 12 | + let destination = source.reduce(into: [String: Any]()) { result, item in |
| 13 | + if let value = item.value { |
| 14 | + result[item.key] = value |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + if destination.isEmpty { |
| 19 | + return nil |
| 20 | + } |
| 21 | + return destination |
| 22 | + } |
| 23 | + |
| 24 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { |
| 25 | + return source.reduce(into: [String: String]()) { result, item in |
| 26 | + if let collection = item.value as? [Any?] { |
| 27 | + result[item.key] = collection |
| 28 | + .compactMap { value in convertAnyToString(value) } |
| 29 | + .joined(separator: ",") |
| 30 | + } else if let value: Any = item.value { |
| 31 | + result[item.key] = convertAnyToString(value) |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertBoolToString(_ source: [String: Any]?) -> [String: Any]? { |
| 37 | + guard let source = source else { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + return source.reduce(into: [String: Any]()) { result, item in |
| 42 | + switch item.value { |
| 43 | + case let x as Bool: |
| 44 | + result[item.key] = x.description |
| 45 | + default: |
| 46 | + result[item.key] = item.value |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertAnyToString(_ value: Any?) -> String? { |
| 52 | + guard let value = value else { return nil } |
| 53 | + if let value = value as? any RawRepresentable { |
| 54 | + return "\(value.rawValue)" |
| 55 | + } else { |
| 56 | + return "\(value)" |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValueToPathItem(_ source: Any) -> Any { |
| 61 | + if let collection = source as? [Any?] { |
| 62 | + return collection |
| 63 | + .compactMap { value in convertAnyToString(value) } |
| 64 | + .joined(separator: ",") |
| 65 | + } else if let value = source as? any RawRepresentable { |
| 66 | + return "\(value.rawValue)" |
| 67 | + } |
| 68 | + return source |
| 69 | + } |
| 70 | + |
| 71 | + /// maps all values from source to query parameters |
| 72 | + /// |
| 73 | + /// explode attribute is respected: collection values might be either joined or split up into separate key value pairs |
| 74 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: (wrappedValue: Any?, isExplode: Bool)]) -> [URLQueryItem]? { |
| 75 | + let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in |
| 76 | + if let collection = item.value.wrappedValue as? [Any?] { |
| 77 | +
|
| 78 | + let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) } |
| 79 | + |
| 80 | + if !item.value.isExplode { |
| 81 | + result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ","))) |
| 82 | + } else { |
| 83 | + collectionValues |
| 84 | + .forEach { value in |
| 85 | + result.append(URLQueryItem(name: item.key, value: value)) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + } else if let value = item.value.wrappedValue { |
| 90 | + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if destination.isEmpty { |
| 95 | + return nil |
| 96 | + } |
| 97 | + return destination.sorted { $0.name < $1.name } |
| 98 | + } |
| 99 | + |
| 100 | + /// maps all values from source to query parameters |
| 101 | + /// |
| 102 | + /// collection values are always exploded |
| 103 | + {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? { |
| 104 | + let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in |
| 105 | + if let collection = item.value as? [Any?] { |
| 106 | + collection |
| 107 | + .compactMap { value in convertAnyToString(value) } |
| 108 | + .forEach { value in |
| 109 | + result.append(URLQueryItem(name: item.key, value: value)) |
| 110 | + } |
| 111 | + |
| 112 | + } else if let value = item.value { |
| 113 | + result.append(URLQueryItem(name: item.key, value: convertAnyToString(value))) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if destination.isEmpty { |
| 118 | + return nil |
| 119 | + } |
| 120 | + return destination.sorted { $0.name < $1.name } |
| 121 | + } |
| 122 | +} |
0 commit comments