From e0c0d2a72a3cfdbfb18f7dbb4760f491c7a5b419 Mon Sep 17 00:00:00 2001 From: kubo_takaichi Date: Mon, 18 May 2015 21:28:41 +0900 Subject: [PATCH] Remove dependency on SwiftyJSON --- .../src/main/resources/swift/APIs.mustache | 25 ++++--- .../main/resources/swift/Extensions.mustache | 74 +++++-------------- .../src/main/resources/swift/Models.mustache | 42 +++++++++++ .../src/main/resources/swift/model.mustache | 62 ++++++---------- 4 files changed, 97 insertions(+), 106 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache index 841d6c03888..aa39ccfcbdd 100644 --- a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache @@ -11,20 +11,20 @@ class {{projectName}}API { static let basePath = "{{^basePathOverride}}{{basePath}}{{/basePathOverride}}{{basePathOverride}}" static var credential: NSURLCredential? static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory() +} - class APIBase { - func toParameters(encodable: JSONEncodable?) -> [String: AnyObject]? { - let encoded: AnyObject? = encodable?.encode() - - if encoded! is [AnyObject] { - var dictionary = [String:AnyObject]() - for (index, item) in enumerate(encoded as! [AnyObject]) { - dictionary["\(index)"] = item - } - return dictionary - } else { - return encoded as? [String:AnyObject] +class APIBase { + func toParameters(encodable: JSONEncodable?) -> [String: AnyObject]? { + let encoded: AnyObject? = encodable?.encode() + + if encoded! is [AnyObject] { + var dictionary = [String:AnyObject]() + for (index, item) in enumerate(encoded as! [AnyObject]) { + dictionary["\(index)"] = item } + return dictionary + } else { + return encoded as? [String:AnyObject] } } } @@ -63,3 +63,4 @@ protocol RequestBuilderFactory { func getBuilder() -> RequestBuilder.Type } + diff --git a/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache b/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache index e21bec5d53e..c937db23fe3 100644 --- a/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache @@ -6,7 +6,6 @@ import Alamofire import PromiseKit -import SwiftyJSON extension Bool: JSONEncodable { func encode() -> AnyObject { return self } @@ -28,63 +27,26 @@ extension String: JSONEncodable { func encode() -> AnyObject { return self } } +private func encodeIfPossible(object: T) -> AnyObject { + if object is JSONEncodable { + return (object as! JSONEncodable).encode() + } else { + return object as! AnyObject + } +} + extension Array: JSONEncodable { func encode() -> AnyObject { - if Element.self is JSONEncodable { - return self.map { ($0 as! JSONEncodable).encode() } - } else { - return self.map { ($0 as! AnyObject) } + return self.map(encodeIfPossible) + } +} + +extension Dictionary: JSONEncodable { + func encode() -> AnyObject { + var dictionary = [NSObject:AnyObject]() + for (key, value) in self { + dictionary[key as! NSObject] = encodeIfPossible(value) } + return dictionary } } - -extension JSON { - func decode() -> Bool? { - return self.bool - } - func decode() -> Bool { - return self.boolValue - } - func decode() -> Int? { - return self.int - } - func decode() -> Int { - return self.intValue - } - func decode() -> Float? { - return self.float - } - func decode() -> Float { - return self.floatValue - } - func decode() -> Double? { - return self.double - } - func decode() -> Double { - return self.doubleValue - } - func decode() -> String? { - return self.string - } - func decode() -> String { - return self.stringValue - } - func decode() -> [T]? { - return self.array?.map({ $0 as! T }) - } - static let DateFormat: NSDateFormatter = { - let formatter = NSDateFormatter() - formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" - return formatter - }() - func decode() -> NSDate? { - return JSON.DateFormat.dateFromString(self.string ?? "") - } - func decode() -> NSData? { - return self.rawData(options: .allZeros, error: nil) - } - func decode() -> NSData { - return self.decode()! - } -} - diff --git a/modules/swagger-codegen/src/main/resources/swift/Models.mustache b/modules/swagger-codegen/src/main/resources/swift/Models.mustache index f4bbfecc779..8bdd72577db 100644 --- a/modules/swagger-codegen/src/main/resources/swift/Models.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/Models.mustache @@ -31,6 +31,7 @@ class Response { } } +private var once = dispatch_once_t() class Decoders { static private var decoders = Dictionary AnyObject)>() @@ -54,6 +55,7 @@ class Decoders { } static func decode(#clazz: T.Type, source: AnyObject) -> T { + initialize() if source is T { return source as! T } @@ -65,4 +67,44 @@ class Decoders { fatalError("Source \(source) is not convertible to type \(clazz): Maybe swagger file is insufficient") } } + + static func decodeOptional(#clazz: T.Type, source: AnyObject?) -> T? { + return source.map { (source: AnyObject) -> T in + Decoders.decode(clazz: clazz, source: source) + } + } + + static func decodeOptional(#clazz: [T].Type, source: AnyObject?) -> [T]? { + return source.map { (someSource: AnyObject) -> [T] in + Decoders.decode(clazz: clazz, source: someSource) + } + } + + static func decodeOptional(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? { + return source.map { (someSource: AnyObject) -> [Key:T] in + Decoders.decode(clazz: clazz, source: someSource) + } + } + + static private func initialize() { + dispatch_once(&once) { + let formatter = NSDateFormatter() + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'" + // Decoder for NSDate + Decoders.addDecoder(clazz: NSDate.self) { (source: AnyObject) -> NSDate in + let sourceString = source as! String + return formatter.dateFromString(sourceString)! + } {{#models}}{{#model}} + + // Decoder for {{{classname}}} + Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in + let sourceDictionary = source as! [NSObject:AnyObject] + var instance = {{classname}}(){{#vars}}{{#isEnum}} + instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}.{{datatypeWithEnum}}(rawValue: $0)! } {{#required}}!{{/required}} {{/isEnum}}{{^isEnum}} + instance.{{name}} = Decoders.decode{{^required}}Optional{{/required}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{name}}"]{{#required}}!{{/required}}){{/isEnum}}{{/vars}} + return instance + }{{/model}} + {{/models}} + } + } } diff --git a/modules/swagger-codegen/src/main/resources/swift/model.mustache b/modules/swagger-codegen/src/main/resources/swift/model.mustache index a47b2909b73..1ecd21a065a 100644 --- a/modules/swagger-codegen/src/main/resources/swift/model.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/model.mustache @@ -5,45 +5,31 @@ // https://github.com/swagger-api/swagger-codegen // -import SwiftyJSON +import Foundation -extension JSON { -} +{{#description}} -extension {{projectName}}API { - {{#description}} +/** {{description}} */{{/description}} +final class {{classname}}: JSONEncodable { +{{#vars}}{{#isEnum}} + enum {{datatypeWithEnum}}: String { {{#allowableValues}}{{#values}} + case {{enum}} = "{{raw}}"{{/values}}{{/allowableValues}} + } + {{/isEnum}}{{/vars}} + {{#vars}}{{#isEnum}}{{#description}}/** {{description}} */ + {{/description}}var {{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}}{{^isEnum}}{{#description}}/** {{description}} */ + {{/description}}var {{name}}: {{{datatype}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}} + {{/vars}} - /** {{description}} */{{/description}} - final class {{classname}}: JSONEncodable { - {{#vars}}{{#isEnum}} - enum {{datatypeWithEnum}}: String { {{#allowableValues}}{{#values}} - case {{enum}} = "{{raw}}"{{/values}}{{/allowableValues}} - } - {{/isEnum}}{{/vars}} - {{#vars}}{{#isEnum}}{{#description}}/** {{description}} */ - {{/description}}var {{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}}{{^isEnum}}{{#description}}/** {{description}} */ - {{/description}}var {{name}}: {{{datatype}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}} - {{/vars}} - - // MARK: JSONDecodable - static func decode(source: AnyObject) -> {{classname}}? { - let json = JSON(source) - var instance = {{classname}}(){{#vars}}{{#isEnum}} - instance.{{name}} = {{datatypeWithEnum}}(rawValue: json["{{name}}"].string ?? ""){{/isEnum}}{{^isEnum}} - instance.{{name}} = json["{{name}}"].decode(){{/isEnum}}{{/vars}} - return instance - } - - // MARK: JSONEncodable - func encode() -> AnyObject { - var nillableDictionary = [String:AnyObject?](){{#vars}}{{#isNotContainer}}{{#isPrimitiveType}}{{^isEnum}} - nillableDictionary["{{name}}"] = self.{{name}}{{/isEnum}}{{/isPrimitiveType}}{{#isEnum}} - nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.rawValue{{/isEnum}}{{^isPrimitiveType}} - nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isPrimitiveType}}{{/isNotContainer}}{{#isContainer}} - nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isContainer}}{{/vars}} - let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:] - return dictionary - } - }{{/model}} -} + // MARK: JSONEncodable + func encode() -> AnyObject { + var nillableDictionary = [String:AnyObject?](){{#vars}}{{#isNotContainer}}{{#isPrimitiveType}}{{^isEnum}} + nillableDictionary["{{name}}"] = self.{{name}}{{/isEnum}}{{/isPrimitiveType}}{{#isEnum}} + nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.rawValue{{/isEnum}}{{^isPrimitiveType}} + nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isPrimitiveType}}{{/isNotContainer}}{{#isContainer}} + nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isContainer}}{{/vars}} + let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:] + return dictionary + } +}{{/model}} {{/models}}