Permalink
Browse files

[Swift4]Update templates for swift 4 (#6921)

* update versions of dependencies on swift4 and swift3

* change syntax for swift4

* run petstore script

* change enum case from UpperCamel to lowerCamel

* remove unneeded break statements

* avoid wrapping conditionals in parentheses

* avoid force casting

* run pod update on petstore/swift4/rxswift

* update project for swift 4

* run swift4-petstore-all.sh

* fix compile error

* avoid use iso8601 date strategy for encoder / decoder

* resolve file references
  • Loading branch information...
d-date authored and wing328 committed Nov 21, 2017
1 parent b06ccec commit a5e4abe5ec7f89025c4e846a455f34a8980bb48b
Showing 379 changed files with 5,117 additions and 8,072 deletions.
@@ -0,0 +1 @@
oracle64-1.8.0.152
@@ -88,7 +88,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}
self.processRequest(uploadRequest, managerId, completion)
case .Failure(let encodingError):
completion(response: nil, error: ErrorResponse.Error(415, nil, encodingError))
completion(response: nil, error: ErrorResponse.error(415, nil, encodingError))
}
}
)
@@ -121,7 +121,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
if stringResponse.result.isFailure {
completion(
response: nil,
error: ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
error: ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error!)
)
return
}
@@ -141,7 +141,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
if voidResponse.result.isFailure {
completion(
response: nil,
error: ErrorResponse.Error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
error: ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
)
return
}
@@ -158,10 +158,10 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
validatedRequest.responseData(completionHandler: { (dataResponse) in
cleanupRequest()
if (dataResponse.result.isFailure) {
if dataResponse.result.isFailure {
completion(
response: nil,
error: ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
error: ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
)
return
}
@@ -179,7 +179,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
cleanupRequest()
if response.result.isFailure {
completion(response: nil, error: ErrorResponse.Error(response.response?.statusCode ?? 500, response.data, response.result.error!))
completion(response: nil, error: ErrorResponse.error(response.response?.statusCode ?? 500, response.data, response.result.error!))
return
}
@@ -198,7 +198,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
return
}
completion(response: nil, error: ErrorResponse.Error(500, nil, NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"])))
completion(response: nil, error: ErrorResponse.error(500, nil, NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"])))
}
}
}
@@ -28,8 +28,8 @@ public class Response<T> {
public convenience init(response: NSHTTPURLResponse, body: T?) {
let rawHeader = response.allHeaderFields
var header = [String:String]()
for (key, value) in rawHeader {
header[key as! String] = value as? String
for case let (key, value) as (String, String) in rawHeader {
header[key] = value
}
self.init(statusCode: response.statusCode, header: header, body: body)
}
@@ -118,16 +118,12 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
else {
mpForm.append(fileURL, withName: k)
}
break
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
break
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
break
default:
fatalError("Unprocessable value \(v) with key \(k)")
break
}
}
}, to: URLString, method: xMethod!, headers: nil, encodingCompletion: { encodingResult in
@@ -206,7 +202,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
validatedRequest.responseData(completionHandler: { (dataResponse) in
cleanupRequest()
if (dataResponse.result.isFailure) {
if dataResponse.result.isFailure {
completion(
nil,
ErrorResponse.HttpError(statusCode: dataResponse.response?.statusCode ?? 500, data: dataResponse.data, error: dataResponse.result.error!)
@@ -1,3 +1,3 @@
github "Alamofire/Alamofire" >= 3.1.0{{#usePromiseKit}}
github "mxcl/PromiseKit" >=1.5.3{{/usePromiseKit}}{{#useRxSwift}}
github "ReactiveX/RxSwift" ~> 2.0{{/useRxSwift}}
github "Alamofire/Alamofire" ~> 4.5{{#usePromiseKit}}
github "mxcl/PromiseKit" ~> 4.4{{/usePromiseKit}}{{#useRxSwift}}
github "ReactiveX/RxSwift" "rxswift-3.0"{{/useRxSwift}}
@@ -54,7 +54,7 @@ extension Dictionary: JSONEncodable {
func encodeToJSON() -> Any {
var dictionary = [AnyHashable: Any]()
for (key, value) in self {
dictionary[key as! NSObject] = encodeIfPossible(value)
dictionary[key] = encodeIfPossible(value)
}
return dictionary as Any
}
@@ -29,8 +29,8 @@ open class Response<T> {
public convenience init(response: HTTPURLResponse, body: T?) {
let rawHeader = response.allHeaderFields
var header = [String:String]()
for (key, value) in rawHeader {
header[key as! String] = value as? String
for case let (key, value) as (String, String) in rawHeader {
header[key] = value
}
self.init(statusCode: response.statusCode, header: header, body: body)
}
@@ -16,6 +16,6 @@ Pod::Spec.new do |s|
s.documentation_url = '{{podDocumentationURL}}'{{/podDocumentationURL}}
s.source_files = '{{projectName}}/Classes/**/*.swift'{{#usePromiseKit}}
s.dependency 'PromiseKit/CorePromise', '~> 4.4.0'{{/usePromiseKit}}{{#useRxSwift}}
s.dependency 'RxSwift', '~> 3.4.1'{{/useRxSwift}}
s.dependency 'RxSwift', '3.6.1'{{/useRxSwift}}
s.dependency 'Alamofire', '~> 4.5.0'
end
@@ -77,16 +77,12 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
else {
mpForm.append(fileURL, withName: k)
}
break
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
break
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
break
default:
fatalError("Unprocessable value \(v) with key \(k)")
break
}
}
}, to: URLString, method: xMethod!, headers: nil, encodingCompletion: { encodingResult in
@@ -97,7 +93,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}
self.processRequest(request: upload, managerId, completion)
case .failure(let encodingError):
completion(nil, ErrorResponse.Error(415, nil, encodingError))
completion(nil, ErrorResponse.error(415, nil, encodingError))
}
})
} else {
@@ -129,7 +125,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
if stringResponse.result.isFailure {
completion(
nil,
ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
)
return
}
@@ -186,9 +182,9 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
)
} catch let requestParserError as DownloadException {
completion(nil, ErrorResponse.Error(400, dataResponse.data, requestParserError))
completion(nil, ErrorResponse.error(400, dataResponse.data, requestParserError))
} catch let error {
completion(nil, ErrorResponse.Error(400, dataResponse.data, error))
completion(nil, ErrorResponse.error(400, dataResponse.data, error))
}
return
})
@@ -199,7 +195,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
if voidResponse.result.isFailure {
completion(
nil,
ErrorResponse.Error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
)
return
}
@@ -215,10 +211,10 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
validatedRequest.responseData(completionHandler: { (dataResponse) in
cleanupRequest()
if (dataResponse.result.isFailure) {
if dataResponse.result.isFailure {
completion(
nil,
ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
)
return
}
@@ -331,7 +327,7 @@ open class AlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilde
if stringResponse.result.isFailure {
completion(
nil,
ErrorResponse.Error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.result.error as Error!)
)
return
}
@@ -351,7 +347,7 @@ open class AlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilde
if voidResponse.result.isFailure {
completion(
nil,
ErrorResponse.Error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.result.error!)
)
return
}
@@ -367,10 +363,10 @@ open class AlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilde
validatedRequest.responseData(completionHandler: { (dataResponse) in
cleanupRequest()
if (dataResponse.result.isFailure) {
if dataResponse.result.isFailure {
completion(
nil,
ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!)
)
return
}
@@ -388,17 +384,17 @@ open class AlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilde
cleanupRequest()
guard dataResponse.result.isSuccess else {
completion(nil, ErrorResponse.Error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!))
completion(nil, ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.result.error!))
return
}
guard let data = dataResponse.data, !data.isEmpty else {
completion(nil, ErrorResponse.Error(-1, nil, AlamofireDecodableRequestBuilderError.emptyDataResponse))
completion(nil, ErrorResponse.error(-1, nil, AlamofireDecodableRequestBuilderError.emptyDataResponse))
return
}
guard let httpResponse = dataResponse.response else {
completion(nil, ErrorResponse.Error(-2, nil, AlamofireDecodableRequestBuilderError.nilHTTPResponse))
completion(nil, ErrorResponse.error(-2, nil, AlamofireDecodableRequestBuilderError.nilHTTPResponse))
return
}
@@ -1,3 +1,3 @@
github "Alamofire/Alamofire" >= 3.1.0{{#usePromiseKit}}
github "mxcl/PromiseKit" >=1.5.3{{/usePromiseKit}}{{#useRxSwift}}
github "ReactiveX/RxSwift" ~> 2.0{{/useRxSwift}}
github "Alamofire/Alamofire" ~> 4.5.0{{#usePromiseKit}}
github "mxcl/PromiseKit" ~> 4.4{{/usePromiseKit}}{{#useRxSwift}}
github "ReactiveX/RxSwift" ~> 4.0{{/useRxSwift}}
@@ -22,9 +22,12 @@ open class CodableHelper {
decoder.dateDecodingStrategy = .formatted(df)
} else {
decoder.dataDecodingStrategy = .base64
if #available(iOS 10.0, *) {
decoder.dateDecodingStrategy = .iso8601
}
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
decoder.dateDecodingStrategy = .formatted(formatter)
}
do {
@@ -45,9 +48,12 @@ open class CodableHelper {
encoder.outputFormatting = .prettyPrinted
}
encoder.dataEncodingStrategy = .base64
if #available(iOS 10.0, *) {
encoder.dateEncodingStrategy = .iso8601
}
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
encoder.dateEncodingStrategy = .formatted(formatter)
do {
returnedData = try encoder.encode(value)
@@ -54,7 +54,7 @@ extension Dictionary: JSONEncodable {
func encodeToJSON() -> Any {
var dictionary = [AnyHashable: Any]()
for (key, value) in self {
dictionary[key as! NSObject] = encodeIfPossible(value)
dictionary[key] = encodeIfPossible(value)
}
return dictionary as Any
}
@@ -11,7 +11,7 @@ protocol JSONEncodable {
}
public enum ErrorResponse : Error {
case Error(Int, Data?, Error)
case error(Int, Data?, Error)
}
open class Response<T> {
@@ -28,8 +28,8 @@ open class Response<T> {
public convenience init(response: HTTPURLResponse, body: T?) {
let rawHeader = response.allHeaderFields
var header = [String:String]()
for (key, value) in rawHeader {
header[key as! String] = value as? String
for case let (key, value) as (String, String) in rawHeader {
header[key] = value
}
self.init(statusCode: response.statusCode, header: header, body: body)
}
@@ -16,6 +16,6 @@ Pod::Spec.new do |s|
s.documentation_url = '{{podDocumentationURL}}'{{/podDocumentationURL}}
s.source_files = '{{projectName}}/Classes/**/*.swift'{{#usePromiseKit}}
s.dependency 'PromiseKit/CorePromise', '~> 4.4.0'{{/usePromiseKit}}{{#useRxSwift}}
s.dependency 'RxSwift', '~> 3.4.1'{{/useRxSwift}}
s.dependency 'RxSwift', '~> 4.0'{{/useRxSwift}}
s.dependency 'Alamofire', '~> 4.5.0'
end
@@ -74,9 +74,9 @@ open class {{classname}} {
return Observable.create { observer -> Disposable in
{{operationId}}({{#allParams}}{{paramName}}: {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { {{#returnType}}data, {{/returnType}}error in
if let error = error {
observer.on(.error(error as Error))
observer.on(.error(error))
} else {
observer.on(.next({{#returnType}}data!{{/returnType}}))
observer.on(.next({{#returnType}}data!{{/returnType}}{{^returnType}}(){{/returnType}}))
}
observer.on(.completed)
}
@@ -64,13 +64,13 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}Codable{{
}
{{/additionalPropertiesType}}
{{#hasVars}}
{{^parent}}{{#hasVars}}
public init({{#vars}}{{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/vars}}) {
{{#vars}}
self.{{name}} = {{name}}
{{/vars}}
}
{{/hasVars}}
{{/hasVars}}{{/parent}}
// Encodable protocol methods
@@ -79,7 +79,7 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}Codable{{
var container = encoder.container(keyedBy: String.self)
{{#vars}}
try container.encode{{#isListContainer}}Array{{/isListContainer}}{{^required}}IfPresent{{/required}}({{{name}}}, forKey: "{{{baseName}}}")
try container.encode{{^required}}IfPresent{{/required}}({{{name}}}, forKey: "{{{baseName}}}")
{{/vars}}
{{#additionalPropertiesType}}
try container.encodeMap(additionalProperties)
@@ -88,11 +88,11 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}Codable{{
// Decodable protocol methods
public {{#parent}}override {{/parent}}required init(from decoder: Decoder) throws {
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: String.self)
{{#vars}}
{{name}} = try container.decode{{#isListContainer}}Array{{/isListContainer}}{{^required}}IfPresent{{/required}}({{#isListContainer}}{{{items.datatype}}}{{/isListContainer}}{{^isListContainer}}{{{datatypeWithEnum}}}{{/isListContainer}}.self, forKey: "{{{baseName}}}")
{{name}} = try container.decode{{^required}}IfPresent{{/required}}({{{datatypeWithEnum}}}.self, forKey: "{{{baseName}}}")
{{/vars}}
{{#additionalPropertiesType}}
var nonAdditionalPropertyKeys = Set<String>()
@@ -101,6 +101,9 @@ open class {{classname}}: {{#parent}}{{{parent}}}{{/parent}}{{^parent}}Codable{{
{{/vars}}
additionalProperties = try container.decodeMap({{{additionalPropertiesType}}}.self, excludedKeys: nonAdditionalPropertyKeys)
{{/additionalPropertiesType}}
{{#parent}}
try super.init(from: decoder)
{{/parent}}
}
}
Oops, something went wrong.

0 comments on commit a5e4abe

Please sign in to comment.