Skip to content

Commit

Permalink
Merge pull request #50 from insanoid/releases/v1.3.0
Browse files Browse the repository at this point in the history
Releases/v1.3.0
  • Loading branch information
Karthikeya Udupa KM authored Dec 7, 2016
2 parents 94b2592 + 9ec48cc commit 682a8ea
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 141 deletions.
42 changes: 26 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
![Logo](https://raw.githubusercontent.com/insanoid/SwiftyJSONAccelerator/master/SwiftyJSONAccelerator/Support/Assets.xcassets/AppIcon.appiconset/Icon_32x32%402x.png)

# SwiftyJSONAccelerator
# SwiftyJSONAccelerator (Model file Generator For Swift 3)

[![Build
Status](https://travis-ci.org/insanoid/SwiftyJSONAccelerator.svg?branch=master)](https://travis-ci.org/insanoid/SwiftyJSONAccelerator) [![codecov](https://codecov.io/gh/insanoid/SwiftyJSONAccelerator/branch/master/graph/badge.svg)](https://codecov.io/gh/insanoid/SwiftyJSONAccelerator)

**Version v1.2.0 Released!**

- Now supports [Marshal](https://github.com/utahiosmac/Marshal)! One of the fastest JSONSerialisation class out there! [(Read more)](https://github.com/bwhiteley/JSONShootout)
- Set `class` as `final`.
- `init` marked as `required` by default for `class`.

**Version v1.1.0 Released!**

- Now generates the correct option `struct` and `class` based on what was selected.
**Version v1.3.0 Released!**

**Version v1.0.0 Released!**
- Serialisation keys moved into a struct for clarity.
- Minor fixes for Marshal and ObjectMapper.
- Generated comments now updated to the new Swift 3 Markup.

- Now generates **Swift 3** and the software itself is upgraded to Swift 3.
- Unit tests and complete code coverage for file generation module.
- Modular code to make adding support for other JSON mapping libraries simple.
- Project upgraded with SwiftLint, Travis, CocoaPods etc.
[Previous Release Notes](#previous-releases)

## Download/Installing

- **Option 1:** Download the repo, install pods and run the project!
- **Option 2:** [Download the .app(v1.2.0)](https://github.com/insanoid/SwiftyJSONAccelerator/releases/download/v1.2.0/SwiftyJSONAccelerator.zip)
- **Option 2:** [Download the .app(v1.3.0)](https://github.com/insanoid/SwiftyJSONAccelerator/releases/download/v1.3.0/SwiftyJSONAccelerator.zip)

A swift model generator like the Objective-C [JSONAccelerator](http://nerdery.com/json-accelerator). Formats and generates models for the given JSON and also breaks them into files making it easy to manage and share between several models.

Expand Down Expand Up @@ -53,6 +44,25 @@ Currently, the pattern is very similar to its Objective-C counterpart. It genera
- Do the necessary UI changes for the dropdown.
- Add tests for your library.

## Previous Releases

**Version v1.2.0 Released!**

- Now supports [Marshal](https://github.com/utahiosmac/Marshal)! One of the fastest JSONSerialisation class out there! [(Read more)](https://github.com/bwhiteley/JSONShootout)
- Set `class` as `final`.
- `init` marked as `required` by default for `class`.

**Version v1.1.0 Released!**

- Now generates the correct option `struct` and `class` based on what was selected.

**Version v1.0.0 Released!**

- Now generates **Swift 3** and the software itself is upgraded to Swift 3.
- Unit tests and complete code coverage for file generation module.
- Modular code to make adding support for other JSON mapping libraries simple.
- Project upgraded with SwiftLint, Travis, CocoaPods etc.

## Swift 2?

[Download (v0.0.6)](https://github.com/insanoid/SwiftyJSONAccelerator/releases/download/v0.0.6/SwiftyJSONAccelerator.zip), the older version of the project, please keep in mind it is **no longer supported**.
Expand Down
14 changes: 8 additions & 6 deletions SwiftyJSONAccelerator/Generators/FileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct FileGenerator {
static func generateFileContentWith(_ modelFile: ModelFile, configuration: ModelGenerationConfiguration) -> String {

var content = loadFileWith("BaseTemplate")
let singleTab = " ", doubleTab = " "
content = content.replacingOccurrences(of: "{OBJECT_NAME}", with: modelFile.fileName)
content = content.replacingOccurrences(of: "{DATE}", with: todayDateString())
content = content.replacingOccurrences(of: "{OBJECT_KIND}", with: modelFile.type.rawValue)
Expand Down Expand Up @@ -73,10 +74,11 @@ struct FileGenerator {
content = content.replacingOccurrences(of: "{EXTENDED_OBJECT_COLON}", with: "")
}

let stringConstants = modelFile.component.stringConstants.map({ " " + $0 }).joined(separator: "\n")
let declarations = modelFile.component.declarations.map({ " " + $0 }).joined(separator: "\n")
let initialisers = modelFile.component.initialisers.map({ " " + $0 }).joined(separator: "\n")
let description = modelFile.component.description.map({ " " + $0 }).joined(separator: "\n")

let stringConstants = modelFile.component.stringConstants.map({doubleTab + $0 }).joined(separator: "\n")
let declarations = modelFile.component.declarations.map({ singleTab + $0 }).joined(separator: "\n")
let initialisers = modelFile.component.initialisers.map({doubleTab + $0 }).joined(separator: "\n")
let description = modelFile.component.description.map({ doubleTab + $0 }).joined(separator: "\n")

content = content.replacingOccurrences(of: "{STRING_CONSTANT}", with: stringConstants)
content = content.replacingOccurrences(of: "{DECLARATION}", with: declarations)
Expand All @@ -89,8 +91,8 @@ struct FileGenerator {

if configuration.supportNSCoding && configuration.constructType == .ClassType {
content = content.replacingOccurrences(of: "{NSCODING_SUPPORT}", with: loadFileWith("NSCodingTemplate"))
let encoders = modelFile.component.encoders.map({ " " + $0 }).joined(separator: "\n")
let decoders = modelFile.component.decoders.map({ " " + $0 }).joined(separator: "\n")
let encoders = modelFile.component.encoders.map({ doubleTab + $0 }).joined(separator: "\n")
let decoders = modelFile.component.decoders.map({ doubleTab + $0 }).joined(separator: "\n")
content = content.replacingOccurrences(of: "{DECODERS}", with: decoders)
content = content.replacingOccurrences(of: "{ENCODERS}", with: encoders)
} else {
Expand Down
4 changes: 2 additions & 2 deletions SwiftyJSONAccelerator/Generators/ModelGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public struct ModelGenerator {

if let rootObject = object.dictionary {
// A model file to store the current model.
var currentModel = self.initaliseModelFileFor(configuration.modelMappingLibrary)
var currentModel = self.initialiseModelFileFor(configuration.modelMappingLibrary)
currentModel.setInfo(className, configuration)

for (key, value) in rootObject {
Expand Down Expand Up @@ -139,7 +139,7 @@ public struct ModelGenerator {

- returns: A new model file of the required type.
*/
func initaliseModelFileFor(_ modelMappingLibrary: JSONMappingLibrary) -> ModelFile {
func initialiseModelFileFor(_ modelMappingLibrary: JSONMappingLibrary) -> ModelFile {
switch modelMappingLibrary {
case .ObjectMapper:
return ObjectMapperModelFile()
Expand Down
7 changes: 2 additions & 5 deletions SwiftyJSONAccelerator/Generators/NameGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ struct NameGenerator {
*/
static func replaceKeywords(_ currentName: String) -> String {

let keywordsWithReplacements = ["id": "internalIdentifier",
let keywordsWithReplacements = [
"description": "descriptionValue",
"_id": "internalIdentifier",
"class": "classProperty",
"struct": "structProperty",
"enum": "enumProperty",
Expand All @@ -91,8 +90,6 @@ struct NameGenerator {
- returns: The name for the key for the variable in the given class.
*/
static func variableKey(_ className: String, _ variableName: String) -> String {
var _variableName = variableName
_variableName.uppercaseFirst()
return "k\(className)\(_variableName)Key"
return "SerializationKeys.\(variableName)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ protocol DefaultModelFileComponent {
extension DefaultModelFileComponent {

func genStringConstant(_ constantName: String, _ value: String) -> String {
return "private let \(constantName): String = \"\(value)\""
//The incoming string is in the format "SeralizationKey.ConstantName" we only need the second part.
let component = constantName.components(separatedBy: ".")
return "static let \(component.last!) = \"\(value)\""
}

func genVariableDeclaration(_ name: String, _ type: String, _ isArray: Bool) -> String {
Expand All @@ -129,7 +131,7 @@ extension DefaultModelFileComponent {

func genPrimitiveVariableDeclaration(_ name: String, _ type: String) -> String {
if type == VariableType.Bool.rawValue {
return "public var \(name): \(type) = false"
return "public var \(name): \(type)? = false"
}
return "public var \(name): \(type)?"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal struct ModelComponent {
var description: [String]

/**
Initalise a blank model component structure.
Initialise a blank model component structure.
*/
init() {
declarations = []
Expand Down
6 changes: 4 additions & 2 deletions SwiftyJSONAccelerator/Support/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.2.0</string>
<string>1.3.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>8</string>
<string>9</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
9 changes: 5 additions & 4 deletions SwiftyJSONAccelerator/Templates/BaseTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import Foundation{INCLUDE_HEADER}
public{IS_FINAL}{OBJECT_KIND} {OBJECT_NAME}{EXTENDED_OBJECT_COLON}{EXTEND_FROM} {

// MARK: Declaration for string constants to be used to decode and also serialize.
private struct SerializationKeys {
{STRING_CONSTANT}
}

// MARK: Properties
{DECLARATION}

{JSON_PARSER_LIBRARY_BODY}
/**
Generates description of the object in the form of a NSDictionary.
- returns: A Key value pair containing all valid values in the object.
*/
/// Generates description of the object in the form of a NSDictionary.
///
/// - returns: A Key value pair containing all valid values in the object.
public func dictionaryRepresentation() -> [String: Any] {
var dictionary: [String: Any] = [:]
{DESCRIPTION}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// MARK: Marshal Initializers

/**
Map a JSON object to this class using ObjectMapper
- parameter map: A mapping from ObjectMapper
*/
/// Map a JSON object to this class using Marshal.
///
/// - parameter object: A mapping from ObjectMapper
public{REQUIRED}init(object: MarshaledObject) {
{INITIALIZER}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// MARK: ObjectMapper Initializers
/**
Map a JSON object to this class using ObjectMapper
- parameter map: A mapping from ObjectMapper
*/
public{REQUIRED}init?(_ map: Map){
/// Map a JSON object to this class using ObjectMapper.
///
/// - parameter map: A mapping from ObjectMapper.
public{REQUIRED}init?(map: Map){

}

/**
Map a JSON object to this class using ObjectMapper
- parameter map: A mapping from ObjectMapper
*/
/// Map a JSON object to this class using ObjectMapper.
///
/// - parameter map: A mapping from ObjectMapper.
public func mapping(map: Map) {
{INITIALIZER}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// MARK: SwiftyJSON Initializers
/**
Initiates the instance based on the object
- parameter object: The object of either Dictionary or Array kind that was passed.
- returns: An initialized instance of the class.
*/
/// Initiates the instance based on the object.
///
/// - parameter object: The object of either Dictionary or Array kind that was passed.
/// - returns: An initialized instance of the class.
public convenience init(object: Any) {
self.init(json: JSON(object))
}

/**
Initiates the instance based on the JSON that was passed.
- parameter json: JSON object from SwiftyJSON.
- returns: An initialized instance of the class.
*/
/// Initiates the instance based on the JSON that was passed.
///
/// - parameter json: JSON object from SwiftyJSON.
public{REQUIRED}init(json: JSON) {
{INITIALIZER}
}
Loading

0 comments on commit 682a8ea

Please sign in to comment.