Model: TunnelConfiguration: Add explicit conformance to Decodable

This commit is contained in:
Roopesh Chander 2018-11-10 19:01:34 +05:30
parent 0db19f187b
commit ae627c5e5d
1 changed files with 15 additions and 1 deletions

View File

@ -4,7 +4,7 @@
import Foundation
@available(OSX 10.14, iOS 12.0, *)
class TunnelConfiguration: Codable {
final class TunnelConfiguration {
var interface: InterfaceConfiguration
let peers: [PeerConfiguration]
init(interface: InterfaceConfiguration, peers: [PeerConfiguration]) {
@ -55,3 +55,17 @@ struct PeerConfiguration: Codable {
if (publicKey.count != 32) { fatalError("Invalid public key") }
}
}
extension TunnelConfiguration: Encodable { }
extension TunnelConfiguration: Decodable {
enum CodingKeys: CodingKey {
case interface
case peers
}
convenience init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let interface = try values.decode(InterfaceConfiguration.self, forKey: .interface)
let peers = try values.decode([PeerConfiguration].self, forKey: .peers)
self.init(interface: interface, peers: peers)
}
}