mirror of
https://github.com/apple/container.git
synced 2026-10-14 18:19:17 +00:00
Use string representation for CIDR and IPAddress types (#458)
This change adds extension to the CIDR and IPAddress types to implement custom encode/decode functions for `Codable` conformance to use their string representation as the output from encode and input to decode. This would make the output from encoding this type (e.g. JSON) more human-readable rather than using the internal integer representation.
This commit is contained in:
1 parent
528f635521
commit
dcbc7bf71d
7 files changed
+181
-4
No files matched your search
@@ -16,7 +16,7 @@
|
||||
|
||||
/// Describes an IPv4 CIDR address block.
|
||||
@frozen
|
||||
public struct CIDRv4: CustomStringConvertible, Equatable, Sendable, Hashable, Codable {
|
||||
public struct CIDRv4: CustomStringConvertible, Equatable, Sendable, Hashable {
|
||||
/// The IP component of this CIDR address.
|
||||
public let address: IPv4Address
|
||||
|
||||
@@ -99,3 +99,16 @@ public struct CIDRv4: CustomStringConvertible, Equatable, Sendable, Hashable, Co
|
||||
"\(address)/\(prefix)"
|
||||
}
|
||||
}
|
||||
|
||||
extension CIDRv4: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let string = try container.decode(String.self)
|
||||
try self.init(string)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(description)
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
/// Describes an IPv4 or IPv6 CIDR address block.
|
||||
@frozen
|
||||
public struct CIDRv6: CustomStringConvertible, Equatable, Sendable, Hashable, Codable {
|
||||
public struct CIDRv6: CustomStringConvertible, Equatable, Sendable, Hashable {
|
||||
|
||||
/// The IP component of this CIDR address.
|
||||
public let address: IPv6Address
|
||||
@@ -100,3 +100,16 @@ public struct CIDRv6: CustomStringConvertible, Equatable, Sendable, Hashable, Co
|
||||
"\(address)/\(prefix)"
|
||||
}
|
||||
}
|
||||
|
||||
extension CIDRv6: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let string = try container.decode(String.self)
|
||||
try self.init(string)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(description)
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@frozen
|
||||
public struct IPv4Address: Sendable, Hashable, CustomStringConvertible, Equatable, Comparable, Codable {
|
||||
public struct IPv4Address: Sendable, Hashable, CustomStringConvertible, Equatable, Comparable {
|
||||
public let value: UInt32
|
||||
|
||||
@inlinable
|
||||
@@ -215,3 +215,16 @@ public struct IPv4Address: Sendable, Hashable, CustomStringConvertible, Equatabl
|
||||
lhs.value < rhs.value
|
||||
}
|
||||
}
|
||||
|
||||
extension IPv4Address: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let string = try container.decode(String.self)
|
||||
try self.init(string)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(description)
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// Represents an IPv6 network address conforming to RFC 5952 and RFC 4291.
|
||||
public struct IPv6Address: Sendable, Hashable, CustomStringConvertible, Equatable, Comparable, Codable {
|
||||
public struct IPv6Address: Sendable, Hashable, CustomStringConvertible, Equatable, Comparable {
|
||||
public let value: UInt128
|
||||
|
||||
public let zone: String?
|
||||
@@ -252,3 +252,16 @@ public struct IPv6Address: Sendable, Hashable, CustomStringConvertible, Equatabl
|
||||
return (lhs.zone ?? "") < (rhs.zone ?? "")
|
||||
}
|
||||
}
|
||||
|
||||
extension IPv6Address: Codable {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let string = try container.decode(String.self)
|
||||
try self.init(string)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(description)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user