added async scala generator #440

This commit is contained in:
Tony Tam
2015-02-22 08:57:40 -08:00
parent 1c7c39c1ab
commit bfc1ed3081
21 changed files with 851 additions and 52 deletions

View File

@@ -0,0 +1,194 @@
package com.wordnik.swagger.codegen.languages;
import com.wordnik.swagger.codegen.*;
import com.wordnik.swagger.models.properties.*;
import java.util.*;
import java.io.File;
public class AsyncScalaClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String invokerPackage = "io.swagger.client";
protected String groupId = "com.wordnik";
protected String artifactId = "swagger-client";
protected String artifactVersion = "1.0.0";
protected String sourceFolder = "src/main/scala";
protected String clientName = "SwaggerClient";
protected String authScheme = "";
protected boolean authPreemptive = false;
protected boolean asyncHttpClient = !authScheme.isEmpty();
public CodegenType getTag() {
return CodegenType.CLIENT;
}
public String getName() {
return "async-scala";
}
public String getHelp() {
return "Generates an Asynchronous Scala client library.";
}
public AsyncScalaClientCodegen() {
super();
outputFolder = "generated-code/async-scala";
modelTemplateFiles.put("model.mustache", ".scala");
apiTemplateFiles.put("api.mustache", ".scala");
templateDir = "asyncscala";
apiPackage = "io.swagger.client.api";
modelPackage = "io.swagger.client.model";
reservedWords = new HashSet<String> (
Arrays.asList(
"abstract", "case", "catch", "class", "def", "do", "else", "extends",
"false", "final", "finally", "for", "forSome", "if", "implicit",
"import", "lazy", "match", "new", "null", "object", "override", "package",
"private", "protected", "return", "sealed", "super", "this", "throw",
"trait", "try", "true", "type", "val", "var", "while", "with", "yield")
);
additionalProperties.put("invokerPackage", invokerPackage);
additionalProperties.put("groupId", groupId);
additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion);
additionalProperties.put("asyncHttpClient", asyncHttpClient);
additionalProperties.put("authScheme", authScheme);
additionalProperties.put("authPreemptive", authPreemptive);
additionalProperties.put("clientName", clientName);
supportingFiles.add(new SupportingFile("sbt.mustache", "", "build.sbt"));
supportingFiles.add(new SupportingFile("client.mustache",
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), clientName + ".scala"));
importMapping.remove("List");
importMapping.remove("Set");
importMapping.remove("Map");
importMapping.put("DateTime", "org.joda.time.DateTime");
importMapping.put("ListBuffer", "scala.collections.mutable.ListBuffer");
typeMapping = new HashMap<String, String>();
typeMapping.put("enum", "NSString");
typeMapping.put("array", "List");
typeMapping.put("set", "Set");
typeMapping.put("boolean", "Boolean");
typeMapping.put("string", "String");
typeMapping.put("int", "Int");
typeMapping.put("long", "Long");
typeMapping.put("float", "Float");
typeMapping.put("byte", "Byte");
typeMapping.put("short", "Short");
typeMapping.put("char", "Char");
typeMapping.put("long", "Long");
typeMapping.put("double", "Double");
typeMapping.put("object", "Any");
typeMapping.put("file", "File");
languageSpecificPrimitives = new HashSet<String>(
Arrays.asList(
"String",
"boolean",
"Boolean",
"Double",
"Int",
"Long",
"Float",
"Object",
"List",
"Map")
);
instantiationTypes.put("array", "ListBuffer");
instantiationTypes.put("map", "HashMap");
}
@Override
public String escapeReservedWord(String name) {
return "_" + name;
}
@Override
public String apiFileFolder() {
return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
}
public String modelFileFolder() {
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
}
@Override
public String getTypeDeclaration(Property p) {
if(p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return getSwaggerType(p) + "[" + getTypeDeclaration(inner) + "]";
}
else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
return getSwaggerType(p) + "[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 toInstantiationType(Property p) {
if (p instanceof MapProperty) {
MapProperty ap = (MapProperty) p;
String inner = getSwaggerType(ap.getAdditionalProperties());
return instantiationTypes.get("map") + "[String, " + inner + "]";
}
else if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
String inner = getSwaggerType(ap.getItems());
return instantiationTypes.get("array") + "[" + inner + "]";
}
else
return null;
}
public String toDefaultValue(Property p) {
if(p instanceof StringProperty)
return "null";
else if (p instanceof BooleanProperty)
return "null";
else if(p instanceof DateProperty)
return "null";
else if(p instanceof DateTimeProperty)
return "null";
else if (p instanceof DoubleProperty)
return "null";
else if (p instanceof FloatProperty)
return "null";
else if (p instanceof IntegerProperty)
return "null";
else if (p instanceof LongProperty)
return "null";
else if (p instanceof MapProperty) {
MapProperty ap = (MapProperty) p;
String inner = getSwaggerType(ap.getAdditionalProperties());
return "new HashMap[String, " + inner + "]() ";
}
else if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
String inner = getSwaggerType(ap.getItems());
return "new ListBuffer[" + inner + "]() ";
}
else
return "null";
}
}

View File

@@ -1,4 +1,5 @@
com.wordnik.swagger.codegen.languages.AndroidClientCodegen
com.wordnik.swagger.codegen.languages.AsyncScalaClientCodegen
com.wordnik.swagger.codegen.languages.JavaClientCodegen
com.wordnik.swagger.codegen.languages.JaxRSServerCodegen
com.wordnik.swagger.codegen.languages.NodeJSServerCodegen
@@ -10,4 +11,4 @@ com.wordnik.swagger.codegen.languages.StaticHtmlGenerator
com.wordnik.swagger.codegen.languages.SwaggerGenerator
com.wordnik.swagger.codegen.languages.TizenClientCodegen
com.wordnik.swagger.codegen.languages.PhpClientCodegen
com.wordnik.swagger.codegen.languages.PythonClientCodegen
com.wordnik.swagger.codegen.languages.PythonClientCodegen

