Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/OpenFeature/Model/ImmutableMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ public ImmutableMetadata(Dictionary<string, object> metadata)
/// <returns>The integer value associated with the key, or null if the key is not found.</returns>
public int? GetInt(string key)
{
return this.GetValue<int>(key);
var hasValue = this._metadata.TryGetValue(key, out var value);
if (!hasValue)
{
return null;
}

return value is double || value is int ? Convert.ToInt32(value) : null;
}

/// <summary>
Expand All @@ -55,7 +61,13 @@ public ImmutableMetadata(Dictionary<string, object> metadata)
/// <returns>The double value associated with the key, or null if the key is not found.</returns>
public double? GetDouble(string key)
{
return this.GetValue<double>(key);
var hasValue = this._metadata.TryGetValue(key, out var value);
if (!hasValue)
{
return null;
}

return value is double || value is int ? Convert.ToDouble(value) : null;
}

/// <summary>
Expand Down
45 changes: 45 additions & 0 deletions test/OpenFeature.Tests/ImmutableMetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ public void GetInt_Should_Return_Value_If_Key_Found()
Assert.NotNull(result);
Assert.Equal(1, result);
}
[Fact]
[Specification("1.4.14",
"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.")]
[Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")]
public void GetInt_Should_Return_Value_If_Key_Found_although_double()
{
// Arrange
var metadata = new Dictionary<string, object>
{
{
"intKey", 1.0
}
};
var flagMetadata = new ImmutableMetadata(metadata);

// Act
var result = flagMetadata.GetInt("intKey");

// Assert
Assert.NotNull(result);
Assert.Equal(1, result);
}

[Fact]
[Specification("1.4.14",
Expand Down Expand Up @@ -160,6 +182,29 @@ public void GetDouble_Should_Return_Value_If_Key_Found()
Assert.Equal(1.2, result);
}

[Fact]
[Specification("1.4.14",
"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.")]
[Specification("1.4.14.1", "Condition: `Flag metadata` MUST be immutable.")]
public void GetDouble_Should_Return_Value_If_Key_Found_Although_Int()
{
// Arrange
var metadata = new Dictionary<string, object>
{
{
"doubleKey", 1
}
};
var flagMetadata = new ImmutableMetadata(metadata);

// Act
var result = flagMetadata.GetDouble("doubleKey");

// Assert
Assert.NotNull(result);
Assert.Equal(1.0, result);
}

[Fact]
[Specification("1.4.14",
"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.")]
Expand Down