From d64e445a798f764ae3a4f19d361b2267e69a3f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Mon, 9 Dec 2024 18:59:25 +0100 Subject: [PATCH] Added methods to return strong typed data/settings from a deserialized DnsRecord --- .../Cloudflare.Zones/Models/DnsRecord.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/Extensions/Cloudflare.Zones/Models/DnsRecord.cs b/Extensions/Cloudflare.Zones/Models/DnsRecord.cs index 9c8f9bc..ff2b9a2 100644 --- a/Extensions/Cloudflare.Zones/Models/DnsRecord.cs +++ b/Extensions/Cloudflare.Zones/Models/DnsRecord.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Runtime.Serialization; +using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; namespace AMWD.Net.Api.Cloudflare.Zones @@ -132,6 +134,48 @@ namespace AMWD.Net.Api.Cloudflare.Zones #region Type definitions for data fields + /// + /// Returns the data strong typed (as might be from deserialization). + /// + [ExcludeFromCodeCoverage] + public object? GetTypedData() + { + return Type switch + { + DnsRecordType.Caa => GetTypedData(), + DnsRecordType.Cert => GetTypedData(), + DnsRecordType.DnsKey => GetTypedData(), + DnsRecordType.Ds => GetTypedData(), + DnsRecordType.Https => GetTypedData(), + DnsRecordType.Loc => GetTypedData(), + DnsRecordType.NaPtr => GetTypedData(), + DnsRecordType.SMimeA => GetTypedData(), + DnsRecordType.Srv => GetTypedData(), + DnsRecordType.SshFp => GetTypedData(), + DnsRecordType.SvcB => GetTypedData(), + DnsRecordType.TlsA => GetTypedData(), + DnsRecordType.Uri => GetTypedData(), + _ => null + }; + } + + /// + /// Returns the data strong typed (as might be from deserialization). + /// + /// The type of the data. + [ExcludeFromCodeCoverage] + public T? GetTypedData() + where T : class + { + if (Data == null) + return default; + + if (Data is JObject jo) + return jo.ToObject(); + + return (T)Data; + } + /// /// Components of a CAA record. /// @@ -610,6 +654,36 @@ namespace AMWD.Net.Api.Cloudflare.Zones #region Type definitions for settings fields + /// + /// Returns the settings strong typed (as might be from deserialization). + /// + [ExcludeFromCodeCoverage] + public object? GetTypedSettings() + { + return Type switch + { + DnsRecordType.Cname => GetTypedSettings(), + _ => null + }; + } + + /// + /// Returns the settings strong typed (as might be from deserialization). + /// + /// The type of the settings. + [ExcludeFromCodeCoverage] + public T? GetTypedSettings() + where T : class + { + if (Settings == null) + return default; + + if (Settings is JObject jo) + return jo.ToObject(); + + return (T)Settings; + } + /// /// Settings for the DNS record. ///