From c1ae3324612d1f23ba45401585c5fe4dd15b7cb5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Silva?=
<2493377+askpt@users.noreply.github.com>
Date: Fri, 2 Feb 2024 14:55:37 +0000
Subject: [PATCH] Added some conditions.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: André Silva <2493377+askpt@users.noreply.github.com>
---
src/OpenFeature/Model/BaseMetadata.cs | 5 ++---
test/OpenFeature.Tests/FlagMetadataTest.cs | 25 ++++++++++++++++++++++
2 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/src/OpenFeature/Model/BaseMetadata.cs b/src/OpenFeature/Model/BaseMetadata.cs
index 039253d2..dd4653b2 100644
--- a/src/OpenFeature/Model/BaseMetadata.cs
+++ b/src/OpenFeature/Model/BaseMetadata.cs
@@ -56,7 +56,6 @@ internal BaseMetadata() : this([])
///
/// The key of the value to retrieve.
/// The string value associated with the key, or null if the key is not found.
- /// Thrown when the value cannot be cast to a string.
public virtual string? GetString(string key)
{
var hasValue = this._metadata.TryGetValue(key, out var value);
@@ -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(string key) where T : struct
@@ -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;
}
}
diff --git a/test/OpenFeature.Tests/FlagMetadataTest.cs b/test/OpenFeature.Tests/FlagMetadataTest.cs
index 5c34d2e2..78c41c7c 100644
--- a/test/OpenFeature.Tests/FlagMetadataTest.cs
+++ b/test/OpenFeature.Tests/FlagMetadataTest.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using OpenFeature.Model;
+using OpenFeature.Tests.Internal;
using Xunit;
#nullable enable
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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