From 887a174b31da0ffa90ecdb9871d08cc531aad45e Mon Sep 17 00:00:00 2001 From: Mccc <> Date: Fri, 18 Oct 2024 17:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96key=E7=9A=84=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=98=A0=E5=B0=84=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example/Podfile.lock | 4 +-- .../Local Podspecs/SmartCodable.podspec.json | 4 +-- Example/Pods/Manifest.lock | 4 +-- .../SmartCodable/SmartCodable-Info.plist | 2 +- .../SmartCodable/Test2ViewController.swift | 22 +++++---------- SmartCodable.podspec | 2 +- .../JSONDecoder/Decoder/KeysMapper.swift | 27 +++++++++++-------- .../Classes/SmartCodable/SmartDecodable.swift | 3 ++- 8 files changed, 32 insertions(+), 36 deletions(-) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 118835d..790ca19 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -8,7 +8,7 @@ PODS: - FBSnapshotTestCase/SwiftSupport (2.1.4): - FBSnapshotTestCase/Core - HandyJSON (5.0.0-beta.1) - - SmartCodable (4.2.3) + - SmartCodable (4.2.5) - SnapKit (5.6.0) DEPENDENCIES: @@ -39,7 +39,7 @@ SPEC CHECKSUMS: CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980 FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978 - SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d + SmartCodable: b39182ec8f12298f277a1c0ec7a7c1031bbfce1b SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25 PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f diff --git a/Example/Pods/Local Podspecs/SmartCodable.podspec.json b/Example/Pods/Local Podspecs/SmartCodable.podspec.json index 43608c3..22b7660 100644 --- a/Example/Pods/Local Podspecs/SmartCodable.podspec.json +++ b/Example/Pods/Local Podspecs/SmartCodable.podspec.json @@ -1,6 +1,6 @@ { "name": "SmartCodable", - "version": "4.2.3", + "version": "4.2.5", "summary": "数据解析库", "homepage": "https://github.com/intsig171", "license": { @@ -12,7 +12,7 @@ }, "source": { "git": "https://github.com/intsig171/SmartCodable.git", - "tag": "4.2.3" + "tag": "4.2.5" }, "platforms": { "ios": "11.0", diff --git a/Example/Pods/Manifest.lock b/Example/Pods/Manifest.lock index 118835d..790ca19 100644 --- a/Example/Pods/Manifest.lock +++ b/Example/Pods/Manifest.lock @@ -8,7 +8,7 @@ PODS: - FBSnapshotTestCase/SwiftSupport (2.1.4): - FBSnapshotTestCase/Core - HandyJSON (5.0.0-beta.1) - - SmartCodable (4.2.3) + - SmartCodable (4.2.5) - SnapKit (5.6.0) DEPENDENCIES: @@ -39,7 +39,7 @@ SPEC CHECKSUMS: CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980 FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978 - SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d + SmartCodable: b39182ec8f12298f277a1c0ec7a7c1031bbfce1b SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25 PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f diff --git a/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist b/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist index efb5db3..bc1e001 100644 --- a/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist +++ b/Example/Pods/Target Support Files/SmartCodable/SmartCodable-Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 4.2.3 + 4.2.5 CFBundleSignature ???? CFBundleVersion diff --git a/Example/SmartCodable/Test2ViewController.swift b/Example/SmartCodable/Test2ViewController.swift index 7ea090b..d30d855 100644 --- a/Example/SmartCodable/Test2ViewController.swift +++ b/Example/SmartCodable/Test2ViewController.swift @@ -16,39 +16,29 @@ class Test2ViewController: BaseViewController { override func viewDidLoad() { super.viewDidLoad() + SmartConfig.debugMode = .none + let dict: [String: Any] = [ -// "name": "mccc", "age": 30, + "name": "Mccc", + "newName": "newMccc", "info": [ "name": "mccc111" ], - "sub": [ - "subname": "qilin", - "subage": 3, - "info": [ - "name": "qilin111" - ], - ] - ] let model = Model.deserialize(from: dict) BTPrint.print(model) - - print("\n") - - let tranDict = model?.toDictionary() ?? [:] - BTPrint.print(tranDict) } } struct Model: SmartCodable { var name: String = "" var age: Int = 0 - var sub: SubModel = SubModel() + static func mappingForKey() -> [SmartKeyTransformer]? { [ - CodingKeys.name <--- "info.name" + CodingKeys.name <--- ["info.name", "newName", "name", ] ] } } diff --git a/SmartCodable.podspec b/SmartCodable.podspec index 21ddc37..e5a02d9 100644 --- a/SmartCodable.podspec +++ b/SmartCodable.podspec @@ -12,7 +12,7 @@ Pod::Spec.new do |s| s.name = 'SmartCodable' - s.version = '4.2.4' + s.version = '4.2.5' s.summary = '数据解析库' s.homepage = 'https://github.com/intsig171' diff --git a/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift b/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift index 09f4369..8e0c755 100644 --- a/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift +++ b/SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift @@ -28,7 +28,7 @@ struct KeysMapper { } return nil } - + private static func parseJSON(from string: String, as type: SmartDecodable.Type) -> Any { guard let jsonObject = string.toJSONObject() else { return string } @@ -42,19 +42,24 @@ struct KeysMapper { private static func mapDictionary(dict: [String: Any], using type: SmartDecodable.Type) -> [String: Any] { var newDict = dict type.mappingForKey()?.forEach { mapping in - for oldKey in mapping.from { - let newKey = mapping.to.stringValue - - // 先移除数据中原本的字段 + let newKey = mapping.to.stringValue + + /** 判断原字段是否为干扰字段(映射关系中是否存在该字段)。 + * 干扰字段场景:注意这种情况 CodingKeys.name <--- ["newName"] + * 有效字段场景:注意这种情况 CodingKeys.name <--- ["name", "newName"] + */ + if !(mapping.from.contains(newKey)) { newDict.removeValue(forKey: newKey) - - if let value = newDict[oldKey] as? JSONValue, value != .null { + } + + // break的作用: 优先使用第一个不为null的字段。 + for oldKey in mapping.from { + if let value = newDict[oldKey] as? JSONValue, value != .null { // 映射关系在当前层 newDict[newKey] = newDict[oldKey] break - } else { // Handles the case of a custom parsing path. - if let pathValue = newDict.getValue(forKeyPath: oldKey) { - newDict.updateValue(pathValue, forKey: newKey) - } + } else if let pathValue = newDict.getValue(forKeyPath: oldKey) { // 映射关系需要根据路径跨层处理 + newDict.updateValue(pathValue, forKey: newKey) + break } } } diff --git a/SmartCodable/Classes/SmartCodable/SmartDecodable.swift b/SmartCodable/Classes/SmartCodable/SmartDecodable.swift index d1f8c28..39b8a8d 100644 --- a/SmartCodable/Classes/SmartCodable/SmartDecodable.swift +++ b/SmartCodable/Classes/SmartCodable/SmartDecodable.swift @@ -12,7 +12,8 @@ public protocol SmartDecodable: Decodable { /// The callback for when mapping is complete mutating func didFinishMapping() - /// The mapping relationship of decoding keys + /// The mapping relationship of decoding keys. + /// The first mapping relationship that is not null is preferred static func mappingForKey() -> [SmartKeyTransformer]? /// The strategy for decoding values