@@ -31,8 +31,8 @@ struct VariableType {
31
31
* - kStructType: Struct type
32
32
*/
33
33
public struct ModelType {
34
- static let kClassType : String = " Class "
35
- static let kStructType : String = " Struct "
34
+ static let kClassType : String = " class "
35
+ static let kStructType : String = " struct "
36
36
}
37
37
38
38
/// Model generator responsible for creation of models based on the JSON, needs to be initalized with all properties before proceeding.
@@ -106,6 +106,7 @@ public class ModelGenerator {
106
106
var initalizers : String = " "
107
107
var encoders : String = " "
108
108
var decoders : String = " "
109
+ var description : String = " "
109
110
110
111
/// Create a classname in Sentence case and removing unwanted stuff.
111
112
let className = buildClassName ( className, prefix: self . prefix!, isSubModule: isSubModule)
@@ -138,12 +139,13 @@ public class ModelGenerator {
138
139
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: " [ \( subClassName) ] " ) )
139
140
initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObjectArray ( variableName, className: subClassName, key: stringConstantName) )
140
141
decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: " [ \( subClassName) ] " ) )
141
-
142
+ description = description . stringByAppendingFormat ( " %@ \n " , descriptionForObjectArray ( variableName , key : stringConstantName ) )
142
143
} else {
143
144
// If it is anything other than an object, it should be a primitive type hence deal with it accordingly.
144
145
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: " [ \( subClassType) ] " ) )
145
146
initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForPrimitiveVariableArray ( variableName, key: stringConstantName, type: subClassType) )
146
147
decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: " [ \( subClassType) ] " ) )
148
+ description = description. stringByAppendingFormat ( " %@ \n " , descriptionForPrimitiveVariableArray ( variableName, key: stringConstantName) )
147
149
}
148
150
// TODO: We should also consider a third case where the type is an [AnyObject] to achive complete redundancy handling.
149
151
@@ -162,13 +164,14 @@ public class ModelGenerator {
162
164
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: subClassName) )
163
165
initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForObject ( variableName, className: subClassName, key: stringConstantName) )
164
166
decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: subClassName) )
167
+ description = description. stringByAppendingFormat ( " %@ \n " , descriptionForObjectVariableArray ( variableName, key: stringConstantName) )
165
168
166
169
} else {
167
170
// If it is a primitive then simply create initalizers, declarations and decoders.
168
171
declarations = declarations. stringByAppendingFormat ( variableDeclarationBuilder ( variableName, type: variableType) )
169
172
initalizers = initalizers. stringByAppendingFormat ( " %@ \n " , initalizerForVariable ( variableName, type: variableType, key: stringConstantName) )
170
173
decoders = decoders. stringByAppendingFormat ( " %@ \n " , decoderForVariable ( variableName, key: stringConstantName, type: variableType) )
171
-
174
+ description = description . stringByAppendingFormat ( " %@ \n " , descriptionForVariable ( variableName , key : stringConstantName ) )
172
175
}
173
176
174
177
}
@@ -186,6 +189,7 @@ public class ModelGenerator {
186
189
content = content. stringByReplacingOccurrencesOfString ( " {INITALIZER} " , withString: initalizers)
187
190
content = content. stringByReplacingOccurrencesOfString ( " {ENCODERS} " , withString: encoders)
188
191
content = content. stringByReplacingOccurrencesOfString ( " {DECODERS} " , withString: decoders)
192
+ content = content. stringByReplacingOccurrencesOfString ( " {DESC} " , withString: description)
189
193
190
194
if authorName != nil {
191
195
content = content. stringByReplacingOccurrencesOfString ( " __NAME__ " , withString: authorName!)
@@ -229,8 +233,7 @@ public class ModelGenerator {
229
233
- returns: A generated string representation of the variable name.
230
234
*/
231
235
internal func variableNameBuilder( variableName: String ) -> String {
232
-
233
- let variableName = variableName. stringByReplacingOccurrencesOfString ( " _ " , withString: " " )
236
+ let variableName = replaceInternalKeywordsForVariableName ( variableName) . stringByReplacingOccurrencesOfString ( " _ " , withString: " " )
234
237
var finalVariableName : String = " "
235
238
for (index, element) in variableName. componentsSeparatedByString ( " " ) . enumerate ( ) {
236
239
var component : String = element
@@ -356,10 +359,10 @@ public class ModelGenerator {
356
359
}
357
360
358
361
/**
359
- Encoder for a variable.
362
+ Initalizer for an primitive kind of elements array variable.
360
363
- parameter variableName: Variable name.
361
364
- parameter key: Key against which the value is stored.
362
- - returns: A single line encoder of the variable.
365
+ - returns: A single line declaration of the variable which is an array of primitive kind .
363
366
*/
364
367
internal func initalizerForPrimitiveVariableArray( variableName: String , key: String , var type: String ) -> String {
365
368
type = typeToSwiftType ( type)
@@ -391,6 +394,64 @@ public class ModelGenerator {
391
394
return " \t \t self. \( variableName) = aDecoder.decodeObjectForKey( \( key) ) as? \( type) "
392
395
}
393
396
397
+ /**
398
+ Initialization of the variable "if let value = json[{key}].{type} { variableName = value }"
399
+ - parameter variableName: Variable name.
400
+ - parameter type: Type of the variable.
401
+ - parameter key: Key against which the value is stored.
402
+
403
+ - returns: A single line declaration of the variable.
404
+ */
405
+ internal func initalize( variableName: String , var type: String , key: String ) -> String {
406
+ type = typeToSwiftType ( type)
407
+ return " \t \t if let value = json[ \( key) ]. \( type) { \n \t \t \t \( variableName) = value \n \t \t } "
408
+ }
409
+
410
+ /**
411
+ Description of the variable if {variableName} != nil { dictionary.updateValue({variableName}!, forKey: {key})
412
+ }
413
+ - parameter variableName: Variable name.
414
+ - parameter type: Type of the variable.
415
+ - parameter key: Key against which the value is stored.
416
+
417
+ - returns: A single line description printer of the variable.
418
+ */
419
+ internal func descriptionForVariable( variableName: String , key: String ) -> String {
420
+ return " \t \t if \( variableName) != nil { \n \t \t \t dictionary.updateValue( \( variableName) !, forKey: \( key) ) \n \t \t } "
421
+ }
422
+
423
+ /**
424
+ Description for an Object kind of an array variable. if {variableName}?.count > 0 { var temp: [AnyObject] = [] for item in {variableName}! { temp.append(item.dictionaryRepresentation()) } dictionary.updateValue(temp, forKey: {key}) }
425
+ }
426
+ - parameter variableName: Variable name.
427
+ - parameter className: Name of the Class of the object.
428
+ - parameter key: Key against which the value is stored.
429
+ - returns: A single line declaration of the variable which is an array of object.
430
+ */
431
+ internal func descriptionForObjectArray( variableName: String , key: String ) -> String {
432
+ return " \t \t if \( variableName) ?.count > 0 { \n \t \t \t var temp: [AnyObject] = [] \n \t \t \t for item in \( variableName) ! { \n \t \t \t \t temp.append(item.dictionaryRepresentation()) \n \t \t \t } \n \t \t \t dictionary.updateValue(temp, forKey: \( key) ) \n \t \t } "
433
+ }
434
+
435
+ /**
436
+ Description for an Object kind of a primitive variable. if {variableName}?.count > 0 { dictionary.updateValue({variableName}!, forKey: {key})
437
+ - parameter variableName: Variable name.
438
+ - parameter key: Key against which the value is stored.
439
+ - returns: A single line declaration of the variable which is an array of primitive kind.
440
+ */
441
+ internal func descriptionForPrimitiveVariableArray( variableName: String , key: String ) -> String {
442
+ return " \t \t if \( variableName) ?.count > 0 { \n \t \t \t dictionary.updateValue( \( variableName) !, forKey: \( key) ) \n \t \t } "
443
+ }
444
+
445
+ /**
446
+ Description for an Object kind of AnyObject. if {variableName}?.count > 0 { dictionary.updateValue({variableName}!.dictionaryRepresentation(), forKey: {key})
447
+ - parameter variableName: Variable name.
448
+ - parameter key: Key against which the value is stored.
449
+ - returns: A single line declaration of the variable which is an array of primitive kind.
450
+ */
451
+ internal func descriptionForObjectVariableArray( variableName: String , key: String ) -> String {
452
+ return " \t \t if \( variableName) != nil { \n \t \t \t dictionary.updateValue( \( variableName) !.dictionaryRepresentation(), forKey: \( key) ) \n \t \t } "
453
+ }
454
+
394
455
/**
395
456
Generates the string for today's date.
396
457
@@ -455,5 +516,25 @@ public class ModelGenerator {
455
516
456
517
return type
457
518
}
519
+
520
+
521
+ /**
522
+ Cross checks the list of internal variables against the current variables and repalces them based on the mapping.
523
+
524
+ - parameter currentName: Current name of the variable.
525
+
526
+ - returns: New name for the variable.
527
+ */
528
+ internal func replaceInternalKeywordsForVariableName( currentName: String ) -> String {
529
+
530
+ let currentReservedName = [ " id " : " internalIdentifier " , " description " : " descriptionValue " ]
531
+ for (key, value) in currentReservedName {
532
+ if key == currentName {
533
+ return value
534
+ }
535
+ }
536
+ return currentName
537
+
538
+ }
458
539
459
540
}
0 commit comments