From 1502bd42d3f4b6c09f1f9025b31f9cb12b2d5b3b Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 10 Nov 2018 19:01:34 +0530 Subject: [PATCH] Model: TunnelConfiguration: Add explicit conformance to Decodable Signed-off-by: Roopesh Chander --- WireGuard/Shared/Model/Configuration.swift | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/WireGuard/Shared/Model/Configuration.swift b/WireGuard/Shared/Model/Configuration.swift index 3e661e6..45c29c1 100644 --- a/WireGuard/Shared/Model/Configuration.swift +++ b/WireGuard/Shared/Model/Configuration.swift @@ -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) + } +}