Introduce decodable array and dictionary

This commit is contained in:
kubo_takaichi 2015-05-17 22:42:32 +09:00
parent 0b7d758077
commit 063e6d062d
5 changed files with 72 additions and 3 deletions

View File

@ -63,6 +63,8 @@ public class SwiftGenerator extends DefaultCodegen implements CodegenConfig {
supportingFiles.add(new SupportingFile("Extensions.mustache", sourceFolder, "Extensions.swift"));
supportingFiles.add(new SupportingFile("Models.mustache", sourceFolder, "Models.swift"));
supportingFiles.add(new SupportingFile("APIs.mustache", sourceFolder, "APIs.swift"));
supportingFiles.add(new SupportingFile("DecodableDictionary.mustache", sourceFolder, "DecodableDictionary.swift"));
supportingFiles.add(new SupportingFile("DecodableArray.mustache", sourceFolder, "DecodableArray.swift"));
languageSpecificPrimitives = new HashSet<String>(
Arrays.asList(
@ -138,11 +140,11 @@ public class SwiftGenerator extends DefaultCodegen implements CodegenConfig {
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return "[" + getTypeDeclaration(inner) + "]";
return "DecodableArray<" + getTypeDeclaration(inner) + ">";
} else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
return "[String:" + getTypeDeclaration(inner) + "]";
return "DecodableDictionary<String, " + getTypeDeclaration(inner) + ">";
}
return super.getTypeDeclaration(p);
}

View File

@ -4,6 +4,7 @@
// https://github.com/swagger-api/swagger-codegen
//
import Foundation
import PromiseKit
class {{projectName}}API {

View File

@ -0,0 +1,28 @@
// DecodableArray.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
final class DecodableArray<T: JSONDecodable>: JSONEncodable, JSONDecodable, ArrayLiteralConvertible {
let value: [T]
required init(arrayLiteral elements: T...) {
self.value = elements
}
init(array elements: [T]) {
self.value = elements
}
static func decode(source: AnyObject) -> DecodableArray? {
let array = source as! [AnyObject]
let decodedArray = DecodableArray(array: array.map({ T.decode($0)! }))
return decodedArray
}
func encode() -> AnyObject {
return value.encode()
}
}

View File

@ -0,0 +1,38 @@
// DecodableDictionary.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import Foundation
final class DecodableDictionary<Key, Value where Key: Hashable, Key: JSONDecodable, Value: JSONDecodable>: JSONEncodable, JSONDecodable, DictionaryLiteralConvertible {
let value: [Key:Value]
init(dictionaryLiteral elements: (Key, Value)...) {
var dic = [Key:Value]()
for (key, value) in elements {
dic[key] = value
}
self.value = dic
}
init(dictionary elements: [Key:Value]) {
self.value = elements
}
static func decode(source: AnyObject) -> DecodableDictionary? {
var destination = [Key:Value]()
let dictionary = source as! [NSObject:AnyObject]
for (key, value) in dictionary {
let decodedKey = Key.decode(key)!
let decodedValue = Value.decode(value)!
destination[decodedKey] = decodedValue
}
return DecodableDictionary(dictionary: destination)
}
func encode() -> AnyObject {
return value.encode()
}
}

View File

@ -30,7 +30,7 @@ extension {{projectName}}API {
:returns: Promise<Response<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Empty{{/returnType}}>> {{description}}
*/
func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{dataType}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Empty{{/returnType}}> {
func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Empty{{/returnType}}> {
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
let url = {{projectName}}API.basePath + path