Skip to content

Commit 12de5f1

Browse files
lager95beeme1mrtoddbaert
authored
feat: Support retrieving numeric metadata as either integers or decimals (#490)
* feat: Support retrieving numeric metadata as either either integers or decimals #443 Signed-off-by: Otto Lagerquist <otto_nr26@hotmail.com> Signed-off-by: lager95 <33402278+lager95@users.noreply.github.com> * Update ImmutableMetadata.cs Updated document comments to reflect the conversions between int and double. Signed-off-by: lager95 <33402278+lager95@users.noreply.github.com> --------- Signed-off-by: Otto Lagerquist <otto_nr26@hotmail.com> Signed-off-by: lager95 <33402278+lager95@users.noreply.github.com> Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com> Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
1 parent ce7baa7 commit 12de5f1

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/OpenFeature/Model/ImmutableMetadata.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,32 @@ public ImmutableMetadata(Dictionary<string, object> metadata)
4242
/// Gets the integer value associated with the specified key.
4343
/// </summary>
4444
/// <param name="key">The key of the value to retrieve.</param>
45-
/// <returns>The integer value associated with the key, or null if the key is not found.</returns>
45+
/// <returns>The value associated with the key as an integer, if it is of type double or int; otherwise, null.</returns>
4646
public int? GetInt(string key)
4747
{
48-
return this.GetValue<int>(key);
48+
var hasValue = this._metadata.TryGetValue(key, out var value);
49+
if (!hasValue)
50+
{
51+
return null;
52+
}
53+
54+
return value is double || value is int ? Convert.ToInt32(value) : null;
4955
}
5056

5157
/// <summary>
5258
/// Gets the double value associated with the specified key.
5359
/// </summary>
5460
/// <param name="key">The key of the value to retrieve.</param>
55-
/// <returns>The double value associated with the key, or null if the key is not found.</returns>
61+
/// <returns>The value associated with the key as a double, if it is of type double or int; otherwise, null.</returns>
5662
public double? GetDouble(string key)
5763
{
58-
return this.GetValue<double>(key);
64+
var hasValue = this._metadata.TryGetValue(key, out var value);
65+
if (!hasValue)
66+
{
67+
return null;
68+
}
69+
70+
return value is double || value is int ? Convert.ToDouble(value) : null;
5971
}
6072

6173
/// <summary>

test/OpenFeature.Tests/ImmutableMetadataTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ public void GetInt_Should_Return_Value_If_Key_Found()
100100
Assert.NotNull(result);
101101
Assert.Equal(1, result);
102102
}
103+
[Fact]
104+
[Specification("1.4.14",
105+
"If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")]
106+
[Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")]
107+
public void GetInt_Should_Return_Value_If_Key_Found_although_double()
108+
{
109+
// Arrange
110+
var metadata = new Dictionary<string, object>
111+
{
112+
{
113+
"intKey", 1.0
114+
}
115+
};
116+
var flagMetadata = new ImmutableMetadata(metadata);
117+
118+
// Act
119+
var result = flagMetadata.GetInt("intKey");
120+
121+
// Assert
122+
Assert.NotNull(result);
123+
Assert.Equal(1, result);
124+
}
103125

104126
[Fact]
105127
[Specification("1.4.14",
@@ -160,6 +182,29 @@ public void GetDouble_Should_Return_Value_If_Key_Found()
160182
Assert.Equal(1.2, result);
161183
}
162184

185+
[Fact]
186+
[Specification("1.4.14",
187+
"If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")]
188+
[Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")]
189+
public void GetDouble_Should_Return_Value_If_Key_Found_Although_Int()
190+
{
191+
// Arrange
192+
var metadata = new Dictionary<string, object>
193+
{
194+
{
195+
"doubleKey", 1
196+
}
197+
};
198+
var flagMetadata = new ImmutableMetadata(metadata);
199+
200+
// Act
201+
var result = flagMetadata.GetDouble("doubleKey");
202+
203+
// Assert
204+
Assert.NotNull(result);
205+
Assert.Equal(1.0, result);
206+
}
207+
163208
[Fact]
164209
[Specification("1.4.14",
165210
"If the `flag metadata` field in the `flag resolution` structure returned by the configured `provider` is set, the `evaluation details` structure's `flag metadata` field MUST contain that value. Otherwise, it MUST contain an empty record.")]

0 commit comments

Comments
 (0)