-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
|
||
namespace OpenFeature.Contrib.Providers.GOFeatureFlag.helpers | ||
{ | ||
/// <summary> | ||
/// DictionaryConverter is converting a json Dictionary to a Dictionary<string, object> | ||
Check warning on line 8 in src/OpenFeature.Contrib.Providers.GOFeatureFlag/helpers/DictionaryConverter.cs
|
||
/// </summary> | ||
Check warning on line 9 in src/OpenFeature.Contrib.Providers.GOFeatureFlag/helpers/DictionaryConverter.cs
|
||
public static class DictionaryConverter | ||
Check warning on line 10 in src/OpenFeature.Contrib.Providers.GOFeatureFlag/helpers/DictionaryConverter.cs
|
||
{ | ||
/// <summary> | ||
/// Function that convert the dictionary to a Dictionary<string, object> | ||
Check warning on line 13 in src/OpenFeature.Contrib.Providers.GOFeatureFlag/helpers/DictionaryConverter.cs
|
||
/// </summary> | ||
Check warning on line 14 in src/OpenFeature.Contrib.Providers.GOFeatureFlag/helpers/DictionaryConverter.cs
|
||
/// <param name="inputDictionary"></param> | ||
/// <returns></returns> | ||
public static Dictionary<string, object> ConvertDictionary(Dictionary<string, object> inputDictionary) | ||
Check warning on line 17 in src/OpenFeature.Contrib.Providers.GOFeatureFlag/helpers/DictionaryConverter.cs
|
||
{ | ||
return inputDictionary.ToDictionary( | ||
kvp => kvp.Key, | ||
kvp => ConvertValue(kvp.Value) | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Function that convert a value to a object | ||
/// </summary> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
public static object ConvertValue(object value) | ||
{ | ||
if (value is JsonElement jsonElement) | ||
switch (jsonElement.ValueKind) | ||
{ | ||
case JsonValueKind.String: | ||
return jsonElement.GetString(); | ||
case JsonValueKind.Number: | ||
if (jsonElement.TryGetInt32(out var intValue)) return intValue; | ||
|
||
if (jsonElement.TryGetDouble(out var doubleValue)) return doubleValue; | ||
return jsonElement.GetRawText(); // Fallback to string if not int or double | ||
case JsonValueKind.True: | ||
return true; | ||
case JsonValueKind.False: | ||
return false; | ||
case JsonValueKind.Null: | ||
return null; | ||
case JsonValueKind.Object: | ||
return ConvertDictionary( | ||
JsonSerializer | ||
.Deserialize<Dictionary<string, object>>(jsonElement | ||
.GetRawText())); //Recursive for nested objects | ||
case JsonValueKind.Array: | ||
var array = new List<object>(); | ||
foreach (var element in jsonElement.EnumerateArray()) array.Add(ConvertValue(element)); | ||
|
||
return array; | ||
default: | ||
return jsonElement.GetRawText(); // Handle other types as needed | ||
} | ||
|
||
return value; // Return original value if not a JsonElement | ||
} | ||
} | ||
} |