Skip to content

Commit 6b71ef5

Browse files
committed
Moved code and fixed code smells.
1 parent ab17ee9 commit 6b71ef5

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/OpenFeature/Model/FlagMetadata.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ public FlagMetadata() : this([])
3333
return this.GetValue<double>(key);
3434
}
3535

36-
private T? GetValue<T>(string key) where T : struct
36+
public string? GetString(string key)
3737
{
3838
var hasValue = this._metadata.TryGetValue(key, out var value);
3939
if (!hasValue)
4040
{
4141
return null;
4242
}
4343

44-
return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(T)}");
44+
return value as string ?? throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(string)}");
4545
}
4646

47-
public string? GetString(string key)
47+
private T? GetValue<T>(string key) where T : struct
4848
{
4949
var hasValue = this._metadata.TryGetValue(key, out var value);
5050
if (!hasValue)
5151
{
5252
return null;
5353
}
5454

55-
return value is string tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(string)}");
55+
return value is T tValue ? tValue : throw new InvalidCastException($"Cannot cast {value?.GetType().ToString() ?? "Nullable"} to {typeof(T)}");
5656
}
5757
}

test/OpenFeature.Tests/FlagMetadataTest.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,39 @@ public void GetDouble_Should_Return_Value_If_Key_Found()
104104
Assert.NotNull(result);
105105
Assert.Equal(1.2, result);
106106
}
107+
108+
[Fact]
109+
public void GetString_Should_Return_Null_If_Key_Not_Found()
110+
{
111+
// Arrange
112+
var metadata = new Dictionary<string, object>();
113+
var flagMetadata = new FlagMetadata(metadata);
114+
115+
// Act
116+
var result = flagMetadata.GetString("nonexistentKey");
117+
118+
// Assert
119+
Assert.Null(result);
120+
}
121+
122+
[Fact]
123+
public void GetString_Should_Return_Value_If_Key_Found()
124+
{
125+
// Arrange
126+
var metadata = new Dictionary<string, object>
127+
{
128+
{
129+
"stringKey", "11"
130+
}
131+
};
132+
var flagMetadata = new FlagMetadata(metadata);
133+
134+
// Act
135+
var result = flagMetadata.GetString("stringKey");
136+
137+
// Assert
138+
Assert.NotNull(result);
139+
Assert.Equal("11", result);
140+
}
107141
}
108142
}

0 commit comments

Comments
 (0)