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
+ }
0 commit comments