-
Notifications
You must be signed in to change notification settings - Fork 41
fix: Add MetadataExtensions for converting metadata to primitive types #524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+497
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
61bb915
fix: Add MetadataExtensions for converting metadata to primitive types
askpt c1bcb6f
test: Add unit tests for ToPrimitiveTypes method in MetadataExtensions
askpt 618ee91
fix: Simplify ExtractPrimitiveValue method by removing unused key par…
askpt c6bcf4f
feat: Enhance ToPrimitiveTypes method to handle JSON objects and arrays
askpt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/OpenFeature.Providers.Ofrep/Extensions/MetadataExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using System.Text.Json; | ||
|
|
||
| namespace OpenFeature.Providers.Ofrep.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for handling metadata conversion. | ||
| /// </summary> | ||
| internal static class MetadataExtensions | ||
| { | ||
| /// <summary> | ||
| /// Converts a dictionary with JsonElement values to a dictionary with primitive types. | ||
| /// </summary> | ||
| /// <param name="metadata">The metadata dictionary with potential JsonElement values.</param> | ||
| /// <returns>A new dictionary with primitive type values (string, double, bool).</returns> | ||
| internal static Dictionary<string, object> ToPrimitiveTypes(this Dictionary<string, object> metadata) | ||
| { | ||
| return metadata.ToDictionary( | ||
| kvp => kvp.Key, | ||
| kvp => ExtractPrimitiveValue(kvp.Value) | ||
| ); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extracts a primitive value from an object that may be a JsonElement. | ||
| /// </summary> | ||
| /// <param name="value">The value to extract.</param> | ||
| /// <returns>The extracted primitive value.</returns> | ||
| private static object ExtractPrimitiveValue(object value) | ||
| { | ||
| if (value is JsonElement jsonElement) | ||
| { | ||
| return jsonElement.ValueKind switch | ||
| { | ||
| JsonValueKind.String => jsonElement.GetString() ?? string.Empty, | ||
| JsonValueKind.Number => jsonElement.GetDouble(), | ||
| JsonValueKind.True => true, | ||
| JsonValueKind.False => false, | ||
| JsonValueKind.Object => ConvertJsonObject(jsonElement), | ||
| JsonValueKind.Array => ConvertJsonArray(jsonElement), | ||
| JsonValueKind.Null => string.Empty, | ||
| _ => jsonElement.GetRawText() | ||
| }; | ||
| } | ||
|
|
||
| // If it's already a primitive type, return as-is | ||
| return value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a JsonElement object to a Dictionary with primitive values. | ||
| /// </summary> | ||
| /// <param name="jsonElement">The JSON element containing the object.</param> | ||
| /// <returns>A dictionary with string keys and primitive values.</returns> | ||
| private static Dictionary<string, object> ConvertJsonObject(JsonElement jsonElement) | ||
| { | ||
| var result = new Dictionary<string, object>(); | ||
| foreach (var property in jsonElement.EnumerateObject()) | ||
| { | ||
| result[property.Name] = ExtractPrimitiveValue(property.Value); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Converts a JsonElement array to a List with primitive values. | ||
| /// </summary> | ||
| /// <param name="jsonElement">The JSON element containing the array.</param> | ||
| /// <returns>A list with primitive values.</returns> | ||
| private static List<object> ConvertJsonArray(JsonElement jsonElement) | ||
| { | ||
| var result = new List<object>(); | ||
| foreach (var element in jsonElement.EnumerateArray()) | ||
| { | ||
| result.Add(ExtractPrimitiveValue(element)); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.