-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
8 changed files
with
198 additions
and
3 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,27 @@ | ||
// | ||
// Safe.swift | ||
// SwiftExtensions | ||
// | ||
// Created by Tatsuya Tanaka on 20171218. | ||
// Copyright © 2017年 tattn. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct Safe<Wrapped: Decodable>: Codable { | ||
public let value: Wrapped? | ||
|
||
public init(from decoder: Decoder) throws { | ||
do { | ||
let container = try decoder.singleValueContainer() | ||
self.value = try container.decode(Wrapped.self) | ||
} catch { | ||
self.value = nil | ||
} | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encodeNil() | ||
} | ||
} |
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,32 @@ | ||
// | ||
// CodableStringTo.swift | ||
// CodableExtensionPack | ||
// | ||
// Created by Tatsuya Tanaka on 20171030. | ||
// Copyright © 2017年 tattn. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct StringTo<T: LosslessStringConvertible>: Codable { | ||
public let value: T | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let stringValue = try container.decode(String.self) | ||
|
||
guard let value = T(stringValue) else { | ||
throw DecodingError.dataCorrupted( | ||
.init(codingPath: decoder.codingPath, | ||
debugDescription: "The string cannot cast to \(T.self).") | ||
) | ||
} | ||
|
||
self.value = value | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(value.description) | ||
} | ||
} |
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,61 @@ | ||
// | ||
// SafeTests.swift | ||
// CodableExtensionPackTests | ||
// | ||
// Created by Tatsuya Tanaka on 20171128. | ||
// Copyright © 2017年 tattn. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import SwiftExtensions | ||
|
||
class SafeTests: XCTestCase { | ||
|
||
override func setUp() { | ||
super.setUp() | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
} | ||
|
||
func testFailableArray() { | ||
let json = """ | ||
[ | ||
{"name": "Taro", "age": 20}, | ||
{"name": "Hanako", "age": "にゃーん"} | ||
] | ||
""".data(using: .utf8)! | ||
|
||
struct User: Decodable { | ||
let name: String | ||
let age: Int | ||
} | ||
|
||
let users = try! JSONDecoder().decode([Safe<User>].self, | ||
from: json) | ||
|
||
XCTAssertEqual(users[0].value?.name, "Taro") | ||
XCTAssertEqual(users[0].value?.age, 20) | ||
XCTAssertNil(users[1].value) | ||
} | ||
|
||
func testFailableURL() { | ||
let json = """ | ||
{"url": "https://foo.com", "url2": "invalid url string"} | ||
""".data(using: .utf8)! | ||
|
||
struct Model: Decodable { | ||
let url: Safe<URL> | ||
let url2: Safe<URL> | ||
} | ||
|
||
let model = try! JSONDecoder().decode(Model.self, | ||
from: json) | ||
|
||
XCTAssertEqual(model.url.value?.absoluteString, "https://foo.com") | ||
XCTAssertNil(model.url2.value) | ||
} | ||
|
||
} | ||
|
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,52 @@ | ||
// | ||
// CodableStringToTests.swift | ||
// CodableExtensionPackTests | ||
// | ||
// Created by Tatsuya Tanaka on 20171030. | ||
// Copyright © 2017年 tattn. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import SwiftExtensions | ||
|
||
class StringToTests: XCTestCase { | ||
|
||
// https://developer.yahoo.co.jp/webapi/shopping/shopping/v1/itemsearch.html | ||
let json = """ | ||
{ | ||
"int": "100", | ||
"customClass": "https://store.shopping.yahoo.co.jp/try3/4905524907230.html" | ||
} | ||
""" | ||
struct Root: Codable { | ||
let int: StringTo<Int> | ||
let customClass: StringTo<CustomClass> | ||
|
||
struct CustomClass: LosslessStringConvertible, Codable { | ||
var description: String | ||
|
||
init?(_ description: String) { | ||
self.description = description | ||
} | ||
} | ||
} | ||
|
||
override func setUp() { | ||
super.setUp() | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
} | ||
|
||
func testExample() { | ||
let data = json.data(using: .utf8)! | ||
let decoder = JSONDecoder() | ||
let root = try! decoder.decode(Root.self, from: data) | ||
XCTAssertEqual(root.int.value, 100) | ||
XCTAssertEqual(root.customClass.value.description, "https://store.shopping.yahoo.co.jp/try3/4905524907230.html") | ||
} | ||
|
||
} | ||
|