Add swift code generator

This commit is contained in:
kubo_takaichi
2015-05-13 22:51:14 +09:00
parent f627c81399
commit 23d714f2f1
12 changed files with 684 additions and 0 deletions

31
bin/swift-petstore.sh Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/sh
SCRIPT="$0"
while [ -h "$SCRIPT" ] ; do
ls=`ls -ld "$SCRIPT"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
SCRIPT="$link"
else
SCRIPT=`dirname "$SCRIPT"`/"$link"
fi
done
if [ ! -d "${APP_DIR}" ]; then
APP_DIR=`dirname "$SCRIPT"`/..
APP_DIR=`cd "${APP_DIR}"; pwd`
fi
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
if [ ! -f "$executable" ]
then
mvn clean package
fi
# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/swift -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l swift -o samples/client/petstore/swift"
java -DappName=PetstoreClient $JAVA_OPTS -jar $executable $ags

View File

@@ -0,0 +1,249 @@
package com.wordnik.swagger.codegen.languages;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.models.Model;
import com.wordnik.swagger.models.Operation;
import com.wordnik.swagger.models.parameters.HeaderParameter;
import com.wordnik.swagger.models.parameters.Parameter;
import com.wordnik.swagger.models.properties.*;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Nullable;
import java.util.*;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SwiftGenerator extends DefaultCodegen implements CodegenConfig {
private static final Pattern PATH_PARAM_PATTERN = Pattern.compile("\\{[a-zA-Z_]+\\}");
protected String sourceFolder = "Classes/Swaggers";
public CodegenType getTag() {
return CodegenType.CLIENT;
}
public String getName() {
return "swift";
}
public String getHelp() {
return "Generates a swift client library.";
}
public SwiftGenerator() {
super();
outputFolder = "generated-code/swift";
modelTemplateFiles.put("model.mustache", ".swift");
apiTemplateFiles.put("api.mustache", ".swift");
templateDir = "swift";
apiPackage = "/APIs";
modelPackage = "/Models";
// Inject application name
String appName = System.getProperty("appName");
if (appName == null) {
appName = "SwaggerClient";
}
additionalProperties.put("projectName", appName);
// Inject base url override
String basePathOverride = System.getProperty("basePathOverride");
if (basePathOverride != null) {
additionalProperties.put("basePathOverride", basePathOverride);
}
sourceFolder = appName + "/" + sourceFolder;
supportingFiles.add(new SupportingFile("Cartfile.mustache", appName, "Cartfile"));
supportingFiles.add(new SupportingFile("APIHelper.mustache", sourceFolder, "APIHelper.swift"));
supportingFiles.add(new SupportingFile("AlamofireImplementations.mustache", sourceFolder, "AlamofireImplementations.swift"));
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"));
languageSpecificPrimitives = new HashSet<String>(
Arrays.asList(
"Int",
"Float",
"Double",
"Bool",
"Void",
"String",
"Character")
);
defaultIncludes = new HashSet<String>(
Arrays.asList(
"NSDate",
"Array",
"Dictionary",
"Set",
"Any",
"Empty",
"AnyObject")
);
reservedWords = new HashSet<String>(
Arrays.asList(
"class", "break", "as", "associativity", "deinit", "case", "dynamicType", "convenience", "enum", "continue",
"false", "dynamic", "extension", "default", "is", "didSet", "func", "do", "nil", "final", "import", "else",
"self", "get", "init", "fallthrough", "Self", "infix", "internal", "for", "super", "inout", "let", "if",
"true", "lazy", "operator", "in", "COLUMN", "left", "private", "return", "FILE", "mutating", "protocol",
"switch", "FUNCTION", "none", "public", "where", "LINE", "nonmutating", "static", "while", "optional",
"struct", "override", "subscript", "postfix", "typealias", "precedence", "var", "prefix", "Protocol",
"required", "right", "set", "Type", "unowned", "weak")
);
typeMapping = new HashMap<String, String>();
typeMapping.put("array", "Array");
typeMapping.put("List", "Array");
typeMapping.put("map", "Dictionary");
typeMapping.put("Date", "NSDate");
typeMapping.put("DateTime", "NSDate");
typeMapping.put("boolean", "Bool");
typeMapping.put("string", "String");
typeMapping.put("char", "Character");
typeMapping.put("short", "Int");
typeMapping.put("int", "Int");
typeMapping.put("long", "Int");
typeMapping.put("integer", "Int");
typeMapping.put("Integer", "Int");
typeMapping.put("float", "Float");
typeMapping.put("number", "Double");
typeMapping.put("double", "Double");
typeMapping.put("object", "AnyObject");
typeMapping.put("file", "NSData");
importMapping = new HashMap<String, String>();
}
@Override
public String escapeReservedWord(String name) {
return "_" + name; // add an underscore to the name
}
@Override
public String modelFileFolder() {
return outputFolder + "/" + sourceFolder + modelPackage().replace('.', File.separatorChar);
}
@Override
public String apiFileFolder() {
return outputFolder + "/" + sourceFolder + apiPackage().replace('.', File.separatorChar);
}
@Override
public String getTypeDeclaration(Property p) {
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return "[" + getTypeDeclaration(inner) + "]";
} else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
return "[String:" + getTypeDeclaration(inner) + "]";
}
return super.getTypeDeclaration(p);
}
@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
String type = null;
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
if (languageSpecificPrimitives.contains(type))
return toModelName(type);
} else
type = swaggerType;
return toModelName(type);
}
@Override
public String toDefaultValue(Property p) {
// nil
return null;
}
@Override
public String toInstantiationType(Property p) {
if (p instanceof MapProperty) {
MapProperty ap = (MapProperty) p;
String inner = getSwaggerType(ap.getAdditionalProperties());
return "[String:" + inner + "]";
} else if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
String inner = getSwaggerType(ap.getItems());
return "[" + inner + "]";
}
return null;
}
@Override
public CodegenProperty fromProperty(String name, Property p) {
CodegenProperty codegenProperty = super.fromProperty(name, p);
if (codegenProperty.isEnum) {
List<Map<String, String>> swiftEnums = new ArrayList<Map<String, String>>();
List<String> values = (List<String>) codegenProperty.allowableValues.get("values");
for (String value : values) {
Map<String, String> map = new HashMap<String, String>();
map.put("enum", StringUtils.capitalize(value));
map.put("raw", value);
swiftEnums.add(map);
}
codegenProperty.allowableValues.put("values", swiftEnums);
codegenProperty.datatypeWithEnum =
StringUtils.left(codegenProperty.datatypeWithEnum, codegenProperty.datatypeWithEnum.length() - "Enum".length());
}
return codegenProperty;
}
@Override
public String toApiName(String name) {
if(name.length() == 0)
return "DefaultAPI";
return initialCaps(name) + "API";
}
@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
path = normalizePath(path);
List<Parameter> parameters = operation.getParameters();
parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() {
@Override
public boolean apply(@Nullable Parameter parameter) {
return !(parameter instanceof HeaderParameter);
}
}));
operation.setParameters(parameters);
return super.fromOperation(path, httpMethod, operation, definitions);
}
private static String normalizePath(String path) {
StringBuilder builder = new StringBuilder();
int cursor = 0;
Matcher matcher = PATH_PARAM_PATTERN.matcher(path);
boolean found = matcher.find();
while (found) {
String stringBeforeMatch = path.substring(cursor, matcher.start());
builder.append(stringBeforeMatch);
String group = matcher.group().substring(1, matcher.group().length() - 1);
group = camelize(group, true);
builder
.append("{")
.append(group)
.append("}");
cursor = matcher.end();
found = matcher.find();
}
String stringAfterMatch = path.substring(cursor);
builder.append(stringAfterMatch);
return builder.toString();
}
}

View File

@@ -18,4 +18,5 @@ com.wordnik.swagger.codegen.languages.StaticDocCodegen
com.wordnik.swagger.codegen.languages.StaticHtmlGenerator
com.wordnik.swagger.codegen.languages.SwaggerGenerator
com.wordnik.swagger.codegen.languages.SwaggerYamlGenerator
com.wordnik.swagger.codegen.languages.SwiftGenerator
com.wordnik.swagger.codegen.languages.TizenClientCodegen

View File

@@ -0,0 +1,21 @@
// APIHelper.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
class APIHelper {
static func rejectNil(source: [String:AnyObject?]) -> [String:AnyObject]? {
var destination = [String:AnyObject]()
for (key, nillableValue) in source {
if let value: AnyObject = nillableValue {
destination[key] = value
}
}
if destination.isEmpty {
return nil
}
return destination
}
}

View File

@@ -0,0 +1,51 @@
// APIs.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import PromiseKit
class {{projectName}}API {
static let basePath = "{{^basePathOverride}}{{basePath}}{{/basePathOverride}}{{basePathOverride}}"
static var credential: NSURLCredential?
static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
class APIBase {
}
}
class RequestBuilder<T: JSONDecodable> {
var credential: NSURLCredential?
var headers: [String:String] = [:]
let parameters: [String:AnyObject]?
let isBody: Bool
let method: String
let URLString: URLStringConvertible
required init(method: String, URLString: URLStringConvertible, parameters: [String:AnyObject]?, isBody: Bool) {
self.method = method
self.URLString = URLString
self.parameters = parameters
self.isBody = isBody
}
func execute() -> Promise<Response<T>> { fatalError("Not implemented") }
func addHeader(#name: String, value: String) -> Self {
if !value.isEmpty {
headers[name] = value
}
return self
}
func addCredential() -> Self {
self.credential = OneteamAPI.credential
return self
}
}
protocol RequestBuilderFactory {
func getBuilder<T: JSONDecodable>() -> RequestBuilder<T>.Type
}

View File

@@ -0,0 +1,73 @@
// AlamofireImplementations.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import Alamofire
import PromiseKit
class AlamofireRequestBuilderFactory: RequestBuilderFactory {
func getBuilder<T: JSONDecodable>() -> RequestBuilder<T>.Type {
return AlamofireRequestBuilder<T>.self
}
}
class AlamofireRequestBuilder<T: JSONDecodable>: RequestBuilder<T> {
required init(method: String, URLString: URLStringConvertible, parameters: [String : AnyObject]?, isBody: Bool) {
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
}
override func execute() -> Promise<Response<T>> {
let configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = buildHeaders()
let manager = Alamofire.Manager(configuration: configuration)
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
let request = manager.request(Alamofire.Method(rawValue: method)!, URLString, parameters: parameters, encoding: encoding)
if let credential = self.credential {
request.authenticate(usingCredential: credential)
}
let defer = Promise<Response<T>>.defer()
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
if let error = error {
defer.reject(error)
return
}
if res!.statusCode >= 400 {
//TODO: Add error entity
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: [:])
defer.reject(error)
return
}
if Empty.instance is T {
let response = Response(response: res!, body: Empty.instance as! T)
defer.fulfill(response)
return
}
if let json: AnyObject = json {
if let body = T.decode(json) {
let response = Response(response: res!, body: body)
defer.fulfill(response)
} else {
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "cannot convertible: \(json)"]))
}
return
}
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"]))
}
return defer.promise
}
private func buildHeaders() -> [String: AnyObject] {
var httpHeaders = Manager.defaultHTTPHeaders
for (key, value) in self.headers {
httpHeaders[key] = value
}
return httpHeaders
}
}

View File

@@ -0,0 +1,2 @@
github "Alamofire/Alamofire" >= 1.2
github "mxcl/PromiseKit"

View File

@@ -0,0 +1,111 @@
// Extensions.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import Alamofire
import PromiseKit
import SwiftyJSON
extension Request {
func responsePromise<T>() -> Promise<Response<T>> {
let defer = Promise<Response<T>>.defer()
responseJSON(options: .AllowFragments) { (req, res, json, error) in
if let error = error {
defer.reject(error)
return
}
if res!.statusCode >= 400 {
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: [:])
defer.reject(error)
return
}
if Empty.instance is T {
let response = Response(response: res!, body: Empty.instance as! T)
defer.fulfill(response)
return
}
if let json: AnyObject = json {
if let body = T.decode(json) {
let response = Response(response: res!, body: body)
defer.fulfill(response)
} else {
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "cannot convertible: \(json)"]))
}
return
}
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"]))
}
return defer.promise
}
}
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) }
}
}
}
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: JSONDecodable>() -> T? {
return T.decode(self.object)
}
func decode<T: JSONDecodable>() -> [T]? {
return self.arrayObject?.map(T.decode).filter({ $0 != nil }).map({ $0! })
}
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()!
}
}

View File

@@ -0,0 +1,47 @@
// Models.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import Foundation
import SwiftyJSON
protocol JSONDecodable {
static func decode(source: AnyObject) -> Self?
}
protocol JSONEncodable {
func encode() -> AnyObject
}
class Response<T: JSONDecodable> {
let statusCode: Int
let header: [String: String]
let body: T
init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
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
}
self.init(statusCode: response.statusCode, header: header, body: body)
}
}
final class Empty: JSONDecodable {
private init() { }
static let instance = Empty()
static func decode(source: AnyObject) -> Empty? {
return instance
}
}

View File

@@ -0,0 +1,50 @@
{{#operations}}//
// {{classname}}.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import Alamofire
import PromiseKit
extension {{projectName}}API {
{{#description}}
/** {{description}} */{{/description}}
class {{classname}}: APIBase {
{{#operation}}
/**
{{#summary}}
{{{summary}}}
{{/summary}}
- {{httpMethod}} {{path}}{{#notes}}
- {{{notes}}}{{/notes}}{{#subresourceOperation}}
- subresourceOperation: {{subresourceOperation}}{{/subresourceOperation}}{{#defaultResponse}}
- defaultResponse: {{defaultResponse}}{{/defaultResponse}}{{#authMethods}}
- authMethods: {{authMethods}}{{/authMethods}}{{#responseHeaders}}
- responseHeaders: {{responseHeaders}}{{/responseHeaders}}{{#examples}}
- examples: {{{examples}}}{{/examples}}{{#externalDocs}}
- externalDocs: {{externalDocs}}{{/externalDocs}}{{#hasParams}}
{{/hasParams}}{{#allParams}}
:param: {{paramName}} ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
: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}}> {
{{^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
{{#bodyParam}}
let parameters = {{paramName}}.encode() as! [String:AnyObject]{{/bodyParam}}{{^bodyParam}}
let nillableParameters: [String:AnyObject?] = {{^queryParams}}[:]{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}}
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
]{{/hasMore}}{{/queryParams}}
let parameters = APIHelper.rejectNil(nillableParameters){{/bodyParam}}
return {{projectName}}API.requestBuilderFactory.getBuilder()
(method: "{{httpMethod}}", URLString: url, parameters: parameters, isBody: {{^queryParams}}true{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}})
}
{{/operation}}
}
}
{{/operations}}

View File

@@ -0,0 +1,46 @@
{{#models}}{{#model}}//
// {{classname}}.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import SwiftyJSON
extension {{projectName}}API {
{{#description}}
/** {{description}} */{{/description}}
final class {{classname}}: JSONEncodable, JSONDecodable {
{{#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}}
}
{{/models}}

View File

@@ -0,0 +1,2 @@
github "Alamofire/Alamofire" >= 1.2
github "mxcl/PromiseKit"