Skip to content

Commit bfc1ed3

Browse files
committed
added async scala generator #440
1 parent 1c7c39c commit bfc1ed3

File tree

21 files changed

+854
-55
lines changed

21 files changed

+854
-55
lines changed

bin/Version.scala

Lines changed: 0 additions & 6 deletions
This file was deleted.

bin/scala-async-petstore.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
root=./modules/swagger-codegen-distribution/pom.xml
21+
22+
# gets version of swagger-codegen
23+
version=$(sed '/<project>/,/<\/project>/d;/<version>/!d;s/ *<\/\?version> *//g' $root | sed -n '2p' | sed -e 's,.*<version>\([^<]*\)</version>.*,\1,g')
24+
25+
executable="./modules/swagger-codegen-distribution/target/swagger-codegen-distribution-$version.jar"
26+
27+
if [ ! -f "$executable" ]
28+
then
29+
mvn clean package
30+
fi
31+
32+
# if you've executed sbt assembly previously it will use that instead.
33+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
34+
ags="$@ -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l async-scala -o samples/client/petstore/async-scala"
35+
36+
java $JAVA_OPTS -jar $executable $ags
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package com.wordnik.swagger.codegen.languages;
2+
3+
import com.wordnik.swagger.codegen.*;
4+
import com.wordnik.swagger.models.properties.*;
5+
6+
import java.util.*;
7+
import java.io.File;
8+
9+
public class AsyncScalaClientCodegen extends DefaultCodegen implements CodegenConfig {
10+
protected String invokerPackage = "io.swagger.client";
11+
protected String groupId = "com.wordnik";
12+
protected String artifactId = "swagger-client";
13+
protected String artifactVersion = "1.0.0";
14+
protected String sourceFolder = "src/main/scala";
15+
protected String clientName = "SwaggerClient";
16+
protected String authScheme = "";
17+
protected boolean authPreemptive = false;
18+
protected boolean asyncHttpClient = !authScheme.isEmpty();
19+
20+
public CodegenType getTag() {
21+
return CodegenType.CLIENT;
22+
}
23+
24+
public String getName() {
25+
return "async-scala";
26+
}
27+
28+
public String getHelp() {
29+
return "Generates an Asynchronous Scala client library.";
30+
}
31+
32+
public AsyncScalaClientCodegen() {
33+
super();
34+
outputFolder = "generated-code/async-scala";
35+
modelTemplateFiles.put("model.mustache", ".scala");
36+
apiTemplateFiles.put("api.mustache", ".scala");
37+
templateDir = "asyncscala";
38+
apiPackage = "io.swagger.client.api";
39+
modelPackage = "io.swagger.client.model";
40+
41+
reservedWords = new HashSet<String> (
42+
Arrays.asList(
43+
"abstract", "case", "catch", "class", "def", "do", "else", "extends",
44+
"false", "final", "finally", "for", "forSome", "if", "implicit",
45+
"import", "lazy", "match", "new", "null", "object", "override", "package",
46+
"private", "protected", "return", "sealed", "super", "this", "throw",
47+
"trait", "try", "true", "type", "val", "var", "while", "with", "yield")
48+
);
49+
50+
additionalProperties.put("invokerPackage", invokerPackage);
51+
additionalProperties.put("groupId", groupId);
52+
additionalProperties.put("artifactId", artifactId);
53+
additionalProperties.put("artifactVersion", artifactVersion);
54+
additionalProperties.put("asyncHttpClient", asyncHttpClient);
55+
additionalProperties.put("authScheme", authScheme);
56+
additionalProperties.put("authPreemptive", authPreemptive);
57+
additionalProperties.put("clientName", clientName);
58+
59+
supportingFiles.add(new SupportingFile("sbt.mustache", "", "build.sbt"));
60+
supportingFiles.add(new SupportingFile("client.mustache",
61+
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), clientName + ".scala"));
62+
63+
importMapping.remove("List");
64+
importMapping.remove("Set");
65+
importMapping.remove("Map");
66+
67+
importMapping.put("DateTime", "org.joda.time.DateTime");
68+
importMapping.put("ListBuffer", "scala.collections.mutable.ListBuffer");
69+
70+
typeMapping = new HashMap<String, String>();
71+
typeMapping.put("enum", "NSString");
72+
typeMapping.put("array", "List");
73+
typeMapping.put("set", "Set");
74+
typeMapping.put("boolean", "Boolean");
75+
typeMapping.put("string", "String");
76+
typeMapping.put("int", "Int");
77+
typeMapping.put("long", "Long");
78+
typeMapping.put("float", "Float");
79+
typeMapping.put("byte", "Byte");
80+
typeMapping.put("short", "Short");
81+
typeMapping.put("char", "Char");
82+
typeMapping.put("long", "Long");
83+
typeMapping.put("double", "Double");
84+
typeMapping.put("object", "Any");
85+
typeMapping.put("file", "File");
86+
87+
languageSpecificPrimitives = new HashSet<String>(
88+
Arrays.asList(
89+
"String",
90+
"boolean",
91+
"Boolean",
92+
"Double",
93+
"Int",
94+
"Long",
95+
"Float",
96+
"Object",
97+
"List",
98+
"Map")
99+
);
100+
instantiationTypes.put("array", "ListBuffer");
101+
instantiationTypes.put("map", "HashMap");
102+
}
103+
104+
@Override
105+
public String escapeReservedWord(String name) {
106+
return "_" + name;
107+
}
108+
109+
@Override
110+
public String apiFileFolder() {
111+
return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
112+
}
113+
114+
public String modelFileFolder() {
115+
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
116+
}
117+
118+
@Override
119+
public String getTypeDeclaration(Property p) {
120+
if(p instanceof ArrayProperty) {
121+
ArrayProperty ap = (ArrayProperty) p;
122+
Property inner = ap.getItems();
123+
return getSwaggerType(p) + "[" + getTypeDeclaration(inner) + "]";
124+
}
125+
else if (p instanceof MapProperty) {
126+
MapProperty mp = (MapProperty) p;
127+
Property inner = mp.getAdditionalProperties();
128+
129+
return getSwaggerType(p) + "[String, " + getTypeDeclaration(inner) + "]";
130+
}
131+
return super.getTypeDeclaration(p);
132+
}
133+
134+
@Override
135+
public String getSwaggerType(Property p) {
136+
String swaggerType = super.getSwaggerType(p);
137+
String type = null;
138+
if(typeMapping.containsKey(swaggerType)) {
139+
type = typeMapping.get(swaggerType);
140+
if(languageSpecificPrimitives.contains(type))
141+
return toModelName(type);
142+
}
143+
else
144+
type = swaggerType;
145+
return toModelName(type);
146+
}
147+
148+
@Override
149+
public String toInstantiationType(Property p) {
150+
if (p instanceof MapProperty) {
151+
MapProperty ap = (MapProperty) p;
152+
String inner = getSwaggerType(ap.getAdditionalProperties());
153+
return instantiationTypes.get("map") + "[String, " + inner + "]";
154+
}
155+
else if (p instanceof ArrayProperty) {
156+
ArrayProperty ap = (ArrayProperty) p;
157+
String inner = getSwaggerType(ap.getItems());
158+
return instantiationTypes.get("array") + "[" + inner + "]";
159+
}
160+
else
161+
return null;
162+
}
163+
164+
public String toDefaultValue(Property p) {
165+
if(p instanceof StringProperty)
166+
return "null";
167+
else if (p instanceof BooleanProperty)
168+
return "null";
169+
else if(p instanceof DateProperty)
170+
return "null";
171+
else if(p instanceof DateTimeProperty)
172+
return "null";
173+
else if (p instanceof DoubleProperty)
174+
return "null";
175+
else if (p instanceof FloatProperty)
176+
return "null";
177+
else if (p instanceof IntegerProperty)
178+
return "null";
179+
else if (p instanceof LongProperty)
180+
return "null";
181+
else if (p instanceof MapProperty) {
182+
MapProperty ap = (MapProperty) p;
183+
String inner = getSwaggerType(ap.getAdditionalProperties());
184+
return "new HashMap[String, " + inner + "]() ";
185+
}
186+
else if (p instanceof ArrayProperty) {
187+
ArrayProperty ap = (ArrayProperty) p;
188+
String inner = getSwaggerType(ap.getItems());
189+
return "new ListBuffer[" + inner + "]() ";
190+
}
191+
else
192+
return "null";
193+
}
194+
}

