Skip to content

Commit a29063a

Browse files
authored
Merge pull request #91 from insanoid/v1.5.0
V1.5.0
2 parents fd4a6f1 + 1207b39 commit a29063a

22 files changed

+339
-281
lines changed

.swiftlint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ disabled_rules: # rule identifiers to exclude from running
33
- function_body_length
44
- unused_closure_parameter
55
- valid_docs
6+
- large_tuple
7+
- identifier_name
68
excluded: # paths to ignore during linting. Takes precedence over `included`.
79
- SwiftyJSONAcceleratorTests
810
- Pods

Core/Constants.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ import Foundation
2020
- Object: Object.
2121
*/
2222
enum VariableType: String {
23-
case String = "String"
24-
case Int = "Int"
25-
case Float = "Float"
26-
case Double = "Double"
27-
case Bool = "Bool"
28-
case Array = "[]"
29-
case Object = "{OBJ}"
30-
case Null = "Any"
23+
case string = "String"
24+
case int = "Int"
25+
case float = "Float"
26+
case double = "Double"
27+
case bool = "Bool"
28+
case array = "[]"
29+
case object = "{OBJ}"
30+
case null = "Any"
3131
}
3232

3333
/**
3434
Various types of construct that can be generated.
3535

36-
- ClassType: Model with construct type class.
37-
- StructType: Model with construct type struct.
36+
- classType: Model with construct type class.
37+
- structType: Model with construct type struct.
3838
*/
3939
enum ConstructType: String {
40-
case ClassType = "class"
41-
case StructType = "struct"
40+
case classType = "class"
41+
case structType = "struct"
4242
}
4343

4444
/**
@@ -49,9 +49,9 @@ enum ConstructType: String {
4949
- Marshal: Marshal - https://github.com/utahiosmac/Marshal
5050
*/
5151
enum JSONMappingLibrary: String {
52-
case SwiftyJSON
53-
case ObjectMapper
54-
case Marshal
52+
case libSwiftyJSON = "SwiftyJSON"
53+
case libObjectMapper = "ObjectMapper"
54+
case libMarshal = "Marshal"
5555
}
5656

5757
/**
@@ -61,14 +61,14 @@ enum JSONMappingLibrary: String {
6161
- ValueArray: Array of Value
6262
- Object: Object type
6363
- ObjectArray: Array of object
64-
- EmptyArray: An empty array
64+
- emptyArray: An empty array
6565
- Null: Null value
6666
*/
6767
enum PropertyType: String {
68-
case ValueType
69-
case ValueTypeArray
70-
case ObjectType
71-
case ObjectTypeArray
72-
case EmptyArray
73-
case NullType
68+
case valueType
69+
case valueTypeArray
70+
case objectType
71+
case objectTypeArray
72+
case emptyArray
73+
case nullType
7474
}

Core/Generators/ModelGenerator.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct ModelGenerator {
5656
let subClassType = firstObject.detailedValueType()
5757
// If the type of the first item is an object then make it the base class and generate
5858
// stuff. However, currently it does not make a base file to handle the array.
59-
if subClassType == .Object {
59+
if subClassType == .object {
6060
return self.generateModelForJSON(JSONHelper.reduce(rootObject), defaultClassName, isTopLevelObject)
6161
}
6262
return []
@@ -76,32 +76,32 @@ public struct ModelGenerator {
7676
let stringConstantName = NameGenerator.variableKey(className, variableName)
7777

7878
switch variableType {
79-
case .Array:
79+
case .array:
8080
if value.arrayValue.count <= 0 {
81-
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.Array.rawValue, stringConstantName, key, .EmptyArray))
81+
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.array.rawValue, stringConstantName, key, .emptyArray))
8282
} else {
8383
let subClassType = value.arrayValue.first!.detailedValueType()
84-
if subClassType == .Object {
84+
if subClassType == .object {
8585
let models = generateModelForJSON(JSONHelper.reduce(value.arrayValue), variableName, false)
86-
modelFiles = modelFiles + models
86+
modelFiles += models
8787
let model = models.first
8888
let classname = model?.fileName
89-
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, classname!, stringConstantName, key, .ObjectTypeArray))
89+
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, classname!, stringConstantName, key, .objectTypeArray))
9090
} else {
91-
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, subClassType.rawValue, stringConstantName, key, .ValueTypeArray))
91+
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, subClassType.rawValue, stringConstantName, key, .valueTypeArray))
9292
}
9393
}
94-
case .Object:
94+
case .object:
9595
let models = generateModelForJSON(value, variableName, false)
9696
let model = models.first
9797
let typeName = model?.fileName
98-
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, typeName!, stringConstantName, key, .ObjectType))
99-
modelFiles = modelFiles + models
100-
case .Null:
101-
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.Null.rawValue, stringConstantName, key, .NullType))
98+
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, typeName!, stringConstantName, key, .objectType))
99+
modelFiles += models
100+
case .null:
101+
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, VariableType.null.rawValue, stringConstantName, key, .nullType))
102102
break
103103
default:
104-
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, variableType.rawValue, stringConstantName, key, .ValueType))
104+
currentModel.generateAndAddComponentsFor(PropertyComponent.init(variableName, variableType.rawValue, stringConstantName, key, .valueType))
105105
}
106106

