@@ -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}
0 commit comments