forked from loafle/openapi-generator-original
Remove dependency on SwiftyJSON
This commit is contained in:
parent
dbfc46658f
commit
e0c0d2a72a
@ -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<T>() -> RequestBuilder<T>.Type
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<T>(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>() -> [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()!
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,6 +31,7 @@ class Response<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private var once = dispatch_once_t()
|
||||
class Decoders {
|
||||
static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
|
||||
|
||||
@ -54,6 +55,7 @@ class Decoders {
|
||||
}
|
||||
|
||||
static func decode<T>(#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<T>(#clazz: T.Type, source: AnyObject?) -> T? {
|
||||
return source.map { (source: AnyObject) -> T in
|
||||
Decoders.decode(clazz: clazz, source: source)
|
||||
}
|
||||
}
|
||||
|
||||
static func decodeOptional<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
|
||||
return source.map { (someSource: AnyObject) -> [T] in
|
||||
Decoders.decode(clazz: clazz, source: someSource)
|
||||
}
|
||||
}
|
||||
|
||||
static func decodeOptional<T, Key: Hashable>(#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}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user