Skip to content

Commit

Permalink
优化key的自定义映射规则
Browse files Browse the repository at this point in the history
  • Loading branch information
Mccc committed Oct 18, 2024
1 parent 26a667a commit 887a174
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d
SmartCodable: b39182ec8f12298f277a1c0ec7a7c1031bbfce1b
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25

PODFILE CHECKSUM: 7f3af03f81934df0c035518074a7abbec8fa9d3f
Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/SmartCodable.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 6 additions & 16 deletions Example/SmartCodable/Test2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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", ]
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion SmartCodable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
27 changes: 16 additions & 11 deletions SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion SmartCodable/Classes/SmartCodable/SmartDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 887a174

Please sign in to comment.