Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Added support for special encoding of URLs #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sources/MessagePack/Box.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ extension Box where Value == Date {
try container.encode(self.value)
}
}

extension Box where Value == URL {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.init(try container.decode(Value.self))
}

func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.value)
}
}
3 changes: 3 additions & 0 deletions Sources/MessagePack/Decoder/MessagePackDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ final public class MessagePackDecoder {
case is Date.Type:
let box = try Box<Date>(from: decoder)
return box.value as! T
case is URL.Type:
let box = try Box<URL>(from: decoder)
return box.value as! T
default:
return try T(from: decoder)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/MessagePack/Decoder/SingleValueDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,23 @@ extension _MessagePackDecoder.SingleValueContainer: SingleValueDecodingContainer

return self.data.subdata(in: self.index..<self.index.advanced(by: length))
}


func decode(_ type: URL.Type) throws -> URL {
let urlStr = try decode(String.self)
guard let url = URL(string: urlStr) else {
throw DecodingError.dataCorruptedError(in: self, debugDescription: "Invalid URL: \(urlStr)")
}
return url
}

func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
switch type {
case is Data.Type:
return try decode(Data.self) as! T
case is Date.Type:
return try decode(Date.self) as! T
case is URL.Type:
return try decode(URL.self) as! T
default:
let decoder = _MessagePackDecoder(data: self.data)
let value = try T(from: decoder)
Expand Down
2 changes: 2 additions & 0 deletions Sources/MessagePack/Encoder/MessagePackEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final public class MessagePackEncoder {
try Box<Data>(data).encode(to: encoder)
case let date as Date:
try Box<Date>(date).encode(to: encoder)
case let url as URL:
try Box<URL>(url).encode(to: encoder)
default:
try value.encode(to: encoder)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,11 @@ extension _MessagePackEncoder.SingleValueContainer: SingleValueEncodingContainer
throw EncodingError.invalidValue(value, context)
}
}


func encode(_ value: URL) throws {
try encode(value.absoluteString)
}

func encode<T>(_ value: T) throws where T : Encodable {
try checkCanEncode(value: value)
defer { self.canEncodeNewValue = false }
Expand All @@ -249,6 +253,8 @@ extension _MessagePackEncoder.SingleValueContainer: SingleValueEncodingContainer
try self.encode(data)
case let date as Date:
try self.encode(date)
case let url as URL:
try self.encode(url)
default:
let encoder = _MessagePackEncoder()
try value.encode(to: encoder)
Expand Down
4 changes: 4 additions & 0 deletions Tests/MessagePackTests/Airport.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Foundation

struct Airport: Codable, Equatable {
let name: String
let iata: String
let icao: String
let coordinates: [Double]
let website: URL

struct Runway: Codable, Equatable {
enum Surface: String, Codable, Equatable {
Expand All @@ -25,6 +28,7 @@ struct Airport: Codable, Equatable {
icao: "KPDX",
coordinates: [-122.5975,
45.5886111111111],
website: URL(string: "https://www.flypdx.com")!,
runways: [
Airport.Runway(
direction: "3/21",
Expand Down
1 change: 1 addition & 0 deletions Tests/MessagePackTests/MessagePackRoundTripTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MessagePackRoundTripTests: XCTestCase {
XCTAssertEqual(value.icao, decoded.icao)
XCTAssertEqual(value.coordinates[0], decoded.coordinates[0], accuracy: 0.01)
XCTAssertEqual(value.coordinates[1], decoded.coordinates[1], accuracy: 0.01)
XCTAssertEqual(value.website, decoded.website)
XCTAssertEqual(value.runways[0].direction, decoded.runways[0].direction)
XCTAssertEqual(value.runways[0].distance, decoded.runways[0].distance)
XCTAssertEqual(value.runways[0].surface, decoded.runways[0].surface)
Expand Down