@@ -44,7 +44,7 @@ public class ModelGenerator {
44
44
*/
45
45
public func generate( ) {
46
46
47
- let name : String = generateModelForClass ( baseContent, className: baseClassName)
47
+ let name : String = generateModelForClass ( baseContent, className: baseClassName, isSubModule : false )
48
48
49
49
// Notify user that the files are generated!
50
50
let notification : NSUserNotification = NSUserNotification ( )
@@ -65,15 +65,15 @@ public class ModelGenerator {
65
65
66
66
- returns: Returns the final name of the class.
67
67
*/
68
- internal func generateModelForClass( parsedJSONObject: JSON , className: String ) -> String {
68
+ internal func generateModelForClass( parsedJSONObject: JSON , className: String , isSubModule : Bool ) -> String {
69
69
70
70
var declarations : String = " "
71
71
var stringConstants : String = " "
72
72
var initalizers : String = " "
73
73
var encoders : String = " "
74
74
var decoders : String = " "
75
75
76
- let className = buildClassName ( className, prefix: self . prefix!)
76
+ let className = buildClassName ( className, prefix: self . prefix!, isSubModule : isSubModule )
77
77
if let object = parsedJSONObject. dictionary {
78
78
for (key, subJson) in object {
79
79
@@ -88,10 +88,19 @@ public class ModelGenerator {
88
88
89
89
// If the array has objects, then take the first one and proces it to generate a model.
90
90
if subJson. arrayValue. count > 0 {
91
- let subClassName = generateModelForClass ( subJson. arrayValue [ 0 ] , className: variableName)
92
- declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: " [ \( subClassName) ] " ) )
93
- initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObjectArray ( variableName, className: subClassName, key: stringConstantName) )
94
- decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: " [ \( subClassName) ] " ) )
91
+ let subClassType = checkType ( subJson. arrayValue [ 0 ] )
92
+
93
+ if subClassType == VariableType . kObjectType {
94
+ let subClassName = generateModelForClass ( subJson. arrayValue [ 0 ] , className: variableName, isSubModule: true )
95
+ declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: " [ \( subClassName) ] " ) )
96
+ initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObjectArray ( variableName, className: subClassName, key: stringConstantName) )
97
+ decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: " [ \( subClassName) ] " ) )
98
+ } else {
99
+ declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: " [ \( subClassType) ] " ) )
100
+ initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForVariableArray ( variableName, key: stringConstantName, type: subClassType) )
101
+ decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: " [ \( subClassType) ] " ) )
102
+ }
103
+
95
104
} else {
96
105
// if nothing is there make it a blank array.
97
106
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: variableType) )
@@ -101,7 +110,7 @@ public class ModelGenerator {
101
110
102
111
} else if variableType == VariableType . kObjectType {
103
112
104
- let subClassName = generateModelForClass ( subJson, className: variableName)
113
+ let subClassName = generateModelForClass ( subJson, className: variableName, isSubModule : true )
105
114
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: subClassName) )
106
115
initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObject ( variableName, className: subClassName, key: stringConstantName) )
107
116
decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: subClassName) )
@@ -154,8 +163,10 @@ public class ModelGenerator {
154
163
155
164
- returns: A generated string representing the name of the class in the model.
156
165
*/
157
- internal func buildClassName( className: String , prefix: String ) -> String {
158
- var classNameCleaned = variableNameBuilder ( className)
166
+ internal func buildClassName( className: String , prefix: String , isSubModule: Bool ) -> String {
167
+
168
+ // If it is a submodule it is already formatted no need to camelcase it.
169
+ var classNameCleaned = isSubModule ? className : variableNameBuilder ( className)
159
170
classNameCleaned. replaceRange ( classNameCleaned. startIndex... classNameCleaned. startIndex, with: String ( classNameCleaned [ classNameCleaned. startIndex] ) . uppercaseString)
160
171
return prefix. stringByAppendingString ( classNameCleaned)
161
172
}
@@ -226,11 +237,7 @@ public class ModelGenerator {
226
237
227
238
228
239
internal func initalizerForVariable( variableName: String , var type: String , key: String ) -> String {
229
- if type == VariableType . kNumberType {
230
- type = " number "
231
- } else {
232
- type. replaceRange ( type. startIndex... type. startIndex, with: String ( type [ type. startIndex] ) . lowercaseString)
233
- }
240
+ type = typeToSwiftType ( type)
234
241
return " \t \t if let value = json[ \( key) ]. \( type) { \n \t \t \t \( variableName) = value \n \t \t } "
235
242
}
236
243
@@ -245,12 +252,17 @@ public class ModelGenerator {
245
252
internal func initalizerForObjectArray( variableName: String , className: String , key: String ) -> String {
246
253
return " \t \t \( variableName) = [] \n \t \t if let items = json[ \( key) ].array { \n \t \t \t for item in items { \n \t \t \t \t \( variableName) ?.append( \( className) (json: item)) \n \t \t \t } \n \t \t } \n "
247
254
}
255
+
256
+ internal func initalizerForVariableArray( variableName: String , key: String , var type: String ) -> String {
257
+ type = typeToSwiftType ( type)
258
+ return " \t \t \( variableName) = [] \n \t \t if let items = json[ \( key) ].array { \n \t \t \t for item in items { \n \t \t \t \t if let value = item. \( type) { \n \t \t \t \t \( variableName) ?.append(value) \n \t \t \t \t } \n \t \t \t } \n \t \t } \n "
259
+ }
248
260
249
261
internal func encoderForVariable( variableName: String , key: String , type: String ) -> String {
250
262
if type == VariableType . kBoolType {
251
263
return " \t \t aCoder.encodeBool( \( variableName) , forKey: \( key) ) "
252
264
}
253
- return " \t \t aCoder.encodeObject( \( variableName) , forKey: \( key) ) "
265
+ return " \t \t aCoder.encodeObject( \( variableName) , forKey: \( key) ) "
254
266
}
255
267
256
268
internal func decoderForVariable( variableName: String , key: String , type: String ) -> String {
@@ -304,15 +316,21 @@ public class ModelGenerator {
304
316
}
305
317
}
306
318
307
- /*
308
- required init(coder aDecoder: NSCoder) {
309
- self.bottlesArray = aDecoder.decodeObjectForKey("bottleArray") as NSMutableArray
310
- }
319
+ /**
320
+ Generates a swift variable type from the given VariableType.
321
+
322
+ - parameter type: VariableType
311
323
312
- func encodeWithCoder(aCoder: NSCoder) {
313
- aCoder.encodeObject(bottlesArray, forKey: "bottleArray")
324
+ - returns: swift variable type.
325
+ */
326
+ internal func typeToSwiftType( var type: String ) -> String {
327
+ if type == VariableType . kNumberType {
328
+ type = " number "
329
+ } else {
330
+ type. replaceRange ( type. startIndex... type. startIndex, with: String ( type [ type. startIndex] ) . lowercaseString)
331
+ }
332
+
333
+ return type
314
334
}
315
- */
316
-
317
335
318
336
}
0 commit comments