forked from loafle/openapi-generator-original
* [swift5] Fix additionalProps inner type * [swift5] Add and use AnyCodable type instead of any * [swift5] Use the original petstore w/fake models for testing default * [swift5] Update swift-tools-version to 5.1 * [swift5] Update samples and docs
227 lines
5.6 KiB
Swift
227 lines
5.6 KiB
Swift
// Extensions.swift
|
|
//
|
|
// Generated by openapi-generator
|
|
// https://openapi-generator.tech
|
|
//
|
|
|
|
import Foundation
|
|
import AnyCodable
|
|
|
|
extension Bool: JSONEncodable {
|
|
func encodeToJSON() -> Any { return self as Any }
|
|
}
|
|
|
|
extension Float: JSONEncodable {
|
|
func encodeToJSON() -> Any { return self as Any }
|
|
}
|
|
|
|
extension Int: JSONEncodable {
|
|
func encodeToJSON() -> Any { return self as Any }
|
|
}
|
|
|
|
extension Int32: JSONEncodable {
|
|
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
|
}
|
|
|
|
extension Int64: JSONEncodable {
|
|
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
|
}
|
|
|
|
extension Double: JSONEncodable {
|
|
func encodeToJSON() -> Any { return self as Any }
|
|
}
|
|
|
|
extension String: JSONEncodable {
|
|
func encodeToJSON() -> Any { return self as Any }
|
|
}
|
|
|
|
extension RawRepresentable where RawValue: JSONEncodable {
|
|
func encodeToJSON() -> Any { return self.rawValue as Any }
|
|
}
|
|
|
|
private func encodeIfPossible<T>(_ object: T) -> Any {
|
|
if let encodableObject = object as? JSONEncodable {
|
|
return encodableObject.encodeToJSON()
|
|
} else {
|
|
return object as Any
|
|
}
|
|
}
|
|
|
|
extension Array: JSONEncodable {
|
|
func encodeToJSON() -> Any {
|
|
return self.map(encodeIfPossible)
|
|
}
|
|
}
|
|
|
|
extension Set: JSONEncodable {
|
|
func encodeToJSON() -> Any {
|
|
return Array(self).encodeToJSON()
|
|
}
|
|
}
|
|
|
|
extension Dictionary: JSONEncodable {
|
|
func encodeToJSON() -> Any {
|
|
var dictionary = [AnyHashable: Any]()
|
|
for (key, value) in self {
|
|
dictionary[key] = encodeIfPossible(value)
|
|
}
|
|
return dictionary as Any
|
|
}
|
|
}
|
|
|
|
extension Data: JSONEncodable {
|
|
func encodeToJSON() -> Any {
|
|
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
|
}
|
|
}
|
|
|
|
extension Date: JSONEncodable {
|
|
func encodeToJSON() -> Any {
|
|
return CodableHelper.dateFormatter.string(from: self) as Any
|
|
}
|
|
}
|
|
|
|
extension URL: JSONEncodable {
|
|
func encodeToJSON() -> Any {
|
|
return self
|
|
}
|
|
}
|
|
|
|
extension UUID: JSONEncodable {
|
|
func encodeToJSON() -> Any {
|
|
return self.uuidString
|
|
}
|
|
}
|
|
|
|
extension String: CodingKey {
|
|
|
|
public var stringValue: String {
|
|
return self
|
|
}
|
|
|
|
public init?(stringValue: String) {
|
|
self.init(stringLiteral: stringValue)
|
|
}
|
|
|
|
public var intValue: Int? {
|
|
return nil
|
|
}
|
|
|
|
public init?(intValue: Int) {
|
|
return nil
|
|
}
|
|
|
|
}
|
|
|
|
extension KeyedEncodingContainerProtocol {
|
|
|
|
public mutating func encodeArray<T>(_ values: [T], forKey key: Self.Key) throws where T: Encodable {
|
|
var arrayContainer = nestedUnkeyedContainer(forKey: key)
|
|
try arrayContainer.encode(contentsOf: values)
|
|
}
|
|
|
|
public mutating func encodeArrayIfPresent<T>(_ values: [T]?, forKey key: Self.Key) throws where T: Encodable {
|
|
if let values = values {
|
|
try encodeArray(values, forKey: key)
|
|
}
|
|
}
|
|
|
|
public mutating func encodeMap<T>(_ pairs: [Self.Key: T]) throws where T: Encodable {
|
|
for (key, value) in pairs {
|
|
try encode(value, forKey: key)
|
|
}
|
|
}
|
|
|
|
public mutating func encodeMapIfPresent<T>(_ pairs: [Self.Key: T]?) throws where T: Encodable {
|
|
if let pairs = pairs {
|
|
try encodeMap(pairs)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension KeyedDecodingContainerProtocol {
|
|
|
|
public func decodeArray<T>(_ type: T.Type, forKey key: Self.Key) throws -> [T] where T: Decodable {
|
|
var tmpArray = [T]()
|
|
|
|
var nestedContainer = try nestedUnkeyedContainer(forKey: key)
|
|
while !nestedContainer.isAtEnd {
|
|
let arrayValue = try nestedContainer.decode(T.self)
|
|
tmpArray.append(arrayValue)
|
|
}
|
|
|
|
return tmpArray
|
|
}
|
|
|
|
public func decodeArrayIfPresent<T>(_ type: T.Type, forKey key: Self.Key) throws -> [T]? where T: Decodable {
|
|
var tmpArray: [T]?
|
|
|
|
if contains(key) {
|
|
tmpArray = try decodeArray(T.self, forKey: key)
|
|
}
|
|
|
|
return tmpArray
|
|
}
|
|
|
|
public func decodeMap<T>(_ type: T.Type, excludedKeys: Set<Self.Key>) throws -> [Self.Key: T] where T: Decodable {
|
|
var map: [Self.Key: T] = [:]
|
|
|
|
for key in allKeys {
|
|
if !excludedKeys.contains(key) {
|
|
let value = try decode(T.self, forKey: key)
|
|
map[key] = value
|
|
}
|
|
}
|
|
|
|
return map
|
|
}
|
|
|
|
}
|
|
|
|
extension HTTPURLResponse {
|
|
var isStatusCodeSuccessful: Bool {
|
|
return Array(200 ..< 300).contains(statusCode)
|
|
}
|
|
}
|
|
|
|
extension AnyCodable: Hashable {
|
|
public func hash(into hasher: inout Hasher) {
|
|
switch value {
|
|
case let value as Bool:
|
|
hasher.combine(value)
|
|
case let value as Int:
|
|
hasher.combine(value)
|
|
case let value as Int8:
|
|
hasher.combine(value)
|
|
case let value as Int16:
|
|
hasher.combine(value)
|
|
case let value as Int32:
|
|
hasher.combine(value)
|
|
case let value as Int64:
|
|
hasher.combine(value)
|
|
case let value as UInt:
|
|
hasher.combine(value)
|
|
case let value as UInt8:
|
|
hasher.combine(value)
|
|
case let value as UInt16:
|
|
hasher.combine(value)
|
|
case let value as UInt32:
|
|
hasher.combine(value)
|
|
case let value as UInt64:
|
|
hasher.combine(value)
|
|
case let value as Float:
|
|
hasher.combine(value)
|
|
case let value as Double:
|
|
hasher.combine(value)
|
|
case let value as String:
|
|
hasher.combine(value)
|
|
case let value as [String: AnyCodable]:
|
|
hasher.combine(value)
|
|
case let value as [AnyCodable]:
|
|
hasher.combine(value)
|
|
default:
|
|
hasher.combine(0)
|
|
}
|
|
}
|
|
} |