Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mccc committed Oct 12, 2024
1 parent b17d751 commit 0b6f0a5
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 22 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.2)
- SmartCodable (4.2.3)
- SnapKit (5.6.0)

DEPENDENCIES:
Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
CleanJSON: 910a36465ce4829e264a902ccf6d1455fdd9f980
FBSnapshotTestCase: 094f9f314decbabe373b87cc339bea235a63e07a
HandyJSON: 582477127ab3ab65bd2e471815f1a7b846856978
SmartCodable: 4f52d801dbd15856fabb69110d24f31f49145734
SmartCodable: f01c43e62a8867828fb9f51f4b354b657dafc37d
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.

32 changes: 29 additions & 3 deletions Example/SmartCodable/Test2ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,43 @@ class Test2ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()

let dict = [
"name": 1.22222
let dict: [String: Any] = [
"name": "mccc",
"subModel": "mccc111",

]
let model = Model.deserialize(from: dict)
print(model)
print(model?.subModel?.rawValue)

let dict1 = model?.toDictionary()
print(dict1)

}

struct Model: SmartCodable {
var name: String = ""
@SmartPublished
var subModel: TestEnum?


static func mappingForValue() -> [SmartValueTransformer]? {
[
CodingKeys.name <--- FastTransformer<String, String>(fromJSON: { json in
"abc"
}),
CodingKeys.subModel <--- FastTransformer<TestEnum, String>(fromJSON: { json in
TestEnum.man
}),
]
}
}

enum TestEnum: String, SmartCaseDefaultable {
case man
}

struct SubModel: SmartCodable {
var name: String = ""

}
}
33 changes: 32 additions & 1 deletion Example/SmartCodable/TestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import CleanJSON
import BTPrint



/** 字典的值情况
1. @Published 修饰的属性的解析。
2. 继承关系!!!!
Expand All @@ -35,6 +34,38 @@ class TestViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
let jsonString = """
{
"enjoyCount": 462,
"talkCount": null,
}
"""

guard let model = RecommendModel.deserialize(from: jsonString) else {
return
}

print(model)
}


struct RecommendModel: SmartCodable {

/// 点赞数
var enjoyCount: Int = 0
/// 评论数
var commentCount: Int = 0



static func mappingForKey() -> [SmartKeyTransformer]? {
[
CodingKeys.commentCount <--- ["commentCount","talkCount","postCommentCount","topicCommentCount","topicTalkCount", "articleCommentCount"],
CodingKeys.enjoyCount <--- ["articleEnjoyCount","enjoyCount","topicEnjoyCount","postEnjoyCount"],
]
}
}

}
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.2'
s.version = '4.2.3'
s.summary = '数据解析库'

s.homepage = 'https://github.com/intsig171'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,16 @@ extension JSONDecoderImpl.KeyedContainer {

// 如果值可以被成功获取
if let value = try? getValue(forKey: key) {
if let decoded = impl.cache.tranform(value: value, for: key) {

// 检查 SmartPublished 包装器类型
if let publishedType = T.self as? any SmartPublishedProtocol.Type,
let publishedValue = publishedType.createInstance(with: decoded) as? T {
return publishedValue
}
}
}
if let decoded = impl.cache.tranform(value: value, for: key) {
if let tTypeValue = decoded as? T {
return tTypeValue
} else if let publishedType = T.self as? any SmartPublishedProtocol.Type,
let publishedValue = publishedType.createInstance(with: decoded) as? T {
// // 检查 SmartPublished 包装器类型
return publishedValue
}
}
}



Expand Down
2 changes: 1 addition & 1 deletion SmartCodable/Classes/JSONDecoder/Decoder/KeysMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct KeysMapper {
type.mappingForKey()?.forEach { mapping in
for oldKey in mapping.from {
let newKey = mapping.to.stringValue
if let value = newDict[oldKey], !(value is NSNull) {
if let value = newDict[oldKey] as? JSONValue, value != .null {
newDict[newKey] = newDict[oldKey]
break
} else { // Handles the case of a custom parsing path.
Expand Down
43 changes: 43 additions & 0 deletions SmartCodable/Classes/Transformer/Transformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,46 @@ public protocol ValueTransformable {
public func <---(location: CodingKey, tranformer: any ValueTransformable) -> SmartValueTransformer {
SmartValueTransformer.init(location: location, tranformer: tranformer)
}



/** 便捷的Transformer
static func mappingForValue() -> [SmartValueTransformer]? {
[
CodingKeys.name <--- FastTransformer<String, String>(fromJSON: { json in
"abc"
}, toJSON: { object in
"123"
}),
CodingKeys.subModel <--- FastTransformer<TestEnum, String>(fromJSON: { json in
TestEnum.man
}, toJSON: { object in
object?.rawValue
}),
]
}
*/

public struct FastTransformer<Object, JSON>: ValueTransformable {

private let fromJSON: (JSON?) -> Object?
private let toJSON: ((Object?) -> JSON?)?


/// 便捷的转换器
/// - Parameters:
/// - fromJSON: json 转 object
/// - toJSON: object 转 json, 如果需要转json,可以不实现。
public init(fromJSON: @escaping (JSON?) -> Object?, toJSON: ((Object?) -> JSON?)? = nil) {
self.fromJSON = fromJSON
self.toJSON = toJSON
}

public func transformFromJSON(_ value: Any) -> Object? {
return fromJSON(value as? JSON)
}

public func transformToJSON(_ value: Object) -> JSON? {
return toJSON?(value)
}
}

0 comments on commit 0b6f0a5

Please sign in to comment.