Skip to content

Commit b456089

Browse files
committed
feat: Support retrieving numeric metadata as either either integers or decimals #443
Signed-off-by: Otto Lagerquist <[email protected]> Signed-off-by: lager95 <[email protected]>
1 parent e18ad50 commit b456089

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/OpenFeature/Model/ImmutableMetadata.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ public ImmutableMetadata(Dictionary<string, object> metadata)
4545
/// <returns>The integer value associated with the key, or null if the key is not found.</returns>
4646
public int? GetInt(string key)
4747
{
48-
return this.GetValue<int>(key);
48+
49+
var hasValue = this._metadata.TryGetValue(key, out var value);
50+
if (!hasValue)
51+
{
52+
return null;
53+
}
54+
55+
return value is double || value is int ? Convert.ToInt32(value) : null;
4956
}
5057

5158
/// <summary>
@@ -55,7 +62,13 @@ public ImmutableMetadata(Dictionary<string, object> metadata)
5562
/// <returns>The double value associated with the key, or null if the key is not found.</returns>
5663
public double? GetDouble(string key)
5764
{
58-
return this.GetValue<double>(key);
65+
var hasValue = this._metadata.TryGetValue(key, out var value);
66+
if (!hasValue)
67+
{
68+
return null;
69+
}
70+
71+
return value is double || value is int ? Convert.ToDouble(value) : null;
5972
}
6073

6174
/// <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)