modules/swagger-codegen/src/main/resources/META-INF/services/com.wordnik.swagger.codegen.CodegenConfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
com.wordnik.swagger.codegen.languages.AndroidClientCodegen
2+
com.wordnik.swagger.codegen.languages.AsyncScalaClientCodegen
23
com.wordnik.swagger.codegen.languages.JavaClientCodegen
34
com.wordnik.swagger.codegen.languages.JaxRSServerCodegen
45
com.wordnik.swagger.codegen.languages.NodeJSServerCodegen
@@ -10,4 +11,4 @@ com.wordnik.swagger.codegen.languages.StaticHtmlGenerator
1011
com.wordnik.swagger.codegen.languages.SwaggerGenerator
1112
com.wordnik.swagger.codegen.languages.TizenClientCodegen
1213
com.wordnik.swagger.codegen.languages.PhpClientCodegen
13-
com.wordnik.swagger.codegen.languages.PythonClientCodegen
14+
com.wordnik.swagger.codegen.languages.PythonClientCodegen

modules/swagger-codegen/src/main/resources/asyncscala/api.mustache

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,31 @@ import scala.concurrent.duration._
88
import collection.mutable
99

1010
{{#operations}}
11-
class {{className}}(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) {
11+
class {{classname}}(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) {
1212
1313
{{#operation}}
14-
15-
def {{nickname}}({{#allParams}}
16-
{{#optional}}
17-
{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}}, {{/hasMore}}
18-
{{/optional}}
19-
{{^optional}}
20-
{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}}, {{/hasMore}}
21-
{{/optional}}{{/allParams}})(implicit reader: ClientResponseReader[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]{{#bodyParams}}, writer: RequestWriter[{{dataType}}]{{/bodyParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}}{{^returnType}}: Future[Unit]{{/returnType}} = {
14+
def {{nickname}}({{#allParams}}{{#optional}}{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}},
15+
{{/hasMore}}
16+
{{/optional}}{{^optional}}{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}},
17+
{{/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}} = {
2218
// create path and map variables
23-
val path =
24-
(addFmt("{{path}}"){{#pathParams}}
25-
replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString)
26-
{{/pathParams}})
19+
val path = (addFmt("{{path}}"){{#pathParams}}
20+
replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString){{/pathParams}})
2721

2822
// query params
2923
val queryParams = new mutable.HashMap[String, String]
3024
val headerParams = new mutable.HashMap[String, String]
3125

32-
{{#requiredParamCount}}
33-
// verify required params are set
26+
{{#requiredParamCount}}// verify required params are set
3427
val paramCount = (Set[Any]({{/requiredParamCount}}{{#requiredParams}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) - null).size
35-
if (paramCount != {{requiredParamCount}}) sys.error("missing required params")
36-
{{/requiredParamCount}}
37-
38-
{{#queryParams}}
39-
{{#optional}}
40-
if({{paramName}} != null) {{paramName}}.foreach { v => queryParams += "{{baseName}}" -> v.toString }
41-
{{/optional}}
42-
{{^optional}}
43-
if({{paramName}} != null) queryParams += "{{baseName}}" -> {{paramName}}.toString
44-
{{/optional}}
45-
{{/queryParams}}
46-
47-
{{#headerParams}}headerParams += "{{baseName}}" -> {{paramName}}.toString
48-
{{/headerParams}}
49-
50-
val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{bodyParam}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}})
28+
if (paramCount != {{requiredParamCount}}) sys.error("missing required params"){{/requiredParamCount}}
29+
30+
{{#queryParams}}{{#optional}}if({{paramName}} != null) {{paramName}}.foreach { v => queryParams += "{{baseName}}" -> v.toString }{{/optional}}{{^optional}}
31+
if({{paramName}} != null) queryParams += "{{baseName}}" -> {{paramName}}.toString{{/optional}}{{/queryParams}}
32+
33+
{{#headerParams}}headerParams += "{{baseName}}" -> {{paramName}}.toString{{/headerParams}}
34+
35+
val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{paramName}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}})
5136
resFuture flatMap { resp =>
5237
process(reader.read(resp))
5338
}

modules/swagger-codegen/src/main/resources/asyncscala/client.mustache

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package {{package}}
1+
package {{invokerPackage}}
22

33
{{#imports}}import {{import}}
44
{{/imports}}
5+
import {{apiPackage}}._
6+
57
import com.wordnik.swagger.client._
6-
import apis._
8+
79
import java.io.Closeable
810

911
class {{clientName}}(config: SwaggerConfig) extends Closeable {
@@ -13,12 +15,9 @@ class {{clientName}}(config: SwaggerConfig) extends Closeable {
1315
private[this] val client = transportClient
1416
1517
protected def transportClient: TransportClient = new RestClient(config)
16-
17-
{{#apiInfo}}
18-
{{#apis}}
19-
val {{name}} = new {{className}}(client, config)
20-
{{/apis}}
21-
{{/apiInfo}}
18+
{{#apiInfo}}{{#apis}}
19+
val {{classVarName}} = new {{classname}}(client, config)
20+
{{/apis}}{{/apiInfo}}
2221

2322
def close() {
2423
client.close()

modules/swagger-codegen/src/main/resources/asyncscala/model.mustache

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import org.joda.time.DateTime
66

77
{{#model}}
88
case class {{classname}} (
9-
{{#vars}}
10-
11-
{{name}}: {{#isNotRequired}}Option[{{/isNotRequired}}{{datatype}}{{#isNotRequired}}]{{/isNotRequired}} {{#hasMore}},{{/hasMore}}{{#description}} // {{description}}{{/description}}{{newline}}
9+
{{#vars}}{{name}}: {{#isNotRequired}}Option[{{/isNotRequired}}{{datatype}}{{#isNotRequired}}]{{/isNotRequired}}{{#hasMore}},{{/hasMore}}{{#description}} // {{description}}{{/description}}
1210
{{/vars}}
1311
)
1412
{{/model}}

modules/swagger-codegen/src/main/resources/asyncscala/sbt.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ organization := "{{package}}"
22

33
name := "{{projectName}}-client"
44

5-
libraryDependencies += "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.0-WN5"
5+
libraryDependencies += "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.3.5"
66

77
libraryDependencies += "joda-time" % "joda-time" % "2.3"
88

modules/swagger-codegen/src/test/resources/2_0/postBodyTest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"swagger": "2.0",
33
"info": {
4-
"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",
4+
"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",
55
"version": "1.0.0",
66
"title": "Swagger Petstore",
77
"termsOfService": "http://helloreverb.com/terms/",
@@ -13,7 +13,7 @@
1313
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
1414
}
1515
},
16-
"host": "petstore.swagger.wordnik.com",
16+
"host": "petstore.swagger.io",
1717
"basePath": "/v2",
1818
"schemes": [
1919
"http"

modules/swagger-generator/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"url": "http://github.com/gruntjs/grunt/blob/master/LICENSE-MIT"
1515
}
1616
},
17-
"host": "petstore.swagger.wordnik.com",
17+
"host": "petstore.swagger.io",
1818
"basePath": "/api",
1919
"paths": {
2020
"/pet": {

0 commit comments

Comments
 (0)