Skip to content

Commit d781a58

Browse files
authored
feat: add access control selection (#121)
1 parent f0c1de3 commit d781a58

File tree

12 files changed

+108
-44
lines changed

12 files changed

+108
-44
lines changed

Core/Constants.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ enum ConstructType: String {
2929
case structType = "struct"
3030
}
3131

32+
/// Various types of access control modifiers that can be applied to objects and properties.
33+
enum AccessControl: String, CaseIterable {
34+
case `internal`
35+
case `private`
36+
case `public`
37+
38+
/// The prefix to be applied to objects and properties' declarations.
39+
var declarationPrefix: String {
40+
switch self {
41+
case .internal:
42+
// The default access control type, no need to explicitly set it
43+
return ""
44+
case .private:
45+
return "private "
46+
case .public:
47+
return "public "
48+
}
49+
}
50+
}
51+
3252
/// JSON mapping options available in the UI
3353
///
3454
/// - Swift: Pure Swift 5 Codeable

Core/Generator/FileGeneratorExtension.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ extension FileGenerator {
1212
static func generateFileContentWith(_ modelFile: ModelFile, configuration: ModelGenerationConfiguration) -> String {
1313
var content = try! loadFileWith("BaseTemplate")
1414
let singleTab = " ", doubleTab = " "
15+
let accessPrefix = modelFile.accessControl.declarationPrefix
1516
content = content.replacingOccurrences(of: "{OBJECT_NAME}", with: modelFile.fileName)
1617
content = content.replacingOccurrences(of: "{DATE}", with: todayDateString())
1718
content = content.replacingOccurrences(of: "{OBJECT_KIND}", with: modelFile.type.rawValue)
19+
content = content.replacingOccurrences(of: "{ACCESS_CONTROL}", with: accessPrefix)
1820

1921
if let authorName = configuration.authorName {
2022
content = content.replacingOccurrences(of: "__NAME__", with: authorName)
@@ -36,7 +38,7 @@ extension FileGenerator {
3638
if modelFile.configuration?.shouldGenerateInitMethod == true {
3739
let assignment = modelFile.component.initialiserFunctionComponent.map { doubleTab + $0.assignmentString }.joined(separator: "\n")
3840
let functionParameters = modelFile.component.initialiserFunctionComponent.map { $0.functionParameter }.joined(separator: ", ")
39-
let initialiserFunctionStatement = "\n\(singleTab)init (\(functionParameters)) {"
41+
let initialiserFunctionStatement = "\n\(singleTab)\(accessPrefix)init (\(functionParameters)) {"
4042
content = content.replacingOccurrences(of: "{INITIALIZER_FUNCTION_DECLRATION}", with: initialiserFunctionStatement)
4143
content = content.replacingOccurrences(of: "{INITIALISER_FUNCTION_ASSIGNMENT}", with: assignment)
4244
content = content.replacingOccurrences(of: "{INITIALISER_FUNCTION_END}", with: "\(singleTab)}\n")

Core/Generator/ModalGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public struct ModelGenerator {
114114
- returns: Notification tht was generated.
115115
*/
116116
func generateNotificationFor(_ modelFiles: [ModelFile]) -> NSUserNotification {
117-
let notification: NSUserNotification = NSUserNotification()
117+
let notification = NSUserNotification()
118118
notification.title = NSLocalizedString("SwiftyJSONAccelerator", comment: "")
119119
if !modelFiles.isEmpty {
120120
let firstModel = (modelFiles.first)!

Core/Generator/Model-File-Components/ModelFile.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ protocol ModelFile {
2020
/// Type of the the object, if a structure or a class.
2121
var type: ConstructType { get }
2222

23+
/// Type of access control for object and properties.
24+
var accessControl: AccessControl { get }
25+
2326
/// Storage for various components of the model, it is used to store the intermediate data.
2427
var component: ModelComponent { get }
2528

Core/Generator/Model-File-Components/SwiftJSONModelFile.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import SwiftyJSON
1313
struct SwiftJSONModelFile: ModelFile {
1414
var fileName: String
1515
var type: ConstructType
16+
var accessControl: AccessControl
1617
var component: ModelComponent
1718
var sourceJSON: JSON
1819
var configuration: ModelGenerationConfiguration?
@@ -24,11 +25,13 @@ struct SwiftJSONModelFile: ModelFile {
2425
type = ConstructType.structType
2526
component = ModelComponent()
2627
sourceJSON = JSON([])
28+
accessControl = .internal
2729
}
2830

2931
mutating func setInfo(_ fileName: String, _ configuration: ModelGenerationConfiguration) {
3032
self.fileName = fileName
3133
type = configuration.constructType
34+
accessControl = configuration.accessControl
3235
self.configuration = configuration
3336
}
3437

@@ -79,10 +82,8 @@ struct SwiftJSONModelFile: ModelFile {
7982
}
8083

8184
func genPrimitiveVariableDeclaration(_ name: String, _ type: String, _ isOptional: Bool) -> String {
82-
if isOptional {
83-
return "var \(name): \(type)?"
84-
}
85-
return "var \(name): \(type)"
85+
let optionalSuffix = isOptional ? "?" : ""
86+
return "\(accessControl.declarationPrefix)var \(name): \(type)\(optionalSuffix)"
8687
}
8788

8889
/// Generate the variable declaration string

Core/Generator/ModelGenerationConfiguration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct ModelGenerationConfiguration {
2222
var prefix: String?
2323
/// Type of the object that have to be generated.
2424
var constructType: ConstructType
25+
/// Access control for object and properties.
26+
var accessControl: AccessControl
2527
/// Model mapping library to be used.
2628
var modelMappingLibrary: JSONMappingMethod
2729
/// Separate coding keys into an enum and not use string.
@@ -36,6 +38,7 @@ struct ModelGenerationConfiguration {
3638
separateCodingKeys = true
3739
modelMappingLibrary = .swiftNormal
3840
constructType = .classType
41+
accessControl = .internal
3942
prefix = ""
4043
filePath = ""
4144
baseClassName = ""

Core/Generator/MultipleModelGenerator.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ struct MultipleModelGenerator {
149149
if let type = fromJSON["construct_type"].string, type == "struct" {
150150
constructType = ConstructType.structType
151151
}
152+
var accessControl = AccessControl.internal
153+
if let string = fromJSON["access_control"].string, let value = AccessControl(rawValue: string) {
154+
accessControl = value
155+
}
152156

153157
let initialiserParameter = fromJSON["initaliser_needed"].bool
154158
let initialisersNeeded = initialiserParameter != nil ? initialiserParameter! : true
@@ -159,6 +163,7 @@ struct MultipleModelGenerator {
159163
companyName: fromJSON["company_name"].string,
160164
prefix: fromJSON["prefix"].string,
161165
constructType: constructType,
166+
accessControl: accessControl,
162167
modelMappingLibrary: jsonLibrary,
163168
separateCodingKeys: fromJSON["separate_coding_keys"].boolValue,
164169
variablesOptional: fromJSON["variable_option"].boolValue,

Core/Template/BaseTemplate.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
{OBJECT_KIND} {OBJECT_NAME}: Codable {
10+
{ACCESS_CONTROL}{OBJECT_KIND} {OBJECT_NAME}: Codable {
1111

1212
enum CodingKeys: String, CodingKey {
1313
{STRING_CONSTANT}
@@ -17,7 +17,7 @@ import Foundation
1717
{INITIALIZER_FUNCTION_DECLRATION}
1818
{INITIALISER_FUNCTION_ASSIGNMENT}
1919
{INITIALISER_FUNCTION_END}
20-
{REQUIRED}init(from decoder: Decoder) throws {
20+
{ACCESS_CONTROL}{REQUIRED}init(from decoder: Decoder) throws {
2121
let container = try decoder.container(keyedBy: CodingKeys.self)
2222
{INITIALIZER}
2323
}

0 commit comments

Comments
 (0)