Skip to content

Commit 994c40b

Browse files
author
shangkun
committed
自动适配json里 // 注释
1 parent 8cea7d6 commit 994c40b

File tree

2 files changed

+128
-88
lines changed

2 files changed

+128
-88
lines changed

SKGenerateModelTool/SKCodeBuilder.swift

Lines changed: 57 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,33 @@ typealias BuildComplete = (NSMutableString, NSMutableString) -> ()
2626
typealias GenerateFileComplete = (Bool, String) -> ()
2727

2828
class SKCodeBuilder: NSObject {
29-
3029
var config = SKCodeBuilderConfig()
3130
lazy var handleDicts = NSMutableDictionary()
3231
lazy var yymodelPropertyGenericClassDicts = NSMutableDictionary()
3332
lazy var handlePropertyMapper = NSMutableDictionary()
3433
lazy var allKeys = [String]()
35-
34+
var commentDicts:[String:String]? // 适配json文件的注释
35+
3636
var fileTye:String {
3737
get {
38-
if config.codeType == .Swift {
39-
return "swift"
40-
}
38+
if config.codeType == .Swift { return "swift" }
4139
return "h"
4240
}
4341
}
44-
4542
// MARK: - Public
46-
4743
func generateCode(with jsonObj:Any, complete:BuildComplete?){
4844

4945
allKeys.removeAll()
50-
5146
let hString = NSMutableString()
5247
let mString = NSMutableString()
53-
5448
handleDictValue(dictValue: jsonObj, key: "", hString: hString, mString: mString)
55-
5649
if config.codeType == .OC {
5750
if config.superClassName == "NSObject" {
5851
hString.insert("\n#import <Foundation/Foundation.h>\n\n", at: 0)
5952
} else {
6053
hString.insert("\n#import \"\(config.superClassName).h\"\n\n", at: 0)
6154
}
6255
mString.insert("\n#import \"\(config.rootModelName).h\"\n\n", at: 0)
63-
6456
} else if config.codeType == .Swift {
6557
if (config.jsonType == .HandyJSON) {
6658
hString.insert("import HandyJSON\n\n", at: 0)
@@ -76,27 +68,26 @@ class SKCodeBuilder: NSObject {
7668
"""
7769
//
7870
// \(config.rootModelName).\(fileTye)
79-
// SKCodeBuilder
71+
// SKGenerateModelTool
8072
//
8173
// Created by \(config.authorName) on \(time).
82-
// Copyright © \(year) SKCodeBuilder. All rights reserved.
74+
// Copyright © \(year) SKGenerateModelTool. All rights reserved.
8375
//\n
8476
"""
8577

8678
let mCommentString =
8779
"""
8880
//
8981
// \(config.rootModelName).m
90-
// SKCodeBuilder
82+
// SKGenerateModelTool
9183
//
9284
// Created by \(config.authorName) on \(time).
93-
// Copyright © \(year) SKCodeBuilder. All rights reserved.
85+
// Copyright © \(year) SKGenerateModelTool. All rights reserved.
9486
//\n
9587
"""
9688

9789
hString.insert(hCommentString, at: 0)
9890
mString.insert(mCommentString, at: 0)
99-
10091
if let handler = complete {
10192
handler(hString, mString)
10293
}
@@ -107,7 +98,6 @@ class SKCodeBuilder: NSObject {
10798

10899
var filePath = filePath
109100
var success = false
110-
111101
if filePath == nil {
112102
if let desktopPath = NSSearchPathForDirectoriesInDomains(.desktopDirectory, .userDomainMask, false).last {
113103
let path = desktopPath.appending("/SKGenerateModelToolFiles")
@@ -129,15 +119,13 @@ class SKCodeBuilder: NSObject {
129119
}
130120

131121
var fileNameH = "", fileNameM = ""
132-
133122
if let filePath = filePath {
134123
if config.codeType == .OC {
135124
fileNameH = filePath.appending("/\(config.rootModelName).h")
136125
fileNameM = filePath.appending("/\(config.rootModelName).m")
137126
} else if config.codeType == .Swift {
138127
fileNameH = filePath.appending("/\(config.rootModelName).swift")
139128
}
140-
141129
do {
142130
if !fileNameH.isBlank {
143131
try hString.write(toFile: fileNameH, atomically: true, encoding: String.Encoding.utf8.rawValue)
@@ -149,11 +137,7 @@ class SKCodeBuilder: NSObject {
149137
} catch {
150138
success = false
151139
}
152-
153-
print("fileNameH = \(fileNameH)")
154-
print("fileNameM = \(fileNameM)")
155140
}
156-
157141
if let complete = complete {
158142
complete(success, filePath!)
159143
}
@@ -168,7 +152,6 @@ class SKCodeBuilder: NSObject {
168152
if key.isBlank { // Root model
169153
hString.append("\n@interface \(config.rootModelName) : \(config.superClassName)\n\n")
170154
mString.append("\n@implementation \(config.rootModelName)\n\n")
171-
172155
} else { // sub model
173156
let modelName = modelClassName(with: key)
174157
hString.insert("@class \(modelName);\n", at: 0)
@@ -178,7 +161,6 @@ class SKCodeBuilder: NSObject {
178161
} else if config.codeType == .Swift {
179162
if key.isBlank { // Root model
180163
hString.append("\nclass \(config.rootModelName) : \(config.superClassName)\n\n")
181-
182164
} else { // sub model
183165
let modelName = modelClassName(with: key)
184166
hString.append("\n\nclass \(modelName) : \(config.superClassName) {\n\n")
@@ -212,7 +194,6 @@ class SKCodeBuilder: NSObject {
212194
if config.codeType == .OC {
213195
hString.append("/** \(key) */\n@property (nonatomic, strong) \(modelName) *\(key);\n")
214196
self.yymodelPropertyGenericClassDicts.setValue(modelName, forKey: key)
215-
216197
} else if config.codeType == .Swift {
217198
hString.append(" /// \n var \(key): \(modelName)?\n")
218199
}
@@ -225,7 +206,7 @@ class SKCodeBuilder: NSObject {
225206
default:
226207
// 识别不出类型
227208
if config.codeType == .OC {
228-
hString.append("/** <#识别不出类型#> */\n@property (nonatomic, strong) id \(key);\n")
209+
hString.append("/** <#泛型#> */\n@property (nonatomic, strong) id \(key);\n")
229210
} else if config.codeType == .Swift {
230211
hString.append(" /// \(key)\n var \(key): Any?\n")
231212
}
@@ -245,17 +226,14 @@ class SKCodeBuilder: NSObject {
245226
if config.codeType == .OC {
246227
hString.append("\n@end\n\n")
247228
handleJsonType(hString: hString, mString: mString)
248-
249229
} else if config.codeType == .Swift {
250230
handleJsonType(hString: hString, mString: mString)
251231
hString.append("}\n")
252232
}
253-
254233
if !key.isBlank {
255234
self.handleDicts.removeObject(forKey: key)
256235
}
257236
mString.append("\n@end\n\n")
258-
259237
self.yymodelPropertyGenericClassDicts.removeAllObjects()
260238
self.handlePropertyMapper.removeAllObjects()
261239
if self.handleDicts.count > 0 {
@@ -271,54 +249,47 @@ class SKCodeBuilder: NSObject {
271249
guard arrayValue.count > 0 else {
272250
return
273251
}
274-
275252
if config.codeType == .OC {
276-
277253
if let firstObject = arrayValue.first {
278-
279254
if firstObject is String {
280255
// String 类型
281-
hString.append("/** \(key) */\n@property (nonatomic, strong) NSArray <NSString *> *\(key);\n")
256+
hString.append("/** \(commentName(key, firstObject as! String)) */\n@property (nonatomic, strong) NSArray <NSString *> *\(key);\n")
282257
}
283258
else if (firstObject is [String:Any]) {
284259
// Dictionary 类型
285260
let key = handleMaybeSameKey(key)
286-
287261
let modeName = modelClassName(with: key)
288262
self.handleDicts.setValue(firstObject, forKey: key)
289263
self.yymodelPropertyGenericClassDicts.setValue(modeName, forKey: key)
290-
hString.append("/** \(key) */\n@property (nonatomic, strong) NSArray <\(modeName) *> *\(key);\n")
264+
hString.append("/** \(commentName(key, key)) */\n@property (nonatomic, strong) NSArray <\(modeName) *> *\(key);\n")
291265
}
292266
else if (firstObject is [Any]) {
293267
// Array 类型
294268
handleArrayValue(arrayValue: firstObject as! [Any] , key: key, hString: hString)
295269
}
296270
else {
297-
hString.append("/** \(key) */\n@property (nonatomic, strong) NSArray *\(key);\n")
271+
hString.append("/** \(commentName(key, key)) */\n@property (nonatomic, strong) NSArray *\(key);\n")
298272
}
299273
}
300-
301274
} else if config.codeType == .Swift {
302-
303275
if let firstObject = arrayValue.first {
304-
305276
if firstObject is String {
306277
// String 类型
307-
hString.append(" /// \n var \(key): [String]?\n")
278+
hString.append(" /// \(commentName(key, firstObject as! String, false)) \n var \(key): [String]?\n")
308279
}
309280
else if (firstObject is [String:Any]) {
310281
// Dictionary 类型
311282
let key = handleMaybeSameKey(key)
312283
let modeName = modelClassName(with: key)
313284
self.handleDicts.setValue(firstObject, forKey: key)
314-
hString.append(" /// \n var \(key): [\(modeName)]?\n")
285+
hString.append(" /// \(commentName(key, key, false)) \n var \(key): [\(modeName)]?\n")
315286
}
316287
else if (firstObject is [Any]) {
317288
// Array 类型
318289
handleArrayValue(arrayValue: firstObject as! [Any] , key: key, hString: hString)
319290
}
320291
else {
321-
hString.append(" /// \n var \(key): [Any]?\n")
292+
hString.append(" /// \(commentName(key, key, false)) \n var \(key): [Any]?\n")
322293
}
323294
}
324295
}
@@ -335,18 +306,18 @@ class SKCodeBuilder: NSObject {
335306
case .doubleType, .floatType, .float32Type, .float64Type, .cgFloatType:
336307
/// 浮点型
337308
if config.codeType == .OC {
338-
hString.append("/** eg. \(numValue) */\n@property (nonatomic, assign) CGFloat \(key);\n")
309+
hString.append("/** eg. \(commentName(key, "\(numValue)")) */\n@property (nonatomic, assign) CGFloat \(key);\n")
339310
} else if config.codeType == .Swift {
340-
hString.append(" /// \(numValue)\n var \(key): Double?\n")
311+
hString.append(" /// \(commentName(key, "\(numValue)"))\n var \(key): Double?\n")
341312
}
342313

343314
case .charType:
344315
if numValue.int32Value == 0 || numValue.int32Value == 1 {
345316
/// Bool 类型
346317
if config.codeType == .OC {
347-
hString.append("/** eg. \(numValue) */\n@property (nonatomic, assign) BOOL \(key);\n")
318+
hString.append("/** eg. \(commentName(key, "\(numValue)")) */\n@property (nonatomic, assign) BOOL \(key);\n")
348319
} else if config.codeType == .Swift {
349-
hString.append(" /// \(numValue.boolValue == true ? "true" : "false")\n var \(key): Bool = false\n")
320+
hString.append(" /// \(commentName(key, (numValue.boolValue == true ? "true" : "false")))\n var \(key): Bool = false\n")
350321
}
351322
} else {
352323
handleIdStringValue(idValue: numValue.stringValue, key: key, hString: hString, ignoreIdValue: ignoreIdValue)
@@ -364,18 +335,19 @@ class SKCodeBuilder: NSObject {
364335

365336
private func handleIdIntValue(intValue: Int, key:String, hString:NSMutableString, ignoreIdValue:Bool) {
366337
/// Int
338+
let comment = (commentName(key, "\(intValue)"))
367339
if key == "id" && !ignoreIdValue {
368340
self.handlePropertyMapper.setValue("id", forKey: "itemId")
369341
if config.codeType == .OC {
370-
hString.append("/** eg. \(intValue) */\n@property (nonatomic, assign) NSInteger itemId;\n")
342+
hString.append("/** eg. \(comment) */\n@property (nonatomic, assign) NSInteger itemId;\n")
371343
} else if config.codeType == .Swift {
372-
hString.append(" /// \(intValue)\n var itemId: Int = 0\n")
344+
hString.append(" /// \(comment)\n var itemId: Int = 0\n")
373345
}
374346
} else {
375347
if config.codeType == .OC {
376-
hString.append("/** eg. \(intValue) */\n@property (nonatomic, assign) NSInteger \(key);\n")
348+
hString.append("/** eg. \(comment) */\n@property (nonatomic, assign) NSInteger \(key);\n")
377349
} else if config.codeType == .Swift {
378-
hString.append(" /// \(intValue)\n var \(key): Int = 0\n")
350+
hString.append(" /// \(comment)\n var \(key): Int = 0\n")
379351
}
380352
}
381353
}
@@ -388,28 +360,25 @@ class SKCodeBuilder: NSObject {
388360
if key == "id" && !ignoreIdValue {
389361
// 字符串id 替换成 itemId
390362
self.handlePropertyMapper.setValue("id", forKey: "itemId")
391-
hString.append("/** eg. \(idValue) */\n@property (nonatomic, copy) NSString *itemId;\n")
363+
hString.append("/** eg. \(commentName(key, idValue)) */\n@property (nonatomic, copy) NSString *itemId;\n")
392364
} else {
393-
if idValue.count > 12 {
394-
hString.append("/** eg. \(key) */\n@property (nonatomic, copy) NSString *\(key);\n")
395-
} else {
396-
hString.append("/** eg. \(idValue) */\n@property (nonatomic, copy) NSString *\(key);\n")
397-
}
365+
hString.append("/** eg. \(commentName(key, idValue)) */\n@property (nonatomic, copy) NSString *\(key);\n")
398366
}
399367
} else if config.codeType == .Swift {
400368
if key == "id" && !ignoreIdValue {
401369
self.handlePropertyMapper.setValue("id", forKey: "itemId")
402-
hString.append(" /// \(idValue)\n var itemId: String?\n")
370+
hString.append(" /// \(commentName(key, idValue))\n var itemId: String?\n")
403371
} else {
404372
if idValue.count > 12 {
405-
hString.append(" /// \n var \(key): String?\n")
373+
hString.append(" /// \(commentName(key, idValue, false))\n var \(key): String?\n")
406374
} else {
407-
hString.append(" /// \(idValue)\n var \(key): String?\n")
375+
hString.append(" /// \(commentName(key, idValue))\n var \(key): String?\n")
408376
}
409377
}
410378
}
411379
}
412380

381+
413382
/// 处理json解析
414383

415384
private func handleJsonType(hString:NSMutableString, mString:NSMutableString) {
@@ -444,9 +413,7 @@ class SKCodeBuilder: NSObject {
444413

445414
/// 2.Custom property mapper.
446415
if (self.handlePropertyMapper.count > 0) {
447-
if (needLineBreak) {
448-
mString.append("\n")
449-
}
416+
if (needLineBreak) { mString.append("\n") }
450417
mString.append("+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper\n")
451418
mString.append("{\n return @{\n")
452419
for (key, obj) in self.handlePropertyMapper {
@@ -471,9 +438,7 @@ class SKCodeBuilder: NSObject {
471438
}
472439

473440
if (self.handlePropertyMapper.count > 0) {
474-
if (needLineBreak) {
475-
mString.append("\n")
476-
}
441+
if (needLineBreak) { mString.append("\n") }
477442
mString.append("+ (NSDictionary *)mj_replacedKeyFromPropertyName\n")
478443
mString.append("{\n return @{\n")
479444
for (key, obj) in self.handlePropertyMapper {
@@ -537,6 +502,21 @@ class SKCodeBuilder: NSObject {
537502
}
538503
return modelName
539504
}
505+
506+
/// 生成注释
507+
private func commentName(_ key:String, _ value:String, _ show:Bool=true) -> String {
508+
var comment = value
509+
if value.count > 12 {
510+
comment = key
511+
if !show {comment = ""}
512+
}
513+
if let commentDict = commentDicts,commentDict.count > 0 {
514+
if let commentValue = commentDict[key] {
515+
comment = commentValue
516+
}
517+
}
518+
return comment
519+
}
540520
}
541521

542522
// MARK: - Config
@@ -591,6 +571,17 @@ extension String {
591571
return NSMakeRange(start, end - start)
592572
}
593573

574+
/// 在字符串中查找另一字符串首次出现的位置(或最后一次出现位置)
575+
func postionOf(sub:String,backwards:Bool = false) -> Int {
576+
var pos = -1
577+
if let range = range(of: sub, options: backwards ? .backwards : .literal, range: nil, locale: nil) {
578+
if !range.isEmpty {
579+
pos = self.distance(from: startIndex, to: range.lowerBound)
580+
}
581+
}
582+
return pos
583+
}
584+
594585
/// url 编码
595586
func _adler32() -> String {
596587
if self.isBlank { return self }

0 commit comments

Comments
 (0)