Skip to content

Commit

Permalink
Merge pull request #91 from insanoid/v1.5.0
Browse files Browse the repository at this point in the history
V1.5.0
  • Loading branch information
insanoid authored Jul 24, 2019
2 parents fd4a6f1 + 1207b39 commit a29063a
Show file tree
Hide file tree
Showing 22 changed files with 339 additions and 281 deletions.
2 changes: 2 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ disabled_rules: # rule identifiers to exclude from running
- function_body_length
- unused_closure_parameter
- valid_docs
- large_tuple
- identifier_name
excluded: # paths to ignore during linting. Takes precedence over `included`.
- SwiftyJSONAcceleratorTests
- Pods
Expand Down
44 changes: 22 additions & 22 deletions Core/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ import Foundation
- Object: Object.
*/
enum VariableType: String {
case String = "String"
case Int = "Int"
case Float = "Float"
case Double = "Double"
case Bool = "Bool"
case Array = "[]"
case Object = "{OBJ}"
case Null = "Any"
case string = "String"
case int = "Int"
case float = "Float"
case double = "Double"
case bool = "Bool"
case array = "[]"
case object = "{OBJ}"
case null = "Any"
}

/**
Various types of construct that can be generated.

- ClassType: Model with construct type class.
- StructType: Model with construct type struct.
- classType: Model with construct type class.
- structType: Model with construct type struct.
*/
enum ConstructType: String {
case ClassType = "class"
case StructType = "struct"
case classType = "class"
case structType = "struct"
}

/**
Expand All @@ -49,9 +49,9 @@ enum ConstructType: String {
- Marshal: Marshal - https://github.com/utahiosmac/Marshal
*/
enum JSONMappingLibrary: String {
case SwiftyJSON
case ObjectMapper
case Marshal
case libSwiftyJSON = "SwiftyJSON"
case libObjectMapper = "ObjectMapper"
case libMarshal = "Marshal"
}

