Skip to content

Commit 273dd87

Browse files
authored
Merge pull request #11368 from itaiferber/urlcomponents-codable-adoption
Adopt Codable on URLComponents
2 parents ca6f9f6 + bf4a125 commit 273dd87

File tree

2 files changed

+416
-266
lines changed

2 files changed

+416
-266
lines changed

stdlib/public/SDK/Foundation/URLComponents.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,42 @@ extension NSURLQueryItem : _HasCustomAnyHashableRepresentation {
471471
return AnyHashable(self as URLQueryItem)
472472
}
473473
}
474+
475+
extension URLComponents : Codable {
476+
private enum CodingKeys : Int, CodingKey {
477+
case scheme
478+
case user
479+
case password
480+
case host
481+
case port
482+
case path
483+
case query
484+
case fragment
485+
}
486+
487+
public init(from decoder: Decoder) throws {
488+
self.init()
489+
490+
let container = try decoder.container(keyedBy: CodingKeys.self)
491+
self.scheme = try container.decodeIfPresent(String.self, forKey: .scheme)
492+
self.user = try container.decodeIfPresent(String.self, forKey: .user)
493+
self.password = try container.decodeIfPresent(String.self, forKey: .password)
494+
self.host = try container.decodeIfPresent(String.self, forKey: .host)
495+
self.port = try container.decodeIfPresent(Int.self, forKey: .port)
496+
self.path = try container.decode(String.self, forKey: .path)
497+
self.query = try container.decodeIfPresent(String.self, forKey: .query)
498+
self.fragment = try container.decodeIfPresent(String.self, forKey: .fragment)
499+
}
500+
501+
public func encode(to encoder: Encoder) throws {
502+
var container = encoder.container(keyedBy: CodingKeys.self)
503+
try container.encodeIfPresent(self.scheme, forKey: .scheme)
504+
try container.encodeIfPresent(self.user, forKey: .user)
505+
try container.encodeIfPresent(self.password, forKey: .password)
506+
try container.encodeIfPresent(self.host, forKey: .host)
507+
try container.encodeIfPresent(self.port, forKey: .port)
508+
try container.encode(self.path, forKey: .path)
509+
try container.encodeIfPresent(self.query, forKey: .query)
510+
try container.encodeIfPresent(self.fragment, forKey: .fragment)
511+
}
512+
}

0 commit comments

Comments
 (0)