Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
tattn committed Dec 11, 2018
1 parent d43014f commit b7b691d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Sources/URLQueryItemsDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ extension URLQueryItemsDecoder {
.unwrapOrThrow(error: decoder.notFound(key: key))
}

private func isNill(forKey key: CodingKey) throws -> Bool {
return container.first(where: { $0.name == key.stringValue }) == nil
private func contains(_ key: CodingKey) -> Bool {
return container.contains(where: { $0.name == key.stringValue })
}

func _decode<T: Decodable & LosslessStringConvertible>(_ type: T.Type, forKey key: Key) throws -> T {
Expand All @@ -112,7 +112,7 @@ extension URLQueryItemsDecoder {
return try decoder.unbox(value, as: T.self)
}

func decodeNil(forKey key: Key) throws -> Bool { return try isNill(forKey: key) }
func decodeNil(forKey key: Key) throws -> Bool { return !contains(key) }
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { return try _decode(type, forKey: key) }
func decode(_ type: Int.Type, forKey key: Key) throws -> Int { return try _decode(type, forKey: key) }
func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { return try _decode(type, forKey: key) }
Expand Down
9 changes: 3 additions & 6 deletions Tests/URLQueryItemsDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ class URLQueryItemsDecoderTests: XCTestCase {
}
let params: [URLQueryItem] = [
URLQueryItem(name: "string", value: "abc"),
URLQueryItem(name: "int", value: "123"),
URLQueryItem(name: "double", value: Double.pi.description)
]
let parameter = try decoder.decode(Parameter.self, from: params)

XCTAssertEqual(parameter.string, params[0].value)
XCTAssertEqual(parameter.int?.description, params[1].value)
XCTAssertEqual(parameter.double?.description, params[2].value)
XCTAssertEqual(parameter.int?.description, nil)
XCTAssertEqual(parameter.double?.description, params[1].value)
}

func testDecodeEmptyOptionalParameter() throws {
Expand All @@ -65,9 +64,7 @@ class URLQueryItemsDecoderTests: XCTestCase {
let int: Int?
let double: Double?
}
let params: [URLQueryItem] = [
]
let parameter = try decoder.decode(Parameter.self, from: params)
let parameter = try decoder.decode(Parameter.self, from: [])

XCTAssertEqual(parameter.string, nil)
XCTAssertEqual(parameter.int, nil)
Expand Down

0 comments on commit b7b691d

Please sign in to comment.