Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #25 from onuniverse/bug/urlSupport
Browse files Browse the repository at this point in the history
Add support for Codable types with URL properties.
  • Loading branch information
tanner0101 authored Feb 26, 2020
2 parents 44999b4 + db0f6b2 commit 20f68fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Sources/URLEncodedForm/Data/URLEncodedFormData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ enum URLEncodedFormData: NestedData, ExpressibleByArrayLiteral, ExpressibleByStr
}
}

/// Converts self to an `URL` or returns `nil` if not convertible.
var url: URL? {
switch self {
case .str(let s): return URL(string: s)
default: return nil
}
}

/// Converts self to an `[URLEncodedFormData]` or returns `nil` if not convertible.
var array: [URLEncodedFormData]? {
switch self {
Expand Down
16 changes: 16 additions & 0 deletions Sources/URLEncodedForm/Data/URLEncodedFormDataConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ extension String: URLEncodedFormDataConvertible {
}
}

extension URL: URLEncodedFormDataConvertible {
/// See `URLEncodedFormDataConvertible`.
func convertToURLEncodedFormData() throws -> URLEncodedFormData {
return .str(self.absoluteString)
}

/// See `URLEncodedFormDataConvertible`.
static func convertFromURLEncodedFormData(_ data: URLEncodedFormData) throws -> URL {
guard let url = data.url else {
throw URLEncodedFormError(identifier: "url", reason: "Could not convert to `URL`: \(data)")
}

return url
}
}

extension FixedWidthInteger {
/// See `URLEncodedFormDataConvertible`.
func convertToURLEncodedFormData() throws -> URLEncodedFormData {
Expand Down
9 changes: 6 additions & 3 deletions Tests/URLEncodedFormTests/URLEncodedFormCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest
class URLEncodedFormCodableTests: XCTestCase {
func testDecode() throws {
let data = """
name=Tanner&age=23&pets[]=Zizek&pets[]=Foo&dict[a]=1&dict[b]=2&foos[]=baz&nums[]=3.14
name=Tanner&age=23&pets[]=Zizek&pets[]=Foo&dict[a]=1&dict[b]=2&foos[]=baz&nums[]=3.14&url=https%3A%2F%2Fvapor.codes
""".data(using: .utf8)!

let user = try URLEncodedFormDecoder().decode(User.self, from: data)
Expand All @@ -17,10 +17,11 @@ class URLEncodedFormCodableTests: XCTestCase {
XCTAssertEqual(user.dict["b"], 2)
XCTAssertEqual(user.foos[0], .baz)
XCTAssertEqual(user.nums[0], 3.14)
XCTAssertEqual(user.url, URL(string: "https://vapor.codes"))
}

func testEncode() throws {
let user = User(name: "Tanner", age: 23, pets: ["Zizek", "Foo"], dict: ["a": 1, "b": 2], foos: [.baz], nums: [3.14])
let user = User(name: "Tanner", age: 23, pets: ["Zizek", "Foo"], dict: ["a": 1, "b": 2], foos: [.baz], nums: [3.14], url: URL(string: "https://vapor.codes")!)
let data = try URLEncodedFormEncoder().encode(user)
let result = String(data: data, encoding: .utf8)!
XCTAssert(result.contains("pets[]=Zizek"))
Expand All @@ -31,10 +32,11 @@ class URLEncodedFormCodableTests: XCTestCase {
XCTAssert(result.contains("dict[b]=2"))
XCTAssert(result.contains("foos[]=baz"))
XCTAssert(result.contains("nums[]=3.14"))
XCTAssert(result.contains("url=https://vapor.codes"))
}

func testCodable() throws {
let a = User(name: "Tanner", age: 23, pets: ["Zizek", "Foo"], dict: ["a": 1, "b": 2], foos: [], nums: [])
let a = User(name: "Tanner", age: 23, pets: ["Zizek", "Foo"], dict: ["a": 1, "b": 2], foos: [], nums: [], url: URL(string: "https://vapor.codes")!)
let body = try URLEncodedFormEncoder().encode(a)
print(String(data: body, encoding: .utf8)!)
let b = try URLEncodedFormDecoder().decode(User.self, from: body)
Expand Down Expand Up @@ -111,6 +113,7 @@ struct User: Codable, Equatable {
var dict: [String: Int]
var foos: [Foo]
var nums: [Decimal]
var url: URL
}

enum Foo: String, Codable {
Expand Down

0 comments on commit 20f68fb

Please sign in to comment.