View File

@@ -8,46 +8,31 @@ import scala.concurrent.duration._
import collection.mutable
{{#operations}}
class {{className}}(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) {
class {{classname}}(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) {
{{#operation}}
def {{nickname}}({{#allParams}}
{{#optional}}
{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}}, {{/hasMore}}
{{/optional}}
{{^optional}}
{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}}, {{/hasMore}}
{{/optional}}{{/allParams}})(implicit reader: ClientResponseReader[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]{{#bodyParams}}, writer: RequestWriter[{{dataType}}]{{/bodyParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}}{{^returnType}}: Future[Unit]{{/returnType}} = {
def {{nickname}}({{#allParams}}{{#optional}}{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}},
{{/hasMore}}
{{/optional}}{{^optional}}{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}},
{{/hasMore}}{{/optional}}{{/allParams}})(implicit reader: ClientResponseReader[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]{{#bodyParams}}, writer: RequestWriter[{{dataType}}]{{/bodyParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}}{{^returnType}}: Future[Unit]{{/returnType}} = {
// create path and map variables
val path =
(addFmt("{{path}}"){{#pathParams}}
replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString)
{{/pathParams}})
val path = (addFmt("{{path}}"){{#pathParams}}
replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString){{/pathParams}})
// query params
val queryParams = new mutable.HashMap[String, String]
val headerParams = new mutable.HashMap[String, String]
{{#requiredParamCount}}
// verify required params are set
{{#requiredParamCount}}// verify required params are set
val paramCount = (Set[Any]({{/requiredParamCount}}{{#requiredParams}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) - null).size
if (paramCount != {{requiredParamCount}}) sys.error("missing required params")
{{/requiredParamCount}}
if (paramCount != {{requiredParamCount}}) sys.error("missing required params"){{/requiredParamCount}}
{{#queryParams}}
{{#optional}}
if({{paramName}} != null) {{paramName}}.foreach { v => queryParams += "{{baseName}}" -> v.toString }
{{/optional}}
{{^optional}}
if({{paramName}} != null) queryParams += "{{baseName}}" -> {{paramName}}.toString
{{/optional}}
{{/queryParams}}
{{#queryParams}}{{#optional}}if({{paramName}} != null) {{paramName}}.foreach { v => queryParams += "{{baseName}}" -> v.toString }{{/optional}}{{^optional}}
if({{paramName}} != null) queryParams += "{{baseName}}" -> {{paramName}}.toString{{/optional}}{{/queryParams}}
{{#headerParams}}headerParams += "{{baseName}}" -> {{paramName}}.toString
{{/headerParams}}
{{#headerParams}}headerParams += "{{baseName}}" -> {{paramName}}.toString{{/headerParams}}
val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{bodyParam}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}})
val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{paramName}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}})
resFuture flatMap { resp =>
process(reader.read(resp))
}

View File

@@ -1,9 +1,11 @@
package {{package}}
package {{invokerPackage}}
{{#imports}}import {{import}}
{{/imports}}
import {{apiPackage}}._
import com.wordnik.swagger.client._
import apis._
import java.io.Closeable
class {{clientName}}(config: SwaggerConfig) extends Closeable {
@@ -13,12 +15,9 @@ class {{clientName}}(config: SwaggerConfig) extends Closeable {
private[this] val client = transportClient
protected def transportClient: TransportClient = new RestClient(config)
{{#apiInfo}}
{{#apis}}
val {{name}} = new {{className}}(client, config)
{{/apis}}
{{/apiInfo}}
{{#apiInfo}}{{#apis}}
val {{classVarName}} = new {{classname}}(client, config)
{{/apis}}{{/apiInfo}}
def close() {
client.close()

View File

@@ -6,9 +6,7 @@ import org.joda.time.DateTime
{{#model}}
case class {{classname}} (
{{#vars}}
{{name}}: {{#isNotRequired}}Option[{{/isNotRequired}}{{datatype}}{{#isNotRequired}}]{{/isNotRequired}} {{#hasMore}},{{/hasMore}}{{#description}} // {{description}}{{/description}}{{newline}}
{{#vars}}{{name}}: {{#isNotRequired}}Option[{{/isNotRequired}}{{datatype}}{{#isNotRequired}}]{{/isNotRequired}}{{#hasMore}},{{/hasMore}}{{#description}} // {{description}}{{/description}}
{{/vars}}
)
{{/model}}

View File

@@ -2,7 +2,7 @@ organization := "{{package}}"
name := "{{projectName}}-client"
libraryDependencies += "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.0-WN5"
libraryDependencies += "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5"
libraryDependencies += "joda-time" % "joda-time" % "2.3"

View File

@@ -1,7 +1,7 @@
{
"swagger": "2.0",
"info": {
"description": "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.wordnik.com\">http://swagger.wordnik.com</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters",
"description": "This is a sample server Petstore server. You can find out more about Swagger at <a href=\"http://swagger.io\">http://swagger.io</a> or on irc.freenode.net, #swagger. For this sample, you can use the api key \"special-key\" to test the authorization filters",
"version": "1.0.0",
"title": "Swagger Petstore",
"termsOfService": "http://helloreverb.com/terms/",
@@ -13,7 +13,7 @@
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"host": "petstore.swagger.wordnik.com",
"host": "petstore.swagger.io",
"basePath": "/v2",
"schemes": [
"http"