-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ObjectMerger.swift | ||
// MoreCodable | ||
// | ||
// Created by Tatsuya Tanaka on 20180302. | ||
// Copyright © 2018年 tattn. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct ObjectMerger { | ||
private let encoder = DictionaryEncoder() | ||
private let decoder = DictionaryDecoder() | ||
|
||
public init() {} | ||
|
||
public func merge<T: Decodable, A: Encodable, B: Encodable>(_ type: T.Type = T.self, _ aObject: A, _ bObject: B) throws -> T { | ||
print(try encoder.encode(bObject)) | ||
let dictionary = try encoder.encode(aObject) | ||
.merging(try encoder.encode(bObject)) { left, _ in left } | ||
return try decoder.decode(T.self, from: dictionary) | ||
} | ||
|
||
public func merge<T: Decodable, A: Encodable, B: Encodable, C: Encodable>(_ type: T.Type = T.self, _ aObject: A, _ bObject: B, _ cObject: C) throws -> T { | ||
let dictionary = try encoder.encode(aObject) | ||
.merging(try encoder.encode(bObject)) { left, _ in left } | ||
.merging(try encoder.encode(cObject)) { left, _ in left } | ||
return try decoder.decode(T.self, from: dictionary) | ||
} | ||
|
||
public func merge<T: Decodable, A: Encodable, B: Encodable, C: Encodable, D: Encodable>(_ type: T.Type = T.self, _ aObject: A, _ bObject: B, _ cObject: C, _ dObject: D) throws -> T { | ||
let dictionary = try encoder.encode(aObject) | ||
.merging(try encoder.encode(bObject)) { left, _ in left } | ||
.merging(try encoder.encode(cObject)) { left, _ in left } | ||
.merging(try encoder.encode(dObject)) { left, _ in left } | ||
return try decoder.decode(T.self, from: dictionary) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// ObjectMergerTests.swift | ||
// MoreCodableTests | ||
// | ||
// Created by Tatsuya Tanaka on 20180302. | ||
// Copyright © 2018年 tattn. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import MoreCodable | ||
|
||
class ObjectMergerTests: XCTestCase { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
} | ||
|
||
func testSimpleCase() throws { | ||
struct APIResponse: Encodable { | ||
let id: Int | ||
let title: String | ||
let foo: String | ||
} | ||
|
||
struct APIResponse2: Encodable { | ||
let tags: [String] | ||
} | ||
|
||
struct Model: Decodable { | ||
let id: Int | ||
let title: String | ||
let tags: [String] | ||
} | ||
|
||
let response = APIResponse(id: 0, title: "Awesome article", foo: "bar") | ||
let response2 = APIResponse2(tags: ["swift", "ios", "macos"]) | ||
let model = try ObjectMerger().merge(Model.self, response, response2) | ||
|
||
XCTAssertEqual(model.id, response.id) | ||
XCTAssertEqual(model.title, response.title) | ||
XCTAssertEqual(model.tags, response2.tags) | ||
} | ||
} |