Skip to content

Commit

Permalink
Merge pull request #554 from swagger-api/swift5-generator
Browse files Browse the repository at this point in the history
Swift5 generator
  • Loading branch information
HugoMario authored Dec 8, 2019
2 parents d0eb3b1 + b78378a commit 8e72029
Show file tree
Hide file tree
Showing 24 changed files with 2,413 additions and 0 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ io.swagger.codegen.v3.generators.scala.ScalaClientCodegen
io.swagger.codegen.v3.generators.scala.AkkaHttpServerCodegen
io.swagger.codegen.v3.generators.swift.Swift3Codegen
io.swagger.codegen.v3.generators.swift.Swift4Codegen
io.swagger.codegen.v3.generators.swift.Swift5Codegen
io.swagger.codegen.v3.generators.typescript.TypeScriptAngularClientCodegen
io.swagger.codegen.v3.generators.javascript.JavaScriptClientCodegen
65 changes: 65 additions & 0 deletions src/main/resources/handlebars/swift5/APIHelper.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// APIHelper.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation

public struct APIHelper {
public static func rejectNil(_ source: [String:Any?]) -> [String:Any]? {
let destination = source.reduce(into: [String: Any]()) { (result, item) in
if let value = item.value {
result[item.key] = value
}
}

if destination.isEmpty {
return nil
}
return destination
}

public static func rejectNilHeaders(_ source: [String:Any?]) -> [String:String] {
return source.reduce(into: [String: String]()) { (result, item) in
if let collection = item.value as? Array<Any?> {
result[item.key] = collection.filter({ $0 != nil }).map{ "\($0!)" }.joined(separator: ",")
} else if let value: Any = item.value {
result[item.key] = "\(value)"
}
}
}

public static func convertBoolToString(_ source: [String: Any]?) -> [String:Any]? {
guard let source = source else {
return nil
}

return source.reduce(into: [String: Any](), { (result, item) in
switch item.value {
case let x as Bool:
result[item.key] = x.description
default:
result[item.key] = item.value
}
})
}


public static func mapValuesToQueryItems(_ source: [String:Any?]) -> [URLQueryItem]? {
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
if let collection = item.value as? Array<Any?> {
let value = collection.filter({ $0 != nil }).map({"\($0!)"}).joined(separator: ",")
result.append(URLQueryItem(name: item.key, value: value))
} else if let value = item.value {
result.append(URLQueryItem(name: item.key, value: "\(value)"))
}
}

if destination.isEmpty {
return nil
}
return destination
}
}

61 changes: 61 additions & 0 deletions src/main/resources/handlebars/swift5/APIs.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// APIs.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation

open class {{projectName}}API {
public static var basePath = "{{{basePath}}}"
public static var credential: URLCredential?
public static var customHeaders: [String:String] = [:]
public static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
}

open class RequestBuilder<T> {
var credential: URLCredential?
var headers: [String:String]
public let parameters: [String:Any]?
public let isBody: Bool
public let method: String
public let URLString: String
/// Optional block to obtain a reference to the request's progress instance when available.
public var onProgressReady: ((Progress) -> ())?
required public init(method: String, URLString: String, parameters: [String:Any]?, isBody: Bool, headers: [String:String] = [:]) {
self.method = method
self.URLString = URLString
self.parameters = parameters
self.isBody = isBody
self.headers = headers
addHeaders({{projectName}}API.customHeaders)
}
open func addHeaders(_ aHeaders:[String:String]) {
for (header, value) in aHeaders {
headers[header] = value
}
}
open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) { }
public func addHeader(name: String, value: String) -> Self {
if !value.isEmpty {
headers[name] = value
}
return self
}
open func addCredential() -> Self {
self.credential = {{projectName}}API.credential
return self
}
}
public protocol RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
func getBuilder<T:Decodable>() -> RequestBuilder<T>.Type
}
Loading

0 comments on commit 8e72029

Please sign in to comment.