Merge pull request #1187 from Edubits/swift-2.0

[Swift] Update generated code to Swift 2.0
This commit is contained in:
wing328 2015-09-24 10:51:05 +08:00
commit ee7f447f77
6 changed files with 31 additions and 36 deletions

View File

@ -18,7 +18,7 @@ public class APIBase {
if encoded! is [AnyObject] {
var dictionary = [String:AnyObject]()
for (index, item) in enumerate(encoded as! [AnyObject]) {
for (index, item) in (encoded as! [AnyObject]).enumerate() {
dictionary["\(index)"] = item
}
return dictionary
@ -43,9 +43,9 @@ public class RequestBuilder<T> {
self.isBody = isBody
}
public func execute(completion: (response: Response<T>?, erorr: NSError?) -> Void) { }
public func execute(completion: (response: Response<T>?, erorr: ErrorType?) -> Void) { }
public func addHeader(#name: String, value: String) -> Self {
public func addHeader(name name: String, value: String) -> Self {
if !value.isEmpty {
headers[name] = value
}

View File

@ -20,7 +20,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
}
override func execute(completion: (response: Response<T>?, erorr: NSError?) -> Void) {
override func execute(completion: (response: Response<T>?, erorr: ErrorType?) -> Void) {
let managerId = NSUUID().UUIDString
// Create a new manager for each request to customize its request header
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
@ -30,7 +30,8 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
let xMethod = Alamofire.Method(rawValue: method)
let fileKeys = parameters == nil ? [] : map(filter(parameters!) { $1.isKindOfClass(NSURL) }) { $0.0 }
let fileKeys = parameters == nil ? [] : parameters!.filter { $1.isKindOfClass(NSURL) }
.map { $0.0 }
if fileKeys.count > 0 {
manager.upload(
@ -69,23 +70,16 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}
private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, erorr: NSError?) -> Void) {
private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, erorr: ErrorType?) -> Void) {
if let credential = self.credential {
request.authenticate(usingCredential: credential)
}
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
request.responseJSON(options: .AllowFragments) { (req, res, result) in
managerStore.removeValueForKey(managerId)
if let error = error {
completion(response: nil, erorr: error)
return
}
if res!.statusCode >= 400 {
//TODO: Add error entity
let userInfo: [NSObject : AnyObject] = (json != nil) ? ["data": json!] : [:]
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: userInfo)
completion(response: nil, erorr: error)
if result.isFailure {
completion(response: nil, erorr: result.error)
return
}
@ -94,7 +88,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
completion(response: response, erorr: nil)
return
}
if let json: AnyObject = json {
if let json: AnyObject = result.value {
let body = Decoders.decode(clazz: T.self, source: json)
let response = Response(response: res!, body: body)
completion(response: response, erorr: nil)

View File

@ -1,2 +1,2 @@
github "Alamofire/Alamofire" >= 1.3
github "mxcl/PromiseKit" >=1.5.3
github "Alamofire/Alamofire" >= 2.0.0{{#usePromiseKit}}
github "mxcl/PromiseKit" >=1.5.3{{/usePromiseKit}}

View File

@ -35,17 +35,17 @@ private var once = dispatch_once_t()
class Decoders {
static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
static func addDecoder<T>(#clazz: T.Type, decoder: ((AnyObject) -> T)) {
static func addDecoder<T>(clazz clazz: T.Type, decoder: ((AnyObject) -> T)) {
let key = "\(T.self)"
decoders[key] = { decoder($0) as! AnyObject }
}
static func decode<T>(#clazz: [T].Type, source: AnyObject) -> [T] {
static func decode<T>(clazz clazz: [T].Type, source: AnyObject) -> [T] {
let array = source as! [AnyObject]
return array.map { Decoders.decode(clazz: T.self, source: $0) }
}
static func decode<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
static func decode<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
let sourceDictinoary = source as! [Key: AnyObject]
var dictionary = [Key:T]()
for (key, value) in sourceDictinoary {
@ -54,7 +54,7 @@ class Decoders {
return dictionary
}
static func decode<T>(#clazz: T.Type, source: AnyObject) -> T {
static func decode<T>(clazz clazz: T.Type, source: AnyObject) -> T {
initialize()
if source is T {
return source as! T
@ -68,7 +68,7 @@ class Decoders {
}
}
static func decodeOptional<T>(#clazz: T.Type, source: AnyObject?) -> T? {
static func decodeOptional<T>(clazz clazz: T.Type, source: AnyObject?) -> T? {
if source is NSNull {
return nil
}
@ -77,7 +77,7 @@ class Decoders {
}
}
static func decodeOptional<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
static func decodeOptional<T>(clazz clazz: [T].Type, source: AnyObject?) -> [T]? {
if source is NSNull {
return nil
}
@ -86,7 +86,7 @@ class Decoders {
}
}
static func decodeOptional<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
static func decodeOptional<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
if source is NSNull {
return nil
}
@ -100,6 +100,7 @@ class Decoders {
let formatters = [
"yyyy-MM-dd",
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss'Z'"
].map { (format: String) -> NSDateFormatter in
let formatter = NSDateFormatter()
@ -120,7 +121,7 @@ class Decoders {
// Decoder for {{{classname}}}
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in
let sourceDictionary = source as! [NSObject:AnyObject]
var instance = {{classname}}(){{#vars}}{{#isEnum}}
let instance = {{classname}}(){{#vars}}{{#isEnum}}
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}.{{datatypeWithEnum}}(rawValue: $0)! }{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}} {{/isEnum}}{{^isEnum}}
instance.{{name}} = Decoders.decode{{^unwrapRequired}}Optional{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}Optional{{/required}}{{/unwrapRequired}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{name}}"]{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}}){{/isEnum}}{{/vars}}
return instance

View File

@ -14,7 +14,7 @@ Pod::Spec.new do |s|
s.description = '{{podDescription}}'{{/podDescription}}{{#podScreenshots}}
s.screenshots = {{& podScreenshots}}{{/podScreenshots}}{{#podDocumentationURL}}
s.documentation_url = '{{podDocumentationURL}}'{{/podDocumentationURL}}
s.source_files = '{{projectName}}/Classes/Swaggers/**/*.swift'
s.dependency 'PromiseKit', '~> 2.1'
s.dependency 'Alamofire', '~> 1.3'
s.source_files = '{{projectName}}/Classes/Swaggers/**/*.swift'{{#usePromiseKit}}
s.dependency 'PromiseKit', '~> 2.1'{{/usePromiseKit}}
s.dependency 'Alamofire', '~> 2.0.0'
end

View File

@ -5,8 +5,8 @@
// https://github.com/swagger-api/swagger-codegen
//
import Alamofire
import PromiseKit
import Alamofire{{#usePromiseKit}}
import PromiseKit{{/usePromiseKit}}
extension {{projectName}}API {
{{#description}}
@ -28,11 +28,11 @@ extension {{projectName}}API {
- examples: {{{examples}}}{{/examples}}{{#externalDocs}}
- externalDocs: {{externalDocs}}{{/externalDocs}}{{#hasParams}}
{{/hasParams}}{{#allParams}}
:param: {{paramName}} ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
:returns: Promise<Response<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>> {{description}}
- returns: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{description}}
*/
public class func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
public class func {{operationId}}({{#allParams}}{{^secondaryParam}}{{paramName}} {{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
let URLString = {{projectName}}API.basePath + path
@ -47,7 +47,7 @@ extension {{projectName}}API {
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder()
return requestBuilder(method: "{{httpMethod}}", URLString: URLString, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}})
return requestBuilder.init(method: "{{httpMethod}}", URLString: URLString, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}})
}
{{/operation}}
}