From cc910375fb0c3c86726a6b025993833ad242ada0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= <2493377+askpt@users.noreply.github.com> Date: Wed, 31 Jan 2024 16:06:54 +0000 Subject: [PATCH] Refactor FlagMetadata class to inherit from BaseMetadata MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com> --- src/OpenFeature/Model/BaseMetadata.cs | 57 +++++++++++++++++++++++++++ src/OpenFeature/Model/FlagMetadata.cs | 46 ++------------------- 2 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 src/OpenFeature/Model/BaseMetadata.cs diff --git a/src/OpenFeature/Model/BaseMetadata.cs b/src/OpenFeature/Model/BaseMetadata.cs new file mode 100644 index 00000000..1b27494a --- /dev/null +++ b/src/OpenFeature/Model/BaseMetadata.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; + +#nullable enable +namespace OpenFeature.Model; + +public abstract class BaseMetadata +{ + private readonly ImmutableDictionary _metadata; + + internal BaseMetadata(Dictionary metadata) + { + this._metadata = metadata.ToImmutableDictionary(); + } + + internal BaseMetadata() : this([]) + { + } + + public virtual bool? GetBool(string key) + { + return this.GetValue(key); + } + + public virtual int? GetInt(string key) + { + return this.GetValue(key); + } + + public virtual double? GetDouble(string key) + { + return this.GetValue(key); + } + + public virtual string? GetString(string key) + { + var hasValue = this._metadata.TryGetValue(key, out var value); + if (!hasValue) + { + return null; + } + + return value as string ?? throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(string)}"); + } + + private T? GetValue(string key) where T : struct + { + var hasValue = this._metadata.TryGetValue(key, out var value); + if (!hasValue) + { + return null; + } + + return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(T)}"); + } +} diff --git a/src/OpenFeature/Model/FlagMetadata.cs b/src/OpenFeature/Model/FlagMetadata.cs index 039829f2..d3dfe76b 100644 --- a/src/OpenFeature/Model/FlagMetadata.cs +++ b/src/OpenFeature/Model/FlagMetadata.cs @@ -5,53 +5,13 @@ #nullable enable namespace OpenFeature.Model; -public sealed class FlagMetadata +public sealed class FlagMetadata : BaseMetadata { - private readonly ImmutableDictionary _metadata; - - public FlagMetadata(Dictionary metadata) - { - this._metadata = metadata.ToImmutableDictionary(); - } - - public FlagMetadata() : this([]) - { - } - - public bool? GetBool(string key) - { - return this.GetValue(key); - } - - public int? GetInt(string key) + public FlagMetadata() { - return this.GetValue(key); } - public double? GetDouble(string key) + public FlagMetadata(Dictionary metadata) : base(metadata) { - return this.GetValue(key); - } - - public string? GetString(string key) - { - var hasValue = this._metadata.TryGetValue(key, out var value); - if (!hasValue) - { - return null; - } - - return value as string ?? throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(string)}"); - } - - private T? GetValue(string key) where T : struct - { - var hasValue = this._metadata.TryGetValue(key, out var value); - if (!hasValue) - { - return null; - } - - return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(T)}"); } }