/samples/client/petstore/swift/PetstoreClient/Classes/Swaggers/Models.swift

https://gitlab.com/akkhil2012/swagger-codegen · Swift · 283 lines · 220 code · 36 blank · 27 comment · 15 complexity · 9ef51fb522fe94f64bd04288251da550 MD5 · raw file

  1. // Models.swift
  2. //
  3. // Generated by swagger-codegen
  4. // https://github.com/swagger-api/swagger-codegen
  5. //
  6. import Foundation
  7. protocol JSONEncodable {
  8. func encodeToJSON() -> AnyObject
  9. }
  10. public class Response<T> {
  11. public let statusCode: Int
  12. public let header: [String: String]
  13. public let body: T
  14. public init(statusCode: Int, header: [String: String], body: T) {
  15. self.statusCode = statusCode
  16. self.header = header
  17. self.body = body
  18. }
  19. public convenience init(response: NSHTTPURLResponse, body: T) {
  20. let rawHeader = response.allHeaderFields
  21. var header = [String:String]()
  22. for (key, value) in rawHeader {
  23. header[key as! String] = value as? String
  24. }
  25. self.init(statusCode: response.statusCode, header: header, body: body)
  26. }
  27. }
  28. private var once = dispatch_once_t()
  29. class Decoders {
  30. static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
  31. static func addDecoder<T>(clazz clazz: T.Type, decoder: ((AnyObject) -> T)) {
  32. let key = "\(T.self)"
  33. decoders[key] = { decoder($0) as! AnyObject }
  34. }
  35. static func decode<T>(clazz clazz: [T].Type, source: AnyObject) -> [T] {
  36. let array = source as! [AnyObject]
  37. return array.map { Decoders.decode(clazz: T.self, source: $0) }
  38. }
  39. static func decode<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
  40. let sourceDictinoary = source as! [Key: AnyObject]
  41. var dictionary = [Key:T]()
  42. for (key, value) in sourceDictinoary {
  43. dictionary[key] = Decoders.decode(clazz: T.self, source: value)
  44. }
  45. return dictionary
  46. }
  47. static func decode<T>(clazz clazz: T.Type, source: AnyObject) -> T {
  48. initialize()
  49. if source is T {
  50. return source as! T
  51. }
  52. let key = "\(T.self)"
  53. if let decoder = decoders[key] {
  54. return decoder(source) as! T
  55. } else {
  56. fatalError("Source \(source) is not convertible to type \(clazz): Maybe swagger file is insufficient")
  57. }
  58. }
  59. static func decodeOptional<T>(clazz clazz: T.Type, source: AnyObject?) -> T? {
  60. if source is NSNull {
  61. return nil
  62. }
  63. return source.map { (source: AnyObject) -> T in
  64. Decoders.decode(clazz: clazz, source: source)
  65. }
  66. }
  67. static func decodeOptional<T>(clazz clazz: [T].Type, source: AnyObject?) -> [T]? {
  68. if source is NSNull {
  69. return nil
  70. }
  71. return source.map { (someSource: AnyObject) -> [T] in
  72. Decoders.decode(clazz: clazz, source: someSource)
  73. }
  74. }
  75. static func decodeOptional<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
  76. if source is NSNull {
  77. return nil
  78. }
  79. return source.map { (someSource: AnyObject) -> [Key:T] in
  80. Decoders.decode(clazz: clazz, source: someSource)
  81. }
  82. }
  83. static private func initialize() {
  84. dispatch_once(&once) {
  85. let formatters = [
  86. "yyyy-MM-dd",
  87. "yyyy-MM-dd'T'HH:mm:ssZZZZZ",
  88. "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
  89. "yyyy-MM-dd'T'HH:mm:ss'Z'"
  90. ].map { (format: String) -> NSDateFormatter in
  91. let formatter = NSDateFormatter()
  92. formatter.dateFormat = format
  93. return formatter
  94. }
  95. // Decoder for NSDate
  96. Decoders.addDecoder(clazz: NSDate.self) { (source: AnyObject) -> NSDate in
  97. if let sourceString = source as? String {
  98. for formatter in formatters {
  99. if let date = formatter.dateFromString(sourceString) {
  100. return date
  101. }
  102. }
  103. }
  104. if let sourceInt = source as? Int {
  105. // treat as a java date
  106. return NSDate(timeIntervalSince1970: Double(sourceInt / 1000) )
  107. }
  108. fatalError("formatter failed to parse \(source)")
  109. }
  110. // Decoder for [Category]
  111. Decoders.addDecoder(clazz: [Category].self) { (source: AnyObject) -> [Category] in
  112. return Decoders.decode(clazz: [Category].self, source: source)
  113. }
  114. // Decoder for Category
  115. Decoders.addDecoder(clazz: Category.self) { (source: AnyObject) -> Category in
  116. let sourceDictionary = source as! [NSObject:AnyObject]
  117. let instance = Category()
  118. instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
  119. instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
  120. return instance
  121. }
  122. // Decoder for [InlineResponse200]
  123. Decoders.addDecoder(clazz: [InlineResponse200].self) { (source: AnyObject) -> [InlineResponse200] in
  124. return Decoders.decode(clazz: [InlineResponse200].self, source: source)
  125. }
  126. // Decoder for InlineResponse200
  127. Decoders.addDecoder(clazz: InlineResponse200.self) { (source: AnyObject) -> InlineResponse200 in
  128. let sourceDictionary = source as! [NSObject:AnyObject]
  129. let instance = InlineResponse200()
  130. instance.tags = Decoders.decodeOptional(clazz: Array.self, source: sourceDictionary["tags"])
  131. instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
  132. instance.category = Decoders.decodeOptional(clazz: AnyObject.self, source: sourceDictionary["category"])
  133. instance.status = InlineResponse200.Status(rawValue: (sourceDictionary["status"] as? String) ?? "")
  134. instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
  135. instance.photoUrls = Decoders.decodeOptional(clazz: Array.self, source: sourceDictionary["photoUrls"])
  136. return instance
  137. }
  138. // Decoder for [Model200Response]
  139. Decoders.addDecoder(clazz: [Model200Response].self) { (source: AnyObject) -> [Model200Response] in
  140. return Decoders.decode(clazz: [Model200Response].self, source: source)
  141. }
  142. // Decoder for Model200Response
  143. Decoders.addDecoder(clazz: Model200Response.self) { (source: AnyObject) -> Model200Response in
  144. let sourceDictionary = source as! [NSObject:AnyObject]
  145. let instance = Model200Response()
  146. instance.name = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["name"])
  147. return instance
  148. }
  149. // Decoder for [ModelReturn]
  150. Decoders.addDecoder(clazz: [ModelReturn].self) { (source: AnyObject) -> [ModelReturn] in
  151. return Decoders.decode(clazz: [ModelReturn].self, source: source)
  152. }
  153. // Decoder for ModelReturn
  154. Decoders.addDecoder(clazz: ModelReturn.self) { (source: AnyObject) -> ModelReturn in
  155. let sourceDictionary = source as! [NSObject:AnyObject]
  156. let instance = ModelReturn()
  157. instance._return = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["return"])
  158. return instance
  159. }
  160. // Decoder for [Name]
  161. Decoders.addDecoder(clazz: [Name].self) { (source: AnyObject) -> [Name] in
  162. return Decoders.decode(clazz: [Name].self, source: source)
  163. }
  164. // Decoder for Name
  165. Decoders.addDecoder(clazz: Name.self) { (source: AnyObject) -> Name in
  166. let sourceDictionary = source as! [NSObject:AnyObject]
  167. let instance = Name()
  168. instance.name = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["name"])
  169. instance.snakeCase = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["snake_case"])
  170. return instance
  171. }
  172. // Decoder for [Order]
  173. Decoders.addDecoder(clazz: [Order].self) { (source: AnyObject) -> [Order] in
  174. return Decoders.decode(clazz: [Order].self, source: source)
  175. }
  176. // Decoder for Order
  177. Decoders.addDecoder(clazz: Order.self) { (source: AnyObject) -> Order in
  178. let sourceDictionary = source as! [NSObject:AnyObject]
  179. let instance = Order()
  180. instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
  181. instance.petId = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["petId"])
  182. instance.quantity = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["quantity"])
  183. instance.shipDate = Decoders.decodeOptional(clazz: NSDate.self, source: sourceDictionary["shipDate"])
  184. instance.status = Order.Status(rawValue: (sourceDictionary["status"] as? String) ?? "")
  185. instance.complete = Decoders.decodeOptional(clazz: Bool.self, source: sourceDictionary["complete"])
  186. return instance
  187. }
  188. // Decoder for [Pet]
  189. Decoders.addDecoder(clazz: [Pet].self) { (source: AnyObject) -> [Pet] in
  190. return Decoders.decode(clazz: [Pet].self, source: source)
  191. }
  192. // Decoder for Pet
  193. Decoders.addDecoder(clazz: Pet.self) { (source: AnyObject) -> Pet in
  194. let sourceDictionary = source as! [NSObject:AnyObject]
  195. let instance = Pet()
  196. instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
  197. instance.category = Decoders.decodeOptional(clazz: Category.self, source: sourceDictionary["category"])
  198. instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
  199. instance.photoUrls = Decoders.decodeOptional(clazz: Array.self, source: sourceDictionary["photoUrls"])
  200. instance.tags = Decoders.decodeOptional(clazz: Array.self, source: sourceDictionary["tags"])
  201. instance.status = Pet.Status(rawValue: (sourceDictionary["status"] as? String) ?? "")
  202. return instance
  203. }
  204. // Decoder for [SpecialModelName]
  205. Decoders.addDecoder(clazz: [SpecialModelName].self) { (source: AnyObject) -> [SpecialModelName] in
  206. return Decoders.decode(clazz: [SpecialModelName].self, source: source)
  207. }
  208. // Decoder for SpecialModelName
  209. Decoders.addDecoder(clazz: SpecialModelName.self) { (source: AnyObject) -> SpecialModelName in
  210. let sourceDictionary = source as! [NSObject:AnyObject]
  211. let instance = SpecialModelName()
  212. instance.specialPropertyName = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["$special[property.name]"])
  213. return instance
  214. }
  215. // Decoder for [Tag]
  216. Decoders.addDecoder(clazz: [Tag].self) { (source: AnyObject) -> [Tag] in
  217. return Decoders.decode(clazz: [Tag].self, source: source)
  218. }
  219. // Decoder for Tag
  220. Decoders.addDecoder(clazz: Tag.self) { (source: AnyObject) -> Tag in
  221. let sourceDictionary = source as! [NSObject:AnyObject]
  222. let instance = Tag()
  223. instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
  224. instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
  225. return instance
  226. }
  227. // Decoder for [User]
  228. Decoders.addDecoder(clazz: [User].self) { (source: AnyObject) -> [User] in
  229. return Decoders.decode(clazz: [User].self, source: source)
  230. }
  231. // Decoder for User
  232. Decoders.addDecoder(clazz: User.self) { (source: AnyObject) -> User in
  233. let sourceDictionary = source as! [NSObject:AnyObject]
  234. let instance = User()
  235. instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
  236. instance.username = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["username"])
  237. instance.firstName = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["firstName"])
  238. instance.lastName = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["lastName"])
  239. instance.email = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["email"])
  240. instance.password = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["password"])
  241. instance.phone = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["phone"])
  242. instance.userStatus = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["userStatus"])
  243. return instance
  244. }
  245. }
  246. }
  247. }