forked from open-feature/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Flag metadata (open-feature#223)
Signed-off-by: Artyom Tonoyan <[email protected]>
- Loading branch information
1 parent
351d4af
commit 403c40f
Showing
6 changed files
with
369 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
|
||
#nullable enable | ||
namespace OpenFeature.Model; | ||
|
||
/// <summary> | ||
/// Represents the base class for metadata objects. | ||
/// </summary> | ||
public abstract class BaseMetadata | ||
{ | ||
private readonly ImmutableDictionary<string, object> _metadata; | ||
|
||
internal BaseMetadata(Dictionary<string, object> metadata) | ||
{ | ||
this._metadata = metadata.ToImmutableDictionary(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the boolean value associated with the specified key. | ||
/// </summary> | ||
/// <param name="key">The key of the value to retrieve.</param> | ||
/// <returns>The boolean value associated with the key, or null if the key is not found.</returns> | ||
public virtual bool? GetBool(string key) | ||
{ | ||
return this.GetValue<bool>(key); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the integer value associated with the specified key. | ||
/// </summary> | ||
/// <param name="key">The key of the value to retrieve.</param> | ||
/// <returns>The integer value associated with the key, or null if the key is not found.</returns> | ||
public virtual int? GetInt(string key) | ||
{ | ||
return this.GetValue<int>(key); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the double value associated with the specified key. | ||
/// </summary> | ||
/// <param name="key">The key of the value to retrieve.</param> | ||
/// <returns>The double value associated with the key, or null if the key is not found.</returns> | ||
public virtual double? GetDouble(string key) | ||
{ | ||
return this.GetValue<double>(key); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the string value associated with the specified key. | ||
/// </summary> | ||
/// <param name="key">The key of the value to retrieve.</param> | ||
/// <returns>The string value associated with the key, or null if the key is not found.</returns> | ||
public virtual string? GetString(string key) | ||
{ | ||
var hasValue = this._metadata.TryGetValue(key, out var value); | ||
if (!hasValue) | ||
{ | ||
return null; | ||
} | ||
|
||
return value as string ?? null; | ||
} | ||
|
||
private T? GetValue<T>(string key) where T : struct | ||
{ | ||
var hasValue = this._metadata.TryGetValue(key, out var value); | ||
if (!hasValue) | ||
{ | ||
return null; | ||
} | ||
|
||
return value is T tValue ? tValue : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
|
||
#nullable enable | ||
namespace OpenFeature.Model; | ||
|
||
/// <summary> | ||
/// Represents the metadata associated with a feature flag. | ||
/// </summary> | ||
/// <seealso href="https://github.com/open-feature/spec/blob/v0.7.0/specification/types.md#flag-metadata"/> | ||
public sealed class FlagMetadata : BaseMetadata | ||
{ | ||
/// <summary> | ||
/// Constructor for the <see cref="BaseMetadata"/> class. | ||
/// </summary> | ||
public FlagMetadata() : this([]) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Constructor for the <see cref="BaseMetadata"/> class. | ||
/// </summary> | ||
/// <param name="metadata">The dictionary containing the metadata.</param> | ||
public FlagMetadata(Dictionary<string, object> metadata) : base(metadata) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.