/**
Expand All @@ -61,14 +61,14 @@ enum JSONMappingLibrary: String {
- ValueArray: Array of Value
- Object: Object type
- ObjectArray: Array of object
- EmptyArray: An empty array
- emptyArray: An empty array
- Null: Null value
*/
enum PropertyType: String {
case ValueType
case ValueTypeArray
case ObjectType
case ObjectTypeArray
case EmptyArray
case NullType
case valueType
case valueTypeArray
case objectType
case objectTypeArray
case emptyArray
case nullType
}
32 changes: 16 additions & 16 deletions Core/Generators/ModelGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public struct ModelGenerator {
let subClassType = firstObject.detailedValueType()
// If the type of the first item is an object then make it the base class and generate
// stuff. However, currently it does not make a base file to handle the array.
if subClassType == .Object {
if subClassType == .object {
return self.generateModelForJSON(JSONHelper.reduce(rootObject), defaultClassName, isTopLevelObject)
}
return []
Expand All @@ -76,32 +76,32 @@ public struct ModelGenerator {
let stringConstantName = NameGenerator.variableKey(className, variableName)

switch variableType {
case .Array:
case .array:
if value.arrayValue.count <= 0 {
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.Array.rawValue, stringConstantName, key, .EmptyArray))
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.array.rawValue, stringConstantName, key, .emptyArray))
} else {
let subClassType = value.arrayValue.first!.detailedValueType()
if subClassType == .Object {
if subClassType == .object {
let models = generateModelForJSON(JSONHelper.reduce(value.arrayValue), variableName, false)
modelFiles = modelFiles + models
modelFiles += models
let model = models.first
let classname = model?.fileName
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, classname!, stringConstantName, key, .ObjectTypeArray))
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, classname!, stringConstantName, key, .objectTypeArray))
} else {
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, subClassType.rawValue, stringConstantName, key, .ValueTypeArray))
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, subClassType.rawValue, stringConstantName, key, .valueTypeArray))
}
}
case .Object:
case .object:
let models = generateModelForJSON(value, variableName, false)
let model = models.first
let typeName = model?.fileName
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, typeName!, stringConstantName, key, .ObjectType))
modelFiles = modelFiles + models
case .Null:
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.Null.rawValue, stringConstantName, key, .NullType))
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, typeName!, stringConstantName, key, .objectType))
modelFiles += models
case .null:
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.null.rawValue, stringConstantName, key, .nullType))
break
default:
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, variableType.rawValue, stringConstantName, key, .ValueType))
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, variableType.rawValue, stringConstantName, key, .valueType))
}

}
Expand Down Expand Up @@ -141,11 +141,11 @@ public struct ModelGenerator {
*/
func initialiseModelFileFor(_ modelMappingLibrary: JSONMappingLibrary) -> ModelFile {
switch modelMappingLibrary {
case .ObjectMapper:
case .libObjectMapper:
return ObjectMapperModelFile()
case .SwiftyJSON:
case .libSwiftyJSON:
return SwiftyJSONModelFile()
case .Marshal:
case .libMarshal:
return MarshalModelFile()
}
}
Expand Down
27 changes: 12 additions & 15 deletions Core/Generators/MultipleModelGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ struct MultipleModelGenerator {

var basePath = fromBasePath
if basePath.hasSuffix("/") == false {
basePath = basePath + "/"
basePath += "/"
}
finalPath = basePath + destinationPath
}

if finalPath.hasSuffix("/") == false {
finalPath = finalPath + "/"
finalPath += "/"
}
return finalPath
}
Expand Down Expand Up @@ -145,16 +145,16 @@ struct MultipleModelGenerator {
/// - Returns: Configuration model.
/// - Throws: `MultipleModelGeneratorError.configInvalid` error.
static func loadConfiguration(fromJSON: JSON) throws -> ModelGenerationConfiguration? {
var constructType = ConstructType.ClassType
var constructType = ConstructType.classType
if let type = fromJSON["construct_type"].string, type == "struct" {
constructType = ConstructType.StructType
constructType = ConstructType.structType
}
var jsonLibrary = JSONMappingLibrary.SwiftyJSON
var jsonLibrary = JSONMappingLibrary.libSwiftyJSON
if let type = fromJSON["model_mapping_library"].string {
if type == JSONMappingLibrary.ObjectMapper.rawValue {
jsonLibrary = JSONMappingLibrary.ObjectMapper
} else if type == JSONMappingLibrary.Marshal.rawValue {
jsonLibrary = JSONMappingLibrary.Marshal
if type == JSONMappingLibrary.libObjectMapper.rawValue {
jsonLibrary = JSONMappingLibrary.libObjectMapper
} else if type == JSONMappingLibrary.libMarshal.rawValue {
jsonLibrary = JSONMappingLibrary.libMarshal
}
}
let config = ModelGenerationConfiguration.init(filePath: fromJSON["destination_path"].string ?? "",
Expand Down Expand Up @@ -213,12 +213,9 @@ struct MultipleModelGenerator {

let m = ModelGenerator.init(combinedJSON, (models.first?.configuration)!)
let newModels = m.generateModelForJSON(combinedJSON, fileName, false)
for newModel in newModels {
// We only care about the current model file, all sub models will be merged on their own.
if newModel.fileName == (models.first?.fileName)! {
modelsToReturn.append(newModel)
break
}
for newModel in newModels where newModel.fileName == (models.first?.fileName)! {
modelsToReturn.append(newModel)
break
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Core/Generators/NameGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ struct NameGenerator {
*/
static func fixVariableName(_ variableName: String) -> String {

var _variableName = replaceKeywords(variableName)
_variableName.replaceOccurrencesOfStringsWithString(["-", "_"], " ")
_variableName.trim()
var tmpVariableName = replaceKeywords(variableName)
tmpVariableName.replaceOccurrencesOfStringsWithString(["-", "_"], " ")
tmpVariableName.trim()

var finalVariableName = ""
for (index, var element) in _variableName.components(separatedBy: " ").enumerated() {
for (index, var element) in tmpVariableName.components(separatedBy: " ").enumerated() {
index == 0 ? element.lowerCaseFirst() : element.uppercaseFirst()
finalVariableName.append(element)
}
Expand Down
14 changes: 7 additions & 7 deletions Core/Helpers/JSON+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ extension JSON {
func detailedValueType() -> VariableType {
switch self.type {
case .string:
return VariableType.String
return VariableType.string
case .bool:
return VariableType.Bool
return VariableType.bool
case .array:
return VariableType.Array
return VariableType.array
case .number:
switch CFNumberGetType(self.numberValue as CFNumber) {
case .sInt8Type,
Expand All @@ -37,18 +37,18 @@ extension JSON {
.longLongType,
.cfIndexType,
.nsIntegerType:
return VariableType.Int
return VariableType.int
case .float32Type,
.float64Type,
.floatType,
.cgFloatType,
.doubleType:
return VariableType.Float
return VariableType.float
}
case .null:
return VariableType.Null
return VariableType.null
default:
return VariableType.Object
return VariableType.object
}
}
}
6 changes: 3 additions & 3 deletions Core/Helpers/String+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ extension String {

/// Fetches the first character of the string.
var first: String {
return String(characters.prefix(1))
return String(self.prefix(1))
}

/**
Makes the first character upper case.
*/
mutating func uppercaseFirst() {
self = first.uppercased() + String(characters.dropFirst())
self = first.uppercased() + String(self.dropFirst())
}

/**
Makes the first character lowercase.
*/
mutating func lowerCaseFirst() {
self = first.lowercased() + String(characters.dropFirst())
self = first.lowercased() + String(self.dropFirst())
}

/**
Expand Down
12 changes: 6 additions & 6 deletions Core/Library-Extensions/FileGeneratorExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension FileGenerator {
content = content.replacingOccurrences(of: "{OBJECT_KIND}", with: modelFile.type.rawValue)
content = content.replacingOccurrences(of: "{JSON_PARSER_LIBRARY_BODY}", with: loadFileWith(modelFile.mainBodyTemplateFileName()))

if modelFile.type == .ClassType {
if modelFile.type == .classType {
content = content.replacingOccurrences(of: "{REQUIRED}", with: " required ")
} else {
content = content.replacingOccurrences(of: "{REQUIRED}", with: " ")
Expand All @@ -40,11 +40,11 @@ extension FileGenerator {
if let extendFrom = modelFile.baseElementName() {
classesExtendFrom = [extendFrom]
}
if configuration.supportNSCoding && configuration.constructType == .ClassType {
classesExtendFrom = classesExtendFrom + ["NSCoding"]
if configuration.supportNSCoding && configuration.constructType == .classType {
classesExtendFrom += ["NSCoding"]
}

if configuration.isFinalRequired && configuration.constructType == .ClassType {
if configuration.isFinalRequired && configuration.constructType == .classType {
content = content.replacingOccurrences(of: "{IS_FINAL}", with: " final ")
} else {
content = content.replacingOccurrences(of: "{IS_FINAL}", with: " ")
Expand All @@ -68,11 +68,11 @@ extension FileGenerator {
content = content.replacingOccurrences(of: "{INITIALIZER}", with: initialisers)
content = content.replacingOccurrences(of: "{DESCRIPTION}", with: description)

if configuration.constructType == .StructType {
if configuration.constructType == .structType {
content = content.replacingOccurrences(of: " convenience", with: "")
}

if configuration.supportNSCoding && configuration.constructType == .ClassType {
if configuration.supportNSCoding && configuration.constructType == .classType {
content = content.replacingOccurrences(of: "{NSCODING_SUPPORT}", with: loadFileWith("NSCodingTemplate"))
let encoders = modelFile.component.encoders.map({ doubleTab + $0 }).joined(separator: "\n")
let decoders = modelFile.component.decoders.map({ doubleTab + $0 }).joined(separator: "\n")
Expand Down
14 changes: 7 additions & 7 deletions Core/Library-Extensions/MarshalModelFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct MarshalModelFile: ModelFile, DefaultModelFileComponent {

init() {
self.fileName = ""
type = ConstructType.StructType
type = ConstructType.structType
component = ModelComponent.init()
sourceJSON = JSON.init([])
}
Expand All @@ -45,38 +45,38 @@ struct MarshalModelFile: ModelFile, DefaultModelFileComponent {
mutating func generateAndAddComponentsFor(_ property: PropertyComponent) {
switch property.propertyType {

case .ValueType:
case .valueType:
component.declarations.append(genVariableDeclaration(property.name, property.type, false))
component.description.append(genDescriptionForPrimitive(property.name, property.type, property.constantName))
component.decoders.append(genDecoder(property.name, property.type, property.constantName, false))
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
generateCommonComponentsFor(property)
case .ValueTypeArray:
case .valueTypeArray:
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
component.declarations.append(genVariableDeclaration(property.name, property.type, true))
component.decoders.append(genDecoder(property.name, property.type, property.constantName, true))
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
generateCommonComponentsFor(property)
case .ObjectType:
case .objectType:
component.description.append(genDescriptionForObject(property.name, property.constantName))
component.declarations.append(genVariableDeclaration(property.name, property.type, false))
component.decoders.append(genDecoder(property.name, property.type, property.constantName, false))
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
generateCommonComponentsFor(property)
case .ObjectTypeArray:
case .objectTypeArray:
component.declarations.append(genVariableDeclaration(property.name, property.type, true))
component.description.append(genDescriptionForObjectArray(property.name, property.constantName))
component.decoders.append(genDecoder(property.name, property.type, property.constantName, true))
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
generateCommonComponentsFor(property)

case .EmptyArray:
case .emptyArray:
component.declarations.append(genVariableDeclaration(property.name, "Any", true))
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
component.decoders.append(genDecoder(property.name, "Any", property.constantName, true))
component.encoders.append(genEncoder(property.name, "Any", property.constantName))
generateCommonComponentsFor(property)
case .NullType: break
case .nullType: break
// Currently we do not deal with null values.

}
Expand Down
Loading

0 comments on commit a29063a

Please sign in to comment.