Skip to content

Commit 89de598

Browse files
committed
Added comment to the base template about description
1 parent 54c5aa0 commit 89de598

File tree

3 files changed

+102
-9
lines changed

3 files changed

+102
-9
lines changed

SwiftyJSONAccelerator/Base Files/BaseTemplate.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ public {OBJECT_KIND} {OBJECT_NAME}: NSObject, NSCoding {
3535
- returns: An initalized instance of the class.
3636
*/
3737
public init(json: JSON) {
38-
3938
{INITALIZER}
39+
}
40+
41+
/**
42+
Generates description of the object in the form of a NSDictionary.
43+
44+
- returns: A Key value pair containing all valid values in the object.
45+
*/
46+
public func dictionaryRepresentation() -> [String : AnyObject ] {
47+
48+
var dictionary: [String : AnyObject ] = [ : ]
49+
{DESC}
4050

51+
return dictionary
4152
}
4253

4354
// MARK: NSCoding Protocol

SwiftyJSONAccelerator/SJEditorViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
9898

9999
// Checks for validity of the content, else can cause crashes.
100100
if object != nil {
101+
101102
let generator: ModelGenerator = ModelGenerator.init(baseContent: JSON(object!), prefix: prefixClassTextField?.stringValue, baseClassName: (baseClassTextField?.stringValue)!, authorName: authorNameTextField?.stringValue, companyName: companyNameTextField?.stringValue, type: ModelType.kClassType, filePath: filePath!)
102103
generator.generate()
103104
} else {

SwiftyJSONAccelerator/SJModelGenerator.swift

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ struct VariableType {
3131
* - kStructType: Struct type
3232
*/
3333
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"
3636
}
3737

3838
/// 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 {
106106
var initalizers: String = ""
107107
var encoders: String = ""
108108
var decoders: String = ""
109+
var description: String = ""
109110

110111
/// Create a classname in Sentence case and removing unwanted stuff.
111112
let className = buildClassName(className, prefix: self.prefix!, isSubModule: isSubModule)
@@ -138,12 +139,13 @@ public class ModelGenerator {
138139
declarations = declarations.stringByAppendingFormat(variableDeclarationBuilder(variableName, type: "[\(subClassName)]"))
139140
initalizers = initalizers.stringByAppendingFormat("%@\n", initalizerForObjectArray(variableName, className: subClassName, key: stringConstantName))
140141
decoders = decoders.stringByAppendingFormat("%@\n", decoderForVariable(variableName,key: stringConstantName, type: "[\(subClassName)]"))
141-
142+
description = description.stringByAppendingFormat("%@\n", descriptionForObjectArray(variableName, key: stringConstantName))
142143
} else {
143144
// If it is anything other than an object, it should be a primitive type hence deal with it accordingly.
144145
declarations = declarations.stringByAppendingFormat(variableDeclarationBuilder(variableName, type: "[\(subClassType)]"))
145146
initalizers = initalizers.stringByAppendingFormat("%@\n", initalizerForPrimitiveVariableArray(variableName, key: stringConstantName, type: subClassType))
146147
decoders = decoders.stringByAppendingFormat("%@\n", decoderForVariable(variableName,key: stringConstantName, type: "[\(subClassType)]"))
148+
description = description.stringByAppendingFormat("%@\n", descriptionForPrimitiveVariableArray(variableName, key: stringConstantName))
147149
}
148150
// TODO: We should also consider a third case where the type is an [AnyObject] to achive complete redundancy handling.
149151

@@ -162,13 +164,14 @@ public class ModelGenerator {
162164
declarations = declarations.stringByAppendingFormat(variableDeclarationBuilder(variableName, type: subClassName))
163165
initalizers = initalizers.stringByAppendingFormat("%@\n", initalizerForObject(variableName, className: subClassName, key: stringConstantName))
164166
decoders = decoders.stringByAppendingFormat("%@\n", decoderForVariable(variableName,key: stringConstantName, type: subClassName))
167+
description = description.stringByAppendingFormat("%@\n", descriptionForObjectVariableArray(variableName, key: stringConstantName))
165168

166169
} else {
167170
// If it is a primitive then simply create initalizers, declarations and decoders.
168171
declarations = declarations.stringByAppendingFormat(variableDeclarationBuilder(variableName, type: variableType))
169172
initalizers = initalizers.stringByAppendingFormat("%@\n", initalizerForVariable(variableName, type: variableType, key: stringConstantName))
170173
decoders = decoders.stringByAppendingFormat("%@\n", decoderForVariable(variableName,key: stringConstantName, type: variableType))
171-
174+
description = description.stringByAppendingFormat("%@\n", descriptionForVariable(variableName, key: stringConstantName))
172175
}
173176

174177
}
@@ -186,6 +189,7 @@ public class ModelGenerator {
186189
content = content.stringByReplacingOccurrencesOfString("{INITALIZER}", withString: initalizers)
187190
content = content.stringByReplacingOccurrencesOfString("{ENCODERS}", withString: encoders)
188191
content = content.stringByReplacingOccurrencesOfString("{DECODERS}", withString: decoders)
192+
content = content.stringByReplacingOccurrencesOfString("{DESC}", withString: description)
189193

190194
if authorName != nil {
191195
content = content.stringByReplacingOccurrencesOfString("__NAME__", withString: authorName!)
@@ -229,8 +233,7 @@ public class ModelGenerator {
229233
- returns: A generated string representation of the variable name.
230234
*/
231235
internal func variableNameBuilder(variableName: String) -> String {
232-
233-
let variableName = variableName.stringByReplacingOccurrencesOfString("_", withString: " ")
236+
let variableName = replaceInternalKeywordsForVariableName(variableName).stringByReplacingOccurrencesOfString("_", withString: " ")
234237
var finalVariableName: String = ""
235238
for (index, element) in variableName.componentsSeparatedByString(" ").enumerate() {
236239
var component: String = element
@@ -356,10 +359,10 @@ public class ModelGenerator {
356359
}
357360

358361
/**
359-
Encoder for a variable.
362+
Initalizer for an primitive kind of elements array variable.
360363
- parameter variableName: Variable name.
361364
- 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.
363366
*/
364367
internal func initalizerForPrimitiveVariableArray(variableName: String, key: String, var type: String) -> String {
365368
type = typeToSwiftType(type)
@@ -391,6 +394,64 @@ public class ModelGenerator {
391394
return "\t\tself.\(variableName) = aDecoder.decodeObjectForKey(\(key)) as? \(type)"
392395
}
393396

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\tif 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\tif \(variableName) != nil {\n\t\t\tdictionary.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\tif \(variableName)?.count > 0 {\n\t\t\tvar temp: [AnyObject] = []\n\t\t\tfor item in \(variableName)! {\n\t\t\t\ttemp.append(item.dictionaryRepresentation())\n\t\t\t}\n\t\t\tdictionary.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\tif \(variableName)?.count > 0 {\n\t\t\tdictionary.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\tif \(variableName) != nil {\n\t\t\tdictionary.updateValue(\(variableName)!.dictionaryRepresentation(), forKey: \(key))\n\t\t}"
453+
}
454+
394455
/**
395456
Generates the string for today's date.
396457

@@ -455,5 +516,25 @@ public class ModelGenerator {
455516

456517
return type
457518
}
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+
}
458539

459540
}

0 commit comments

Comments
 (0)