Skip to content

Commit 6831576

Browse files
committed
Added basic code generation
1 parent 4049dbc commit 6831576

File tree

6 files changed

+1556
-4
lines changed

6 files changed

+1556
-4
lines changed

SwiftyJSONAccelerator.xcodeproj/project.pbxproj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
9341A14A1BD6E5FE0048CE2C /* SJModelGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9341A1491BD6E5FE0048CE2C /* SJModelGenerator.swift */; settings = {ASSET_TAGS = (); }; };
11+
9341A14C1BD6E6300048CE2C /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9341A14B1BD6E6300048CE2C /* SwiftyJSON.swift */; settings = {ASSET_TAGS = (); }; };
12+
9341A14F1BD6E7290048CE2C /* BaseTemplate.txt in Resources */ = {isa = PBXBuildFile; fileRef = 9341A14E1BD6E7290048CE2C /* BaseTemplate.txt */; settings = {ASSET_TAGS = (); }; };
13+
9341A1511BD6E7A60048CE2C /* TestModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9341A1501BD6E7A60048CE2C /* TestModel.swift */; settings = {ASSET_TAGS = (); }; };
14+
9341A1521BD6E9EC0048CE2C /* SwiftyJSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9341A14B1BD6E6300048CE2C /* SwiftyJSON.swift */; settings = {ASSET_TAGS = (); }; };
1015
9361FD141BD18558001ED533 /* LineNumberRulerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9361FD131BD18558001ED533 /* LineNumberRulerView.swift */; settings = {ASSET_TAGS = (); }; };
1116
9362F8661BD07DBA0001372E /* JSONHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9362F8651BD07DBA0001372E /* JSONHelperTests.swift */; settings = {ASSET_TAGS = (); }; };
1217
93CDE7721BD1A00B00B5B234 /* SJTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93CDE7711BD1A00B00B5B234 /* SJTextView.swift */; settings = {ASSET_TAGS = (); }; };
@@ -29,6 +34,10 @@
2934
/* End PBXContainerItemProxy section */
3035

3136
/* Begin PBXFileReference section */
37+
9341A1491BD6E5FE0048CE2C /* SJModelGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SJModelGenerator.swift; sourceTree = "<group>"; };
38+
9341A14B1BD6E6300048CE2C /* SwiftyJSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyJSON.swift; sourceTree = "<group>"; };
39+
9341A14E1BD6E7290048CE2C /* BaseTemplate.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = BaseTemplate.txt; sourceTree = "<group>"; };
40+
9341A1501BD6E7A60048CE2C /* TestModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestModel.swift; sourceTree = "<group>"; };
3241
9361FD131BD18558001ED533 /* LineNumberRulerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineNumberRulerView.swift; sourceTree = "<group>"; };
3342
9362F8651BD07DBA0001372E /* JSONHelperTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONHelperTests.swift; sourceTree = "<group>"; };
3443
93CDE7711BD1A00B00B5B234 /* SJTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SJTextView.swift; sourceTree = "<group>"; };
@@ -62,6 +71,14 @@
6271
/* End PBXFrameworksBuildPhase section */
6372

