Skip to content

Commit dd4774f

Browse files
committed
Refactor FlagMetadata class to use ImmutableDictionary
This commit refactors the FlagMetadata class in the OpenFeature.Model namespace to use ImmutableDictionary instead of Dictionary for the _metadata field. This change ensures that the metadata dictionary is immutable, providing better thread safety and preventing unintended modifications. The GetString method is also moved to the bottom of the class for better organization. Signed-off-by: André Silva <[email protected]>
1 parent 7cc8e63 commit dd4774f

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/OpenFeature/Model/FlagMetadata.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,17 @@ namespace OpenFeature.Model;
77

88
public sealed class FlagMetadata
99
{
10-
private readonly Dictionary<string, object> _metadata;
10+
private readonly ImmutableDictionary<string, object> _metadata;
1111

1212
public FlagMetadata(Dictionary<string, object> metadata)
1313
{
14-
this._metadata = metadata;
14+
this._metadata = metadata.ToImmutableDictionary();
1515
}
1616

1717
public FlagMetadata() : this([])
1818
{
1919
}
2020

21-
public string? GetString(string key)
22-
{
23-
var hasValue = this._metadata.TryGetValue(key, out var value);
24-
if (!hasValue)
25-
{
26-
return null;
27-
}
28-
29-
return value is string tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(string)}");
30-
}
31-
3221
public bool? GetBool(string key)
3322
{
3423
return this.GetValue<bool>(key);
@@ -59,4 +48,15 @@ public FlagMetadata() : this([])
5948

6049
return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(T)}");
6150
}
51+
52+
public string? GetString(string key)
53+
{
54+
var hasValue = this._metadata.TryGetValue(key, out var value);
55+
if (!hasValue)
56+
{
57+
return null;
58+
}
59+
60+
return value is string tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(string)}");
61+
}
6262
}

0 commit comments

Comments
 (0)