107107
}
@@ -141,11 +141,11 @@ public struct ModelGenerator {
141141
*/
142142
func initialiseModelFileFor(_ modelMappingLibrary: JSONMappingLibrary) -> ModelFile {
143143
switch modelMappingLibrary {
144-
case .ObjectMapper:
144+
case .libObjectMapper:
145145
return ObjectMapperModelFile()
146-
case .SwiftyJSON:
146+
case .libSwiftyJSON:
147147
return SwiftyJSONModelFile()
148-
case .Marshal:
148+
case .libMarshal:
149149
return MarshalModelFile()
150150
}
151151
}

Core/Generators/MultipleModelGenerator.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ struct MultipleModelGenerator {
107107

108108
var basePath = fromBasePath
109109
if basePath.hasSuffix("/") == false {
110-
basePath = basePath + "/"
110+
basePath += "/"
111111
}
112112
finalPath = basePath + destinationPath
113113
}
114114

115115
if finalPath.hasSuffix("/") == false {
116-
finalPath = finalPath + "/"
116+
finalPath += "/"
117117
}
118118
return finalPath
119119
}
@@ -145,16 +145,16 @@ struct MultipleModelGenerator {
145145
/// - Returns: Configuration model.
146146
/// - Throws: `MultipleModelGeneratorError.configInvalid` error.
147147
static func loadConfiguration(fromJSON: JSON) throws -> ModelGenerationConfiguration? {
148-
var constructType = ConstructType.ClassType
148+
var constructType = ConstructType.classType
149149
if let type = fromJSON["construct_type"].string, type == "struct" {
150-
constructType = ConstructType.StructType
150+
constructType = ConstructType.structType
151151
}
152-
var jsonLibrary = JSONMappingLibrary.SwiftyJSON
152+
var jsonLibrary = JSONMappingLibrary.libSwiftyJSON
153153
if let type = fromJSON["model_mapping_library"].string {
154-
if type == JSONMappingLibrary.ObjectMapper.rawValue {
155-
jsonLibrary = JSONMappingLibrary.ObjectMapper
156-
} else if type == JSONMappingLibrary.Marshal.rawValue {
157-
jsonLibrary = JSONMappingLibrary.Marshal
154+
if type == JSONMappingLibrary.libObjectMapper.rawValue {
155+
jsonLibrary = JSONMappingLibrary.libObjectMapper
156+
} else if type == JSONMappingLibrary.libMarshal.rawValue {
157+
jsonLibrary = JSONMappingLibrary.libMarshal
158158
}
159159
}
160160
let config = ModelGenerationConfiguration.init(filePath: fromJSON["destination_path"].string ?? "",
@@ -213,12 +213,9 @@ struct MultipleModelGenerator {
213213

214214
let m = ModelGenerator.init(combinedJSON, (models.first?.configuration)!)
215215
let newModels = m.generateModelForJSON(combinedJSON, fileName, false)
216-
for newModel in newModels {
217-
// We only care about the current model file, all sub models will be merged on their own.
218-
if newModel.fileName == (models.first?.fileName)! {
219-
modelsToReturn.append(newModel)
220-
break
221-
}
216+
for newModel in newModels where newModel.fileName == (models.first?.fileName)! {
217+
modelsToReturn.append(newModel)
218+
break
222219
}
223220
}
224221
}

Core/Generators/NameGenerator.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ struct NameGenerator {
4444
*/
4545
static func fixVariableName(_ variableName: String) -> String {
4646

47-
var _variableName = replaceKeywords(variableName)
48-
_variableName.replaceOccurrencesOfStringsWithString(["-", "_"], " ")
49-
_variableName.trim()
47+
var tmpVariableName = replaceKeywords(variableName)
48+
tmpVariableName.replaceOccurrencesOfStringsWithString(["-", "_"], " ")
49+
tmpVariableName.trim()
5050

5151
var finalVariableName = ""
52-
for (index, var element) in _variableName.components(separatedBy: " ").enumerated() {
52+
for (index, var element) in tmpVariableName.components(separatedBy: " ").enumerated() {
5353
index == 0 ? element.lowerCaseFirst() : element.uppercaseFirst()
5454
finalVariableName.append(element)
5555
}

Core/Helpers/JSON+Helpers.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ extension JSON {
1919
func detailedValueType() -> VariableType {
2020
switch self.type {
2121
case .string:
22-
return VariableType.String
22+
return VariableType.string
2323
case .bool:
24-
return VariableType.Bool
24+
return VariableType.bool
2525
case .array:
26-
return VariableType.Array
26+
return VariableType.array
2727
case .number:
2828
switch CFNumberGetType(self.numberValue as CFNumber) {
2929
case .sInt8Type,
@@ -37,18 +37,18 @@ extension JSON {
3737
.longLongType,
3838
.cfIndexType,
3939
.nsIntegerType:
40-
return VariableType.Int
40+
return VariableType.int
4141
case .float32Type,
4242
.float64Type,
4343
.floatType,
4444
.cgFloatType,
4545
.doubleType:
46-
return VariableType.Float
46+
return VariableType.float
4747
}
4848
case .null:
49-
return VariableType.Null
49+
return VariableType.null
5050
default:
51-
return VariableType.Object
51+
return VariableType.object
5252
}
5353
}
5454
}

Core/Helpers/String+Helpers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ extension String {
1313

1414
/// Fetches the first character of the string.
1515
var first: String {
16-
return String(characters.prefix(1))
16+
return String(self.prefix(1))
1717
}
1818

1919
/**
2020
Makes the first character upper case.
2121
*/
2222
mutating func uppercaseFirst() {
23-
self = first.uppercased() + String(characters.dropFirst())
23+
self = first.uppercased() + String(self.dropFirst())
2424
}
2525

2626
/**
2727
Makes the first character lowercase.
2828
*/
2929
mutating func lowerCaseFirst() {
30-
self = first.lowercased() + String(characters.dropFirst())
30+
self = first.lowercased() + String(self.dropFirst())
3131
}
3232

3333
/**

Core/Library-Extensions/FileGeneratorExtension.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension FileGenerator {
1919
content = content.replacingOccurrences(of: "{OBJECT_KIND}", with: modelFile.type.rawValue)
2020
content = content.replacingOccurrences(of: "{JSON_PARSER_LIBRARY_BODY}", with: loadFileWith(modelFile.mainBodyTemplateFileName()))
2121

22-
if modelFile.type == .ClassType {
22+
if modelFile.type == .classType {
2323
content = content.replacingOccurrences(of: "{REQUIRED}", with: " required ")
2424
} else {
2525
content = content.replacingOccurrences(of: "{REQUIRED}", with: " ")
@@ -40,11 +40,11 @@ extension FileGenerator {
4040
if let extendFrom = modelFile.baseElementName() {
4141
classesExtendFrom = [extendFrom]
4242
}
43-
if configuration.supportNSCoding && configuration.constructType == .ClassType {
44-
classesExtendFrom = classesExtendFrom + ["NSCoding"]
43+
if configuration.supportNSCoding && configuration.constructType == .classType {
44+
classesExtendFrom += ["NSCoding"]
4545
}
4646

47-
if configuration.isFinalRequired && configuration.constructType == .ClassType {
47+
if configuration.isFinalRequired && configuration.constructType == .classType {
4848
content = content.replacingOccurrences(of: "{IS_FINAL}", with: " final ")
4949
} else {
5050
content = content.replacingOccurrences(of: "{IS_FINAL}", with: " ")
@@ -68,11 +68,11 @@ extension FileGenerator {
6868
content = content.replacingOccurrences(of: "{INITIALIZER}", with: initialisers)
6969
content = content.replacingOccurrences(of: "{DESCRIPTION}", with: description)
7070

71-
if configuration.constructType == .StructType {
71+
if configuration.constructType == .structType {
7272
content = content.replacingOccurrences(of: " convenience", with: "")
7373
}
7474

75-
if configuration.supportNSCoding && configuration.constructType == .ClassType {
75+
if configuration.supportNSCoding && configuration.constructType == .classType {
7676
content = content.replacingOccurrences(of: "{NSCODING_SUPPORT}", with: loadFileWith("NSCodingTemplate"))
7777
let encoders = modelFile.component.encoders.map({ doubleTab + $0 }).joined(separator: "\n")
7878
let decoders = modelFile.component.decoders.map({ doubleTab + $0 }).joined(separator: "\n")

Core/Library-Extensions/MarshalModelFile.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct MarshalModelFile: ModelFile, DefaultModelFileComponent {
1919

2020
init() {
2121
self.fileName = ""
22-
type = ConstructType.StructType
22+
type = ConstructType.structType
2323
component = ModelComponent.init()
2424
sourceJSON = JSON.init([])
2525
}
@@ -45,38 +45,38 @@ struct MarshalModelFile: ModelFile, DefaultModelFileComponent {
4545
mutating func generateAndAddComponentsFor(_ property: PropertyComponent) {
4646
switch property.propertyType {
4747

48-
case .ValueType:
48+
case .valueType:
4949
component.declarations.append(genVariableDeclaration(property.name, property.type, false))
5050
component.description.append(genDescriptionForPrimitive(property.name, property.type, property.constantName))
5151
component.decoders.append(genDecoder(property.name, property.type, property.constantName, false))
5252
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
5353
generateCommonComponentsFor(property)
54-
case .ValueTypeArray:
54+
case .valueTypeArray:
5555
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
5656
component.declarations.append(genVariableDeclaration(property.name, property.type, true))
5757
component.decoders.append(genDecoder(property.name, property.type, property.constantName, true))
5858
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
5959
generateCommonComponentsFor(property)
60-
case .ObjectType:
60+
case .objectType:
6161
component.description.append(genDescriptionForObject(property.name, property.constantName))
6262
component.declarations.append(genVariableDeclaration(property.name, property.type, false))
6363
component.decoders.append(genDecoder(property.name, property.type, property.constantName, false))
6464
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
6565
generateCommonComponentsFor(property)
66-
case .ObjectTypeArray:
66+
case .objectTypeArray:
6767
component.declarations.append(genVariableDeclaration(property.name, property.type, true))
6868
component.description.append(genDescriptionForObjectArray(property.name, property.constantName))
6969
component.decoders.append(genDecoder(property.name, property.type, property.constantName, true))
7070
component.encoders.append(genEncoder(property.name, property.type, property.constantName))
7171
generateCommonComponentsFor(property)
7272

73-
case .EmptyArray:
73+
case .emptyArray:
7474
component.declarations.append(genVariableDeclaration(property.name, "Any", true))
7575
component.description.append(genDescriptionForPrimitiveArray(property.name, property.constantName))
7676
component.decoders.append(genDecoder(property.name, "Any", property.constantName, true))
7777
component.encoders.append(genEncoder(property.name, "Any", property.constantName))
7878
generateCommonComponentsFor(property)
79-
case .NullType: break
79+
case .nullType: break
8080
// Currently we do not deal with null values.
8181

8282
}

0 commit comments

Comments
 (0)