6473
/* Begin PBXGroup section */
74+
9341A14D1BD6E6EF0048CE2C /* Base Files */ = {
75+
isa = PBXGroup;
76+
children = (
77+
9341A14E1BD6E7290048CE2C /* BaseTemplate.txt */,
78+
);
79+
path = "Base Files";
80+
sourceTree = "<group>";
81+
};
6582
93F174361BD0707D007E7DFC = {
6683
isa = PBXGroup;
6784
children = (
@@ -83,6 +100,7 @@
83100
93F174411BD0707D007E7DFC /* SwiftyJSONAccelerator */ = {
84101
isa = PBXGroup;
85102
children = (
103+
9341A14D1BD6E6EF0048CE2C /* Base Files */,
86104
93F174421BD0707D007E7DFC /* AppDelegate.swift */,
87105
93F174441BD0707D007E7DFC /* SJEditorViewController.swift */,
88106
9361FD131BD18558001ED533 /* LineNumberRulerView.swift */,
@@ -91,6 +109,8 @@
91109
93F1744B1BD0707D007E7DFC /* Info.plist */,
92110
93F1745F1BD070AA007E7DFC /* JSONHelper.swift */,
93111
93CDE7711BD1A00B00B5B234 /* SJTextView.swift */,
112+
9341A1491BD6E5FE0048CE2C /* SJModelGenerator.swift */,
113+
9341A14B1BD6E6300048CE2C /* SwiftyJSON.swift */,
94114
);
95115
path = SwiftyJSONAccelerator;
96116
sourceTree = "<group>";
@@ -101,6 +121,7 @@
101121
93F174541BD0707D007E7DFC /* SwiftyJSONAcceleratorTests.swift */,
102122
93F174561BD0707D007E7DFC /* Info.plist */,
103123
9362F8651BD07DBA0001372E /* JSONHelperTests.swift */,
124+
9341A1501BD6E7A60048CE2C /* TestModel.swift */,
104125
);
105126
path = SwiftyJSONAcceleratorTests;
106127
sourceTree = "<group>";
@@ -186,6 +207,7 @@
186207
buildActionMask = 2147483647;
187208
files = (
188209
93F174471BD0707D007E7DFC /* Assets.xcassets in Resources */,
210+
9341A14F1BD6E7290048CE2C /* BaseTemplate.txt in Resources */,
189211
93F1744A1BD0707D007E7DFC /* Main.storyboard in Resources */,
190212
);
191213
runOnlyForDeploymentPostprocessing = 0;
@@ -206,8 +228,10 @@
206228
files = (
207229
93CDE7721BD1A00B00B5B234 /* SJTextView.swift in Sources */,
208230
93F174601BD070AA007E7DFC /* JSONHelper.swift in Sources */,
231+
9341A14A1BD6E5FE0048CE2C /* SJModelGenerator.swift in Sources */,
209232
93F174451BD0707D007E7DFC /* SJEditorViewController.swift in Sources */,
210233
9361FD141BD18558001ED533 /* LineNumberRulerView.swift in Sources */,
234+
9341A14C1BD6E6300048CE2C /* SwiftyJSON.swift in Sources */,
211235
93F174431BD0707D007E7DFC /* AppDelegate.swift in Sources */,
212236
);
213237
runOnlyForDeploymentPostprocessing = 0;
@@ -216,8 +240,10 @@
216240
isa = PBXSourcesBuildPhase;
217241
buildActionMask = 2147483647;
218242
files = (
243+
9341A1511BD6E7A60048CE2C /* TestModel.swift in Sources */,
219244
93F174551BD0707D007E7DFC /* SwiftyJSONAcceleratorTests.swift in Sources */,
220245
9362F8661BD07DBA0001372E /* JSONHelperTests.swift in Sources */,
246+
9341A1521BD6E9EC0048CE2C /* SwiftyJSON.swift in Sources */,
221247
);
222248
runOnlyForDeploymentPostprocessing = 0;
223249
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// {OBJECT_NAME}.swift
3+
//
4+
// Created by __NAME__ on {DATE}
5+
// Copyright (c) {COMPANY_NAME}. All rights reserved.
6+
//
7+
8+
import Foundation
9+
10+
public {OBJECT_KIND} {OBJECT_NAME}: Serializable {
11+
12+
{
13+
14+
STRING_CONSTANT_BLOCK}
15+
16+
17+
// MARK: Declaration for string constants to be used to decode and also serialize.
18+
{STRING_CONSTANT_BLOCK}
19+
20+
// MARK: Properties
21+
{PROPERTIES}
22+
23+
/**
24+
Initates the {OBJECT_KIND} based on the object
25+
26+
- parameter object: The object of either Dictionary or Array kind that was passed.
27+
28+
- returns: An initalized instance of the {OBJECT_KIND}.
29+
*/
30+
convenience init(object: AnyObject) {
31+
self.init(json: JSON(object))
32+
}
33+
34+
/**
35+
Initates the {OBJECT_KIND} based on the JSON that was passed.
36+
37+
- parameter json: JSON object from SwiftyJSON.
38+
39+
- returns: An initalized instance of the {OBJECT_KIND}.
40+
*/
41+
init(json: JSON) {
42+
43+
}
44+
45+
@end

SwiftyJSONAccelerator/SJEditorViewController.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,23 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
3333
}
3434
}
3535
// MARK: Actions
36-
@IBAction func format(sender: AnyObject?) {
36+
@IBAction func format(sender: AnyObject?) {
37+
validateAndFormat(true)
38+
}
39+
40+
func validateAndFormat(pretty: Bool) {
3741

3842
if textView?.string?.characters.count == 0 {
3943
return
4044
}
4145

4246
if let (valid, error): (Bool, NSError?) = JSONHelper.isStringValidJSON(textView?.string) {
43-
if(valid){
44-
textView?.string = JSONHelper.prettyJSON(textView?.string)!
47+
if valid {
48+
if pretty {
49+
textView?.string = JSONHelper.prettyJSON(textView?.string)!
50+
}
4551
correctJSONMessage()
52+
ModelGenerator.generate(JSONHelper.convertToObject(textView?.string).1!, className: "Test", suffix: "EF")
4653
} else if error != nil {
4754
handleError(error)
4855
}
@@ -145,7 +152,7 @@ class SJEditorViewController: NSViewController, NSTextViewDelegate {
145152

146153
//MARK: TextView Delegate
147154
func textDidChange(notification: NSNotification) {
148-
format(textView)
155+
validateAndFormat(false)
149156
}
150157

151158
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// SJModelGenerator.swift
3+
// SwiftyJSONAccelerator
4+
//
5+
// Created by Karthik on 20/10/2015.
6+
// Copyright © 2015 Karthikeya Udupa K M. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
//
12+
// ModelGenerator.swift
13+
// swiftin
14+
//
15+
// Created by Philip Woods on 6/11/14.
16+
// Copyright (c) 2014 pvwoods. All rights reserved.
17+
//
18+
19+
import Foundation
20+
21+
import Cocoa
22+
23+
24+
public class ModelGenerator {
25+
26+
class func buildClassName(className: String, suffix: String) -> String {
27+
var classNameCleaned = variableNameBuilder(className)
28+
classNameCleaned.replaceRange(classNameCleaned.startIndex...classNameCleaned.startIndex, with: String(classNameCleaned[classNameCleaned.startIndex]).uppercaseString)
29+
return suffix.stringByAppendingString(classNameCleaned)
30+
}
31+
32+
33+
class func variableNameBuilder(variableName: String) -> String {
34+
var variableName = variableName.stringByReplacingOccurrencesOfString("_", withString: " ")
35+
variableName = variableName.capitalizedString
36+
variableName = variableName.stringByReplacingOccurrencesOfString(" ", withString: "")
37+
variableName.replaceRange(variableName.startIndex...variableName.startIndex, with: String(variableName[variableName.startIndex]).lowercaseString)
38+
return variableName
39+
40+
}
41+
class func variableNameKeyBuilder(className: String, var variableName: String) -> String {
42+
variableName.replaceRange(variableName.startIndex...variableName.startIndex, with: String(variableName[variableName.startIndex]).uppercaseString)
43+
return "k\(className)\(variableName)Key"
44+
}
45+
46+
class func checkType(value: JSON) -> String {
47+
var js : JSON = value as JSON
48+
var type: String = ""
49+
if let _ = js.string {
50+
type = "String"
51+
} else if let _ = js.number {
52+
type = "NSNumber"
53+
} else if let _ = js.bool {
54+
type = "Bool"
55+
} else if let _ = js.array {
56+
type = "[]"
57+
} else {
58+
type = "AnyObject"
59+
}
60+
return type
61+
}
62+
63+
public class func generate(anyObject: AnyObject, className: String, suffix: String?) {
64+
65+
var declarations: String = ""
66+
var stringConstants: String = ""
67+
68+
let parsedJSONObject = JSON(anyObject)
69+
let className = buildClassName(className, suffix: suffix!)
70+
if let object = parsedJSONObject.dictionary {
71+
for (key, subJson) in object {
72+
let variableName: String = variableNameBuilder(key)
73+
let stringConstantName: String = variableNameKeyBuilder(className, variableName: variableName)
74+
stringConstants = stringConstants.stringByAppendingFormat("\tinternal let %@: String = \"%@\"\n", stringConstantName, key)
75+
declarations = declarations.stringByAppendingFormat("\tvar %@: %@?\n", variableName, checkType(subJson))
76+
}
77+
78+
print(stringConstants)
79+
print(declarations)
80+
}
81+
}
82+
83+
}

0 commit comments

Comments
 (0)