Skip to content

Commit aa0587c

Browse files
committed
Added Marshal Generator
1 parent 92384b9 commit aa0587c

File tree

11 files changed

+150
-20
lines changed

11 files changed

+150
-20
lines changed

SwiftyJSONAccelerator.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
930462B51D31131800FE61A6 /* PropertyComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930462B11D310FF200FE61A6 /* PropertyComponent.swift */; };
1616
930462B61D31133B00FE61A6 /* FileGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930462AC1D3106A400FE61A6 /* FileGenerator.swift */; };
1717
930462B71D31134900FE61A6 /* ModelComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930462AF1D310F6900FE61A6 /* ModelComponent.swift */; };
18+
9327F2881DF4BCA70002BE40 /* MarshalModelFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9327F2871DF4BCA70002BE40 /* MarshalModelFile.swift */; };
19+
9327F28A1DF4BD540002BE40 /* MarshalTemplate.txt in Resources */ = {isa = PBXBuildFile; fileRef = 9327F2891DF4BD540002BE40 /* MarshalTemplate.txt */; };
1820
9341A14A1BD6E5FE0048CE2C /* ModelGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9341A1491BD6E5FE0048CE2C /* ModelGenerator.swift */; };
1921
9341A14F1BD6E7290048CE2C /* BaseTemplate.txt in Resources */ = {isa = PBXBuildFile; fileRef = 9341A14E1BD6E7290048CE2C /* BaseTemplate.txt */; };
2022
9361FD141BD18558001ED533 /* LineNumberRulerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9361FD131BD18558001ED533 /* LineNumberRulerView.swift */; };
@@ -79,6 +81,8 @@
7981
930462AC1D3106A400FE61A6 /* FileGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileGenerator.swift; sourceTree = "<group>"; };
8082
930462AF1D310F6900FE61A6 /* ModelComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModelComponent.swift; sourceTree = "<group>"; };
8183
930462B11D310FF200FE61A6 /* PropertyComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PropertyComponent.swift; sourceTree = "<group>"; };
84+
9327F2871DF4BCA70002BE40 /* MarshalModelFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MarshalModelFile.swift; sourceTree = "<group>"; };
85+
9327F2891DF4BD540002BE40 /* MarshalTemplate.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = MarshalTemplate.txt; sourceTree = "<group>"; };
8286
9341A1491BD6E5FE0048CE2C /* ModelGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ModelGenerator.swift; sourceTree = "<group>"; };
8387
9341A14E1BD6E7290048CE2C /* BaseTemplate.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = BaseTemplate.txt; sourceTree = "<group>"; };
8488
9361FD131BD18558001ED533 /* LineNumberRulerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineNumberRulerView.swift; sourceTree = "<group>"; };
@@ -211,6 +215,7 @@
211215
children = (
212216
93F1593F1CFF96E700175636 /* ObjectMapperModelFile.swift */,
213217
93F1593D1CFF969900175636 /* SwiftyJSONModelFile.swift */,
218+
9327F2871DF4BCA70002BE40 /* MarshalModelFile.swift */,
214219
);
215220
path = "Library-Extensions";
216221
sourceTree = "<group>";
@@ -291,6 +296,7 @@
291296
children = (
292297
C667011C1BDCAF23009BA254 /* ObjectMapperTemplate.txt */,
293298
C667011A1BDCABCE009BA254 /* SwiftyJSONTemplate.txt */,
299+
9327F2891DF4BD540002BE40 /* MarshalTemplate.txt */,
294300
);
295301
path = "Library-Extension-Templates";
296302
sourceTree = "<group>";
@@ -369,7 +375,7 @@
369375
93F174371BD0707D007E7DFC /* Project object */ = {
370376
isa = PBXProject;
371377
attributes = {
372-
LastUpgradeCheck = 0800;
378+
LastUpgradeCheck = 0810;
373379
ORGANIZATIONNAME = "Karthikeya Udupa K M";
374380
TargetAttributes = {
375381
93F1743E1BD0707D007E7DFC = {
@@ -414,6 +420,7 @@
414420
C667011D1BDCAF23009BA254 /* ObjectMapperTemplate.txt in Resources */,
415421
9341A14F1BD6E7290048CE2C /* BaseTemplate.txt in Resources */,
416422
93F1744A1BD0707D007E7DFC /* Main.storyboard in Resources */,
423+
9327F28A1DF4BD540002BE40 /* MarshalTemplate.txt in Resources */,
417424
);
418425
runOnlyForDeploymentPostprocessing = 0;
419426
};
@@ -549,6 +556,7 @@
549556
9341A14A1BD6E5FE0048CE2C /* ModelGenerator.swift in Sources */,
550557
930462B21D310FF200FE61A6 /* PropertyComponent.swift in Sources */,
551558
93F1593A1CFF853C00175636 /* NameGenerator.swift in Sources */,
559+
9327F2881DF4BCA70002BE40 /* MarshalModelFile.swift in Sources */,
552560
930462AD1D3106A400FE61A6 /* FileGenerator.swift in Sources */,
553561
93F174451BD0707D007E7DFC /* SJEditorViewController.swift in Sources */,
554562
93F1593E1CFF969900175636 /* SwiftyJSONModelFile.swift in Sources */,

SwiftyJSONAccelerator.xcodeproj/xcshareddata/xcschemes/SwiftyJSONAccelerator.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0800"
3+
LastUpgradeVersion = "0810"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

SwiftyJSONAccelerator/Constants.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ enum ConstructType: String {
4646

4747
- SwiftyJSON: SwiftyJSON - https://github.com/SwiftyJSON/SwiftyJSON
4848
- ObjectMapper: ObjectMapper - https://github.com/Hearst-DD/ObjectMapper
49+
- Marshal: Marshal - https://github.com/utahiosmac/Marshal
4950
*/
5051
enum JSONMappingLibrary: String {
5152
case SwiftyJSON
5253
case ObjectMapper
54+
case Marshal
5355
}
5456

5557
/**

SwiftyJSONAccelerator/Generators/FileGenerator.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ struct FileGenerator {
3737
content = content.replacingOccurrences(of: "{DATE}", with: todayDateString())
3838
content = content.replacingOccurrences(of: "{OBJECT_KIND}", with: modelFile.type.rawValue)
3939
content = content.replacingOccurrences(of: "{JSON_PARSER_LIBRARY_BODY}", with: loadFileWith(modelFile.mainBodyFileName()))
40+
41+
if modelFile.type == .ClassType {
42+
content = content.replacingOccurrences(of: "{REQUIRED}", with: " required ")
43+
} else {
44+
content = content.replacingOccurrences(of: "{REQUIRED}", with: " ")
45+
}
4046
if let authorName = configuration.authorName {
4147
content = content.replacingOccurrences(of: "__NAME__", with: authorName)
4248
}
@@ -68,7 +74,7 @@ struct FileGenerator {
6874

6975
content = content.replacingOccurrences(of: "{STRING_CONSTANT}", with: stringConstants)
7076
content = content.replacingOccurrences(of: "{DECLARATION}", with: declarations)
71-
content = content.replacingOccurrences(of: "{INITALIZER}", with: initialisers)
77+
content = content.replacingOccurrences(of: "{INITIALIZER}", with: initialisers)
7278
content = content.replacingOccurrences(of: "{DESCRIPTION}", with: description)
7379

7480
if configuration.constructType == .StructType {

SwiftyJSONAccelerator/Generators/ModelGenerator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public struct ModelGenerator {
145145
return ObjectMapperModelFile()
146146
case .SwiftyJSON:
147147
return SwiftyJSONModelFile()
148+
case .Marshal:
149+
return MarshalModelFile()
148150
}
149151
}
150152

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// MarshalModelFile.swift
3+
// SwiftyJSONAccelerator
4+
//
5+
// Created by Karthik on 04/12/2016.
6+
// Copyright © 2016 Karthikeya Udupa K M. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct MarshalModelFile: ModelFile, DefaultModelFileComponent {
12+
13+
/// Filename for the model.
14+
var fileName: String
15+
var type: ConstructType
16+
var component: ModelComponent
17+
18+
init() {
19+
self.fileName = ""
20+
type = ConstructType.StructType
21+
component = ModelComponent.init()
22+
}
23+
24+
mutating func setInfo(_ fileName: String, _ configuration: ModelGenerationConfiguration) {
25+
self.fileName = fileName
26+
type = configuration.constructType
27+
}
28+
29+
func moduleName() -> String {
30+
return "Marshal"
31+
}
32+
33+
func baseElementName() -> String? {
34+
return "Unmarshaling"
35+
}
36+
37+
func mainBodyFileName() -> String {
38+
return "MarshalTemplate"
39+
}
40+
41+
mutating func generateAndAddComponentsFor(_ property: PropertyComponent) {
42+
switch property.propertyType {
43+
44+
case .ValueType:
45+
component.declarations.append(genVariableDeclaration(property.name, property.type, false))
46+
component.description.append(genDescriptionForPrimitive(property.name, property.type, property.constantName))
47+
component.decoders.append(genDecoder(property.name, property.type, property.constantName, false))
48+
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
49+
generateCommonComponentsFor(property)
50+
case .ValueTypeArray:
51+
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
52+
component.declarations.append(genVariableDeclaration(property.name, property.type, true))
53+
component.decoders.append(genDecoder(property.name, property.type, property.constantName, true))
54+
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
55+
generateCommonComponentsFor(property)
56+
case .ObjectType:
57+
component.description.append(genDescriptionForObject(property.name, property.constantName))
58+
component.declarations.append(genVariableDeclaration(property.name, property.type, false))
59+
component.decoders.append(genDecoder(property.name, property.type, property.constantName, false))
60+
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
61+
generateCommonComponentsFor(property)
62+
case .ObjectTypeArray:
63+
component.declarations.append(genVariableDeclaration(property.name, property.type, true))
64+
component.description.append(genDescriptionForObjectArray(property.name, property.constantName))
65+
component.decoders.append(genDecoder(property.name, property.type, property.constantName, true))
66+
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
67+
generateCommonComponentsFor(property)
68+
69+
case .EmptyArray:
70+
component.declarations.append(genVariableDeclaration(property.name, "Any", true))
71+
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
72+
component.decoders.append(genDecoder(property.name, "Any", property.constantName, true))
73+
component.encoders.append(genEncoder(property.name, "Any", property.constantName))
74+
generateCommonComponentsFor(property)
75+
case .NullType: break
76+
// Currently we do not deal with null values.
77+
78+
}
79+
}
80+
81+
fileprivate mutating func generateCommonComponentsFor(_ property: PropertyComponent) {
82+
component.stringConstants.append(genStringConstant(property.constantName, property.key))
83+
component.initialisers.append(genInitializerForVariable(property.name, property.constantName))
84+
}
85+
86+
// MARK: - Customised methods for ObjectMapper
87+
// MARK: - Initialisers
88+
func genInitializerForVariable(_ name: String, _ constantName: String) -> String {
89+
return "\(name) = try? object.value(for: \(constantName))"
90+
}
91+
92+
}

SwiftyJSONAccelerator/Storyboard/Base.lproj/Main.storyboard

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="15G1004" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="XfG-lQ-9wD">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="XfG-lQ-9wD">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11201"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11542"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
67
</dependencies>
78
<scenes>
89
<!--Application-->
@@ -751,13 +752,14 @@
751752
</box>
752753
<popUpButton verticalHuggingPriority="750" springLoaded="YES" translatesAutoresizingMaskIntoConstraints="NO" id="r5l-Lv-rfg">
753754
<rect key="frame" x="10" y="35" width="348" height="26"/>
754-
<popUpButtonCell key="cell" type="push" title="SwiftyJSON" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" pullsDown="YES" arrowPosition="arrowAtCenter" autoenablesItems="NO" altersStateOfSelectedItem="NO" selectedItem="0QP-u9-O6V" id="l4o-yX-zdG">
755+
<popUpButtonCell key="cell" type="push" title="SwiftyJSON" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" pullsDown="YES" arrowPosition="arrowAtCenter" autoenablesItems="NO" altersStateOfSelectedItem="NO" id="l4o-yX-zdG">
755756
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
756-
<font key="font" metaFont="system"/>
757+
<font key="font" metaFont="menu"/>
757758
<menu key="menu" autoenablesItems="NO" id="QJi-PT-im1">
758759
<items>
759760
<menuItem title="SwiftyJSON" state="on" hidden="YES" id="0QP-u9-O6V"/>
760761
<menuItem title="ObjectMapper" id="FPc-o9-hXy"/>
762+
<menuItem title="Marshal" id="7Q8-u2-Yfm"/>
761763
</items>
762764
</menu>
763765
</popUpButtonCell>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// MARK: Marshal Initializers
2+
3+
/**
4+
Map a JSON object to this class using ObjectMapper
5+
- parameter map: A mapping from ObjectMapper
6+
*/
7+
public{REQUIRED}init(object: MarshaledObject) {
8+
{INITIALIZER}
9+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// MARK: ObjectMapper Initalizers
1+
// MARK: ObjectMapper Initializers
22
/**
33
Map a JSON object to this class using ObjectMapper
44
- parameter map: A mapping from ObjectMapper
55
*/
6-
required public init?(_ map: Map){
6+
public{REQUIRED}init?(_ map: Map){
77

88
}
99

@@ -12,5 +12,5 @@
1212
- parameter map: A mapping from ObjectMapper
1313
*/
1414
public func mapping(map: Map) {
15-
{INITALIZER}
15+
{INITIALIZER}
1616
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// MARK: SwiftyJSON Initalizers
1+
// MARK: SwiftyJSON Initializers
22
/**
3-
Initates the instance based on the object
3+
Initiates the instance based on the object
44
- parameter object: The object of either Dictionary or Array kind that was passed.
5-
- returns: An initalized instance of the class.
5+
- returns: An initialized instance of the class.
66
*/
7-
convenience public init(object: Any) {
7+
public convenience init(object: Any) {
88
self.init(json: JSON(object))
99
}
1010

1111
/**
12-
Initates the instance based on the JSON that was passed.
12+
Initiates the instance based on the JSON that was passed.
1313
- parameter json: JSON object from SwiftyJSON.
14-
- returns: An initalized instance of the class.
14+
- returns: An initialized instance of the class.
1515
*/
16-
public init(json: JSON) {
17-
{INITALIZER}
16+
public{REQUIRED}init(json: JSON) {
17+
{INITIALIZER}
1818
}

0 commit comments

Comments
 (0)