Skip to content

Commit

Permalink
Added some conditions.
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <[email protected]>
  • Loading branch information
askpt committed Feb 2, 2024
1 parent cdb8a86 commit c1ae332
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/OpenFeature/Model/BaseMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ internal BaseMetadata() : this([])
/// </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>
/// <exception cref="InvalidCastException">Thrown when the value cannot be cast to a string.</exception>
public virtual string? GetString(string key)
{
var hasValue = this._metadata.TryGetValue(key, out var value);
Expand All @@ -65,7 +64,7 @@ internal BaseMetadata() : this([])
return null;
}

return value as string ?? throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(string)}");
return value as string ?? null;
}

private T? GetValue<T>(string key) where T : struct
Expand All @@ -76,6 +75,6 @@ internal BaseMetadata() : this([])
return null;
}

return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType()} to {typeof(T)}");
return value is T tValue ? tValue : null;
}
}
25 changes: 25 additions & 0 deletions test/OpenFeature.Tests/FlagMetadataTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using OpenFeature.Model;
using OpenFeature.Tests.Internal;
using Xunit;

#nullable enable
Expand All @@ -9,6 +10,8 @@ namespace OpenFeature.Tests;
public class FlagMetadataTest
{
[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.")]
public void GetBool_Should_Return_Null_If_Key_Not_Found()
{
// Arrange
Expand All @@ -22,6 +25,8 @@ public void GetBool_Should_Return_Null_If_Key_Not_Found()
}

[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.")]
public void GetBool_Should_Return_Value_If_Key_Found()
{
// Arrange
Expand All @@ -41,6 +46,8 @@ public void GetBool_Should_Return_Value_If_Key_Found()
}

[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.")]
public void GetBool_Should_Throw_Value_Is_Invalid()
{
// Arrange
Expand All @@ -61,6 +68,8 @@ public void GetBool_Should_Throw_Value_Is_Invalid()
}

[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.")]
public void GetInt_Should_Return_Null_If_Key_Not_Found()
{
// Arrange
Expand All @@ -74,6 +83,8 @@ public void GetInt_Should_Return_Null_If_Key_Not_Found()
}

[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.")]
public void GetInt_Should_Return_Value_If_Key_Found()
{
// Arrange
Expand All @@ -94,6 +105,8 @@ public void GetInt_Should_Return_Value_If_Key_Found()
}

[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.")]
public void GetInt_Should_Throw_Value_Is_Invalid()
{
// Arrange
Expand All @@ -114,6 +127,8 @@ public void GetInt_Should_Throw_Value_Is_Invalid()
}

[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.")]
public void GetDouble_Should_Return_Null_If_Key_Not_Found()
{
// Arrange
Expand All @@ -127,6 +142,8 @@ public void GetDouble_Should_Return_Null_If_Key_Not_Found()
}

[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.")]
public void GetDouble_Should_Return_Value_If_Key_Found()
{
// Arrange
Expand All @@ -147,6 +164,8 @@ public void GetDouble_Should_Return_Value_If_Key_Found()
}

[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.")]
public void GetDouble_Should_Throw_Value_Is_Invalid()
{
// Arrange
Expand All @@ -167,6 +186,8 @@ public void GetDouble_Should_Throw_Value_Is_Invalid()
}

[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.")]
public void GetString_Should_Return_Null_If_Key_Not_Found()
{
// Arrange
Expand All @@ -180,6 +201,8 @@ public void GetString_Should_Return_Null_If_Key_Not_Found()
}

[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.")]
public void GetString_Should_Return_Value_If_Key_Found()
{
// Arrange
Expand All @@ -200,6 +223,8 @@ public void GetString_Should_Return_Value_If_Key_Found()
}

[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.")]
public void GetString_Should_Throw_Value_Is_Invalid()
{
// Arrange
Expand Down

0 comments on commit c1ae332

Please sign in to comment.