Skip to content

Commit 887a174

Browse files
author
Mccc
committed
优化key的自定义映射规则
1 parent 26a667a commit 887a174

8 files changed

Lines changed: 32 additions & 36 deletions

File tree

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PODS:
88
- FBSnapshotTestCase/SwiftSupport (2.1.4):
99
- FBSnapshotTestCase/Core
1010
- HandyJSON (5.0.0-beta.1)
11-
- SmartCodable (4.2.3)
11+
- SmartCodable (4.2.5)
1212
- SnapKit (5.6.0)
1313

1414
DEPENDENCIES:
@@ -39,7 +39,7 @@ SPEC CHECKSUMS:
3939
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
4040
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
4141
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
42-
SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d
42+
SmartCodable: b39182ec8f12298f277a1c0ec7a7c1031bbfce1b
4343
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
4444

4545
PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f

Example/Pods/Local Podspecs/SmartCodable.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SmartCodable/Test2ViewController.swift

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,29 @@ class Test2ViewController: BaseViewController {
1616
override func viewDidLoad() {
1717
super.viewDidLoad()
1818

19+
SmartConfig.debugMode = .none
20+
1921
let dict: [String: Any] = [
20-
// "name": "mccc",
2122
"age": 30,
23+
"name": "Mccc",
24+
"newName": "newMccc",
2225
"info": [
2326
"name": "mccc111"
2427
],
25-
"sub": [
26-
"subname": "qilin",
27-
"subage": 3,
28-
"info": [
29-
"name": "qilin111"
30-
],
31-
]
32-
3328
]
3429
let model = Model.deserialize(from: dict)
3530
BTPrint.print(model)
36-
37-
print("\n")
38-
39-
let tranDict = model?.toDictionary() ?? [:]
40-
BTPrint.print(tranDict)
4131
}
4232
}
4333

4434
struct Model: SmartCodable {
4535
var name: String = ""
4636
var age: Int = 0
47-
var sub: SubModel = SubModel()
37+
4838

4939
static func mappingForKey() -> [SmartKeyTransformer]? {
5040
[
51-
CodingKeys.name <--- "info.name"
41+
CodingKeys.name <--- ["info.name", "newName", "name", ]
5242
]
5343
}
5444
}

SmartCodable.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
Pod::Spec.new do |s|
1414
s.name = 'SmartCodable'
15-
s.version = '4.2.4'
15+
s.version = '4.2.5'
1616
s.summary = '数据解析库'
1717

1818
s.homepage = 'https://github.com/intsig171'

SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct KeysMapper {
2828
}
2929
return nil
3030
}
31-
31+
3232

3333
private static func parseJSON(from string: String, as type: SmartDecodable.Type) -> Any {
3434
guard let jsonObject = string.toJSONObject() else { return string }
@@ -42,19 +42,24 @@ struct KeysMapper {
4242
private static func mapDictionary(dict: [String: Any], using type: SmartDecodable.Type) -> [String: Any] {
4343
var newDict = dict
4444
type.mappingForKey()?.forEach { mapping in
45-
for oldKey in mapping.from {
46-
let newKey = mapping.to.stringValue
47-
48-
// 先移除数据中原本的字段
45+
let newKey = mapping.to.stringValue
46+
47+
/** 判断原字段是否为干扰字段(映射关系中是否存在该字段)。
48+
* 干扰字段场景:注意这种情况 CodingKeys.name <--- ["newName"]
49+
* 有效字段场景:注意这种情况 CodingKeys.name <--- ["name", "newName"]
50+
*/
51+
if !(mapping.from.contains(newKey)) {
4952
newDict.removeValue(forKey: newKey)
50-
51-
if let value = newDict[oldKey] as? JSONValue, value != .null {
53+
}
54+
55+
// break的作用: 优先使用第一个不为null的字段。
56+
for oldKey in mapping.from {
57+
if let value = newDict[oldKey] as? JSONValue, value != .null { // 映射关系在当前层
5258
newDict[newKey] = newDict[oldKey]
5359
break
54-
} else { // Handles the case of a custom parsing path.
55-
if let pathValue = newDict.getValue(forKeyPath: oldKey) {
56-
newDict.updateValue(pathValue, forKey: newKey)
57-
}
60+
} else if let pathValue = newDict.getValue(forKeyPath: oldKey) { // 映射关系需要根据路径跨层处理
61+
newDict.updateValue(pathValue, forKey: newKey)
62+
break
5863
}
5964
}
6065
}

SmartCodable/Classes/SmartCodable/SmartDecodable.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public protocol SmartDecodable: Decodable {
1212
/// The callback for when mapping is complete
1313
mutating func didFinishMapping()
1414

15-
/// The mapping relationship of decoding keys
15+
/// The mapping relationship of decoding keys.
16+
/// The first mapping relationship that is not null is preferred
1617
static func mappingForKey() -> [SmartKeyTransformer]?
1718

1819
/// The strategy for decoding values

0 commit comments

Comments
 (0)