From 7be35bb02eecb73d8488dc4d16871cd8bbba2707 Mon Sep 17 00:00:00 2001 From: kubo_takaichi Date: Tue, 26 May 2015 10:59:55 +0900 Subject: [PATCH] Change method name to avoid name collision --- .../src/main/resources/swift/APIs.mustache | 2 +- .../main/resources/swift/Extensions.mustache | 29 ++++++++++++++----- .../src/main/resources/swift/Models.mustache | 2 +- .../src/main/resources/swift/api.mustache | 2 +- .../src/main/resources/swift/model.mustache | 6 ++-- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache index aa39ccfcbdd..d9fcb84c125 100644 --- a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache @@ -15,7 +15,7 @@ class {{projectName}}API { class APIBase { func toParameters(encodable: JSONEncodable?) -> [String: AnyObject]? { - let encoded: AnyObject? = encodable?.encode() + let encoded: AnyObject? = encodable?.encodeToJSON() if encoded! is [AnyObject] { var dictionary = [String:AnyObject]() diff --git a/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache b/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache index c937db23fe3..a06ebae7026 100644 --- a/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/Extensions.mustache @@ -8,41 +8,41 @@ import Alamofire import PromiseKit extension Bool: JSONEncodable { - func encode() -> AnyObject { return self } + func encodeToJSON() -> AnyObject { return self } } extension Float: JSONEncodable { - func encode() -> AnyObject { return self } + func encodeToJSON() -> AnyObject { return self } } extension Int: JSONEncodable { - func encode() -> AnyObject { return self } + func encodeToJSON() -> AnyObject { return self } } extension Double: JSONEncodable { - func encode() -> AnyObject { return self } + func encodeToJSON() -> AnyObject { return self } } extension String: JSONEncodable { - func encode() -> AnyObject { return self } + func encodeToJSON() -> AnyObject { return self } } private func encodeIfPossible(object: T) -> AnyObject { if object is JSONEncodable { - return (object as! JSONEncodable).encode() + return (object as! JSONEncodable).encodeToJSON() } else { return object as! AnyObject } } extension Array: JSONEncodable { - func encode() -> AnyObject { + func encodeToJSON() -> AnyObject { return self.map(encodeIfPossible) } } extension Dictionary: JSONEncodable { - func encode() -> AnyObject { + func encodeToJSON() -> AnyObject { var dictionary = [NSObject:AnyObject]() for (key, value) in self { dictionary[key as! NSObject] = encodeIfPossible(value) @@ -50,3 +50,16 @@ extension Dictionary: JSONEncodable { return dictionary } } + + +private let dateFormatter: NSDateFormatter = { + let dateFormatter = NSDateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + return dateFormatter +}() + +extension NSDate: JSONEncodable { + func encodeToJSON() -> AnyObject { + return dateFormatter.stringFromDate(self) + } +} diff --git a/modules/swagger-codegen/src/main/resources/swift/Models.mustache b/modules/swagger-codegen/src/main/resources/swift/Models.mustache index 37c497ddcb9..587b710326b 100644 --- a/modules/swagger-codegen/src/main/resources/swift/Models.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/Models.mustache @@ -7,7 +7,7 @@ import Foundation protocol JSONEncodable { - func encode() -> AnyObject + func encodeToJSON() -> AnyObject } class Response { diff --git a/modules/swagger-codegen/src/main/resources/swift/api.mustache b/modules/swagger-codegen/src/main/resources/swift/api.mustache index e24bdae6773..8c40c3f5be7 100644 --- a/modules/swagger-codegen/src/main/resources/swift/api.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/api.mustache @@ -35,7 +35,7 @@ extension {{projectName}}API { path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}} let url = {{projectName}}API.basePath + path {{#bodyParam}} - let parameters = {{paramName}}{{^required}}?{{/required}}.encode() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}} + let parameters = {{paramName}}{{^required}}?{{/required}}.encodeToJSON() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}} let nillableParameters: [String:AnyObject?] = {{^queryParams}}[:]{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}} "{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}} ]{{/hasMore}}{{/queryParams}} diff --git a/modules/swagger-codegen/src/main/resources/swift/model.mustache b/modules/swagger-codegen/src/main/resources/swift/model.mustache index a964882c97c..06f10146cf6 100644 --- a/modules/swagger-codegen/src/main/resources/swift/model.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/model.mustache @@ -22,12 +22,12 @@ class {{classname}}: JSONEncodable { {{/vars}} // MARK: JSONEncodable - func encode() -> AnyObject { + func encodeToJSON() -> 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}} + nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encodeToJSON(){{/isPrimitiveType}}{{/isNotContainer}}{{#isContainer}} + nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encodeToJSON(){{/isContainer}}{{/vars}} let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:] return dictionary }