From 063e6d062dbc4336b972c5c6b90b0f8facc2b5a8 Mon Sep 17 00:00:00 2001 From: kubo_takaichi Date: Sun, 17 May 2015 22:42:32 +0900 Subject: [PATCH] Introduce decodable array and dictionary --- .../codegen/languages/SwiftGenerator.java | 6 ++- .../src/main/resources/swift/APIs.mustache | 1 + .../resources/swift/DecodableArray.mustache | 28 ++++++++++++++ .../swift/DecodableDictionary.mustache | 38 +++++++++++++++++++ .../src/main/resources/swift/api.mustache | 2 +- 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/swift/DecodableArray.mustache create mode 100644 modules/swagger-codegen/src/main/resources/swift/DecodableDictionary.mustache diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java index 53097c3573..cd27383e70 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/SwiftGenerator.java @@ -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( 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"; } return super.getTypeDeclaration(p); } diff --git a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache index 8e8764514a..22c1490423 100644 --- a/modules/swagger-codegen/src/main/resources/swift/APIs.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/APIs.mustache @@ -4,6 +4,7 @@ // https://github.com/swagger-api/swagger-codegen // +import Foundation import PromiseKit class {{projectName}}API { diff --git a/modules/swagger-codegen/src/main/resources/swift/DecodableArray.mustache b/modules/swagger-codegen/src/main/resources/swift/DecodableArray.mustache new file mode 100644 index 0000000000..6af73c7401 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/swift/DecodableArray.mustache @@ -0,0 +1,28 @@ +// DecodableArray.swift +// +// Generated by swagger-codegen +// https://github.com/swagger-api/swagger-codegen +// + + +final class DecodableArray: 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() + } +} diff --git a/modules/swagger-codegen/src/main/resources/swift/DecodableDictionary.mustache b/modules/swagger-codegen/src/main/resources/swift/DecodableDictionary.mustache new file mode 100644 index 0000000000..4463b60eec --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/swift/DecodableDictionary.mustache @@ -0,0 +1,38 @@ +// DecodableDictionary.swift +// +// Generated by swagger-codegen +// https://github.com/swagger-api/swagger-codegen +// + +import Foundation + +final class DecodableDictionary: 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() + } +} diff --git a/modules/swagger-codegen/src/main/resources/swift/api.mustache b/modules/swagger-codegen/src/main/resources/swift/api.mustache index 28f14853fc..e4961e2e83 100644 --- a/modules/swagger-codegen/src/main/resources/swift/api.mustache +++ b/modules/swagger-codegen/src/main/resources/swift/api.mustache @@ -30,7 +30,7 @@ extension {{projectName}}API { :returns: Promise> {{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