Skip to content

Commit

Permalink
Release 1.8.1 - Encodable+toDictionary() logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Burdzicki committed Feb 28, 2023
1 parent 0b152aa commit 5317360
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CombineNetworking.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "CombineNetworking"
spec.version = "1.8.0"
spec.version = "1.8.1"
spec.summary = "Easy approach on Networking using Combine"

# This description is used to generate tags and improve search results.
Expand Down
23 changes: 21 additions & 2 deletions Sources/CombineNetworking/Data/Encodable + extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ extension Encodable {
let mirror = Mirror(reflecting: self)

mirror.children.filter { !$0.label.isNilOrEmpty }.forEach { child in
if let value = valueOrNil(child.value) {
output[child.label!] = value
guard let value = valueOrNil(child.value) else { return }

if let array = value as? Array<Any> {
guard array.count > 0 else { return }
output[child.label!] = array.compactMap { arrayValue -> Any? in
guard let value = valueOrNil(arrayValue) else { return nil }
return rawValueIfNeeded(value)
}
} else if let dict = value as? Dictionary<String, Any> {
guard Array(dict.keys).count > 0 else { return }
output[child.label!] = dict.filter { _, value in
valueOrNil(value) != nil
}
} else {
output[child.label!] = rawValueIfNeeded(value)
}
}

Expand All @@ -35,6 +48,12 @@ extension Encodable {
return value
}
}

private func rawValueIfNeeded(_ value: Any) -> Any {
let mirror = Mirror(reflecting: value)
guard mirror.displayStyle == .enum, let value = value as? any RawRepresentable else { return value }
return value.rawValue
}
}

fileprivate extension Optional where Wrapped: Collection {
Expand Down
30 changes: 30 additions & 0 deletions Tests/CombineNetworkingTests/CombineNetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,34 @@ final class CombineNetworkingTests: XCTestCase {

XCTAssertFalse(result)
}

func testToDictionaryWithEmptyArray() throws {
let model = TestParamsModelWithArray(name: "First", lastname: "Last", age: 24, array: [])
XCTAssertFalse(model.toDictionary().contains { $0.key == "array" })
}

func testToDictionaryWithArray() throws {
let model = TestParamsModelWithArray(name: "First", lastname: "Last", age: 24, array: ["testValue"])
XCTAssertTrue(model.toDictionary().contains { $0.key == "array" })
}

func testToDictionaryWithEmptyDict() throws {
let model = TestParamsModelWithDict(name: "First", lastname: "Last", age: 24, dict: [:])
XCTAssertFalse(model.toDictionary().contains { $0.key == "dict" })
}

func testToDictionaryWithDict() throws {
let model = TestParamsModelWithDict(name: "First", lastname: "Last", age: 24, dict: ["testKey": "testValue"])
XCTAssertTrue(model.toDictionary().contains { $0.key == "dict" })
}

func testToDictionaryWithEmptyEnum() throws {
let model = TestParamsModelWithEnum(name: "First", lastname: "Last", age: 24, sex: nil)
XCTAssertFalse(model.toDictionary().contains { $0.key == "sex" })
}

func testToDictionaryWithEnum() throws {
let model = TestParamsModelWithEnum(name: "First", lastname: "Last", age: 24, sex: .male)
XCTAssertTrue(model.toDictionary().contains { ($0.value as? String) == "male" })
}
}
25 changes: 25 additions & 0 deletions Tests/CombineNetworkingTests/TestParamsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,28 @@ struct TestParamsModel: Codable {
let lastname: String
let age: Int?
}

struct TestParamsModelWithArray: Codable {
let name: String
let lastname: String
let age: Int?
let array: [String]
}

struct TestParamsModelWithDict: Codable {
let name: String
let lastname: String
let age: Int?
let dict: [String: String]
}

struct TestParamsModelWithEnum: Codable {
let name: String
let lastname: String
let age: Int?
let sex: Sex?

enum Sex: String, Codable {
case male, female
}
}

0 comments on commit 5317360

Please sign in to comment.