Skip to content

Commit 423100a

Browse files
committed
support TypeScript interface.
1 parent 7db270f commit 423100a

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

SKGenerateModelTool/SKCodeBuilder.swift

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum SKCodeBuilderCodeType: Int {
1313
case OC = 1
1414
case Swift
1515
case Dart
16+
case TypeScript
1617
case Java
1718
}
1819

@@ -47,6 +48,7 @@ class SKCodeBuilder: NSObject {
4748
get {
4849
if config.codeType == .Swift { return "swift" }
4950
else if config.codeType == .Dart { return "dart" }
51+
else if config.codeType == .TypeScript { return "ts" }
5052
return "h"
5153
}
5254
}
@@ -61,7 +63,6 @@ class SKCodeBuilder: NSObject {
6163
let hString = NSMutableString()
6264
let mString = NSMutableString()
6365
let fileName = (config.codeType == .Dart) ? config.rootModelName.underscore_name : config.rootModelName
64-
6566
handleDictValue(dictValue: jsonObj, key: "", hString: hString, mString: mString)
6667
if config.codeType == .OC {
6768
if config.superClassName == "NSObject" {
@@ -97,6 +98,8 @@ class SKCodeBuilder: NSObject {
9798
var fileSuffixName = "m"
9899
if config.codeType == .Dart {
99100
fileSuffixName = "m.dart"
101+
} else if config.codeType == .TypeScript {
102+
fileSuffixName = "ts"
100103
}
101104
let mCommentString =
102105
"""
@@ -108,7 +111,6 @@ class SKCodeBuilder: NSObject {
108111
// Copyright © \(year) SKGenerateModelTool. All rights reserved.
109112
//\n
110113
"""
111-
112114
hString.insert(hCommentString, at: 0)
113115
mString.insert(mCommentString, at: 0)
114116
if let handler = complete {
@@ -150,6 +152,8 @@ class SKCodeBuilder: NSObject {
150152
} else if config.codeType == .Dart {
151153
fileNameH = filePath.appending("/\(fileName).dart")
152154
fileNameM = filePath.appending("/\(fileName).m.dart")
155+
} else if config.codeType == .TypeScript {
156+
fileNameH = filePath.appending("/\(fileName).ts")
153157
}
154158
do {
155159
if !fileNameH.isBlank {
@@ -209,6 +213,13 @@ class SKCodeBuilder: NSObject {
209213
fromJsonString.append("\n\(modelName) _$\(modelName)FromJson(Map<String, dynamic> json, \(modelName) instance) {\n")
210214
toJsonString.append("\nMap<String, dynamic> _$\(modelName)ToJson(\(modelName) instance) {\n")
211215
toJsonString.append(" final Map<String, dynamic> json = new Map<String, dynamic>();\n")
216+
} else if config.codeType == .TypeScript {
217+
if key.isBlank { // Root model
218+
hString.append("\nexport interface \(config.rootModelName) {\n")
219+
} else { // sub model
220+
let modelName = modelClassName(with: key)
221+
hString.append("\n\nexport interface \(modelName) {\n")
222+
}
212223
}
213224

214225
switch dictValue {
@@ -259,6 +270,8 @@ class SKCodeBuilder: NSObject {
259270
260271
"""
261272
toJsonString.append(tString)
273+
} else if config.codeType == .TypeScript {
274+
hString.append(" \(key): \(modelName);\n")
262275
}
263276
self.handleDicts.setValue(value, forKey: key)
264277

@@ -290,6 +303,8 @@ class SKCodeBuilder: NSObject {
290303
291304
"""
292305
toJsonString.append(tString)
306+
} else if config.codeType == .TypeScript {
307+
hString.append(" \(key): null;\n")
293308
}
294309
}
295310
}
@@ -302,6 +317,8 @@ class SKCodeBuilder: NSObject {
302317
hString.append("}\n")
303318
} else if config.codeType == .Dart {
304319
hString.append("}\n")
320+
} else if config.codeType == .TypeScript {
321+
hString.append("}\n")
305322
}
306323
return
307324
}
@@ -329,6 +346,9 @@ class SKCodeBuilder: NSObject {
329346

330347
fromJsonString.append(" return instance;\n");
331348
toJsonString.append(" return json;\n");
349+
} else if config.codeType == .TypeScript {
350+
handleJsonType(hString: hString, mString: mString)
351+
hString.append("}")
332352
}
333353
if !key.isBlank {
334354
self.handleDicts.removeObject(forKey: key)
@@ -481,6 +501,27 @@ class SKCodeBuilder: NSObject {
481501
toJsonString.append(tString)
482502
}
483503
}
504+
} else if config.codeType == .TypeScript {
505+
if let firstObject = arrayValue.first {
506+
if firstObject is String {
507+
// String 类型
508+
hString.append(" \(key)?: string[] | null; \(singlelineCommentName(key, firstObject as! String, false))\n")
509+
}
510+
else if (firstObject is [String:Any]) {
511+
// Dictionary 类型
512+
let key = handleMaybeSameKey(key)
513+
let modeName = modelClassName(with: key)
514+
self.handleDicts.setValue(firstObject, forKey: key)
515+
hString.append(" \(key)?: (\(modeName))[] | null; \(singlelineCommentName(key, "", false))\n")
516+
}
517+
else if (firstObject is [Any]) {
518+
// Array 类型
519+
handleArrayValue(arrayValue: firstObject as! [Any] , key: key, hString: hString)
520+
}
521+
else {
522+
hString.append(" \(key)?: (null))[] | null; \(singlelineCommentName(key, "", false))\n")
523+
}
524+
}
484525
}
485526
}
486527

@@ -515,6 +556,8 @@ class SKCodeBuilder: NSObject {
515556

516557
let tString = " json['\(key)'] = instance.\(key);\n"
517558
toJsonString.append(tString)
559+
} else if config.codeType == .TypeScript {
560+
hString.append(" \(key): number; \(singlelineCommentName(key, "\(numValue)"))\n")
518561
}
519562

520563
case .charType:
@@ -537,6 +580,8 @@ class SKCodeBuilder: NSObject {
537580

538581
let tString = " json['\(key)'] = instance.\(key);\n"
539582
toJsonString.append(tString)
583+
} else if config.codeType == .TypeScript {
584+
hString.append(" \(key): boolean; \(singlelineCommentName(key, (numValue.boolValue == true ? "true" : "false")))\n")
540585
}
541586
} else {
542587
handleIdStringValue(idValue: numValue.stringValue, key: key, hString: hString, ignoreIdValue: ignoreIdValue)
@@ -587,9 +632,10 @@ class SKCodeBuilder: NSObject {
587632
"""
588633

589634
fromJsonString.append(fString)
590-
591635
let tString = " json['\(key)'] = instance.\(key);\n"
592636
toJsonString.append(tString)
637+
} else if config.codeType == .TypeScript {
638+
hString.append(" \(key): number; \(singlelineCommentName(key, "\(intValue)"))\n")
593639
}
594640
}
595641

@@ -637,6 +683,12 @@ class SKCodeBuilder: NSObject {
637683

638684
let tString = " json['\(key)'] = instance.\(key);\n"
639685
toJsonString.append(tString)
686+
} else if config.codeType == .TypeScript {
687+
if idValue.count > 12 {
688+
hString.append(" \(key): string; \(singlelineCommentName(key, idValue, false))\n")
689+
} else {
690+
hString.append(" \(key): string; \(singlelineCommentName(key, idValue))\n")
691+
}
640692
}
641693
}
642694

@@ -670,7 +722,6 @@ class SKCodeBuilder: NSObject {
670722
mString.append("\n}\n")
671723
needLineBreak = true;
672724
}
673-
674725
/// 2.Custom property mapper.
675726
if (self.handlePropertyMapper.count > 0) {
676727
if (needLineBreak) { mString.append("\n") }
@@ -696,7 +747,6 @@ class SKCodeBuilder: NSObject {
696747
mString.append("\n}\n")
697748
needLineBreak = true;
698749
}
699-
700750
if (self.handlePropertyMapper.count > 0) {
701751
if (needLineBreak) { mString.append("\n") }
702752
mString.append("+ (NSDictionary *)mj_replacedKeyFromPropertyName\n")

SKGenerateModelTool/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
5252
reqTypeBtn.selectItem(at: 0)
5353

5454
codeTypeBtn.removeAllItems()
55-
codeTypeBtn.addItems(withTitles: ["Objective-C","Swift","Dart"])
55+
codeTypeBtn.addItems(withTitles: ["Objective-C","Swift","Dart","TypeScript"])
5656
codeTypeBtn.selectItem(at: 0)
5757

5858
jsonTypeBtn.removeAllItems()
@@ -123,7 +123,7 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
123123
attriStr.enumerateLines { (line, _) in
124124
if line.contains("//") {
125125
let substrings = line.components(separatedBy: "//")
126-
let hasHttpLink = line.contains("http://") || line.contains("https://") || line.contains("tel://")
126+
let hasHttpLink = line.contains("http://") || line.contains("https://") || line.contains("://")
127127
// 只有图片链接 且没注释的情况下 不做截断操作
128128
let cannComment = !(substrings.count == 2 && hasHttpLink)
129129
guard cannComment else { return }
@@ -186,7 +186,7 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
186186
var multiplier:CGFloat = 3/5.0
187187
if builder.config.codeType == .OC {
188188
configJsonTextView(text: mString as String , textView: mTextView, color: codeTextColor)
189-
} else if builder.config.codeType == .Swift {
189+
} else if builder.config.codeType == .Swift || builder.config.codeType == .TypeScript {
190190
multiplier = 1.0
191191
} else if builder.config.codeType == .Dart {
192192
configJsonTextView(text: mString as String , textView: mTextView, color: codeTextColor)

0 commit comments

Comments
 (0)