Skip to content

Commit 96a7b48

Browse files
committed
add decoder/encoder to Attachment and NetworkStatus
1 parent 7476743 commit 96a7b48

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Sources/ContainerResource/Network/Attachment.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,49 @@ public struct Attachment: Codable, Sendable {
4747
self.ipv6Address = ipv6Address
4848
self.macAddress = macAddress
4949
}
50+
51+
enum CodingKeys: String, CodingKey {
52+
case network
53+
case hostname
54+
case ipv4Address
55+
case ipv4Gateway
56+
case ipv6Address
57+
case macAddress
58+
// TODO: retain for deserialization compatability for now, remove later
59+
case address
60+
case gateway
61+
}
62+
63+
/// Create a configuration from the supplied Decoder, initializing missing
64+
/// values where possible to reasonable defaults.
65+
public init(from decoder: Decoder) throws {
66+
let container = try decoder.container(keyedBy: CodingKeys.self)
67+
68+
network = try container.decode(String.self, forKey: .network)
69+
hostname = try container.decode(String.self, forKey: .hostname)
70+
if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Address) {
71+
ipv4Address = address
72+
} else {
73+
ipv4Address = try container.decode(CIDRv4.self, forKey: .address)
74+
}
75+
if let gateway = try? container.decode(IPv4Address.self, forKey: .ipv4Gateway) {
76+
ipv4Gateway = gateway
77+
} else {
78+
ipv4Gateway = try container.decode(IPv4Address.self, forKey: .gateway)
79+
}
80+
ipv6Address = try container.decodeIfPresent(CIDRv6.self, forKey: .ipv6Address)
81+
macAddress = try container.decodeIfPresent(MACAddress.self, forKey: .macAddress)
82+
}
83+
84+
/// Encode the configuration to the supplied Encoder.
85+
public func encode(to encoder: Encoder) throws {
86+
var container = encoder.container(keyedBy: CodingKeys.self)
87+
88+
try container.encode(network, forKey: .network)
89+
try container.encode(hostname, forKey: .hostname)
90+
try container.encode(ipv4Address, forKey: .ipv4Address)
91+
try container.encode(ipv4Gateway, forKey: .ipv4Gateway)
92+
try container.encodeIfPresent(ipv6Address, forKey: .ipv6Address)
93+
try container.encodeIfPresent(macAddress, forKey: .macAddress)
94+
}
5095
}

Sources/ContainerResource/Network/NetworkState.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@ public struct NetworkStatus: Codable, Sendable {
3939
self.ipv4Gateway = ipv4Gateway
4040
self.ipv6Subnet = ipv6Subnet
4141
}
42+
43+
enum CodingKeys: String, CodingKey {
44+
case ipv4Subnet
45+
case ipv4Gateway
46+
case ipv6Subnet
47+
// TODO: retain for deserialization compatability for now, remove later
48+
case address
49+
case gateway
50+
}
51+
52+
/// Create a configuration from the supplied Decoder, initializing missing
53+
/// values where possible to reasonable defaults.
54+
public init(from decoder: Decoder) throws {
55+
let container = try decoder.container(keyedBy: CodingKeys.self)
56+
57+
if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Subnet) {
58+
ipv4Subnet = address
59+
} else {
60+
ipv4Subnet = try container.decode(CIDRv4.self, forKey: .address)
61+
}
62+
if let gateway = try? container.decode(IPv4Address.self, forKey: .ipv4Gateway) {
63+
ipv4Gateway = gateway
64+
} else {
65+
ipv4Gateway = try container.decode(IPv4Address.self, forKey: .gateway)
66+
}
67+
ipv6Subnet = try container.decodeIfPresent(String.self, forKey: .ipv6Subnet)
68+
.map { try CIDRv6($0) }
69+
}
70+
71+
/// Encode the configuration to the supplied Encoder.
72+
public func encode(to encoder: Encoder) throws {
73+
var container = encoder.container(keyedBy: CodingKeys.self)
74+
75+
try container.encode(ipv4Subnet, forKey: .ipv4Subnet)
76+
try container.encode(ipv4Gateway, forKey: .ipv4Gateway)
77+
try container.encodeIfPresent(ipv6Subnet, forKey: .ipv6Subnet)
78+
}
4279
}
4380

4481
/// The configuration and runtime attributes for a network.

0 commit comments

Comments
 (0)