-
Notifications
You must be signed in to change notification settings - Fork 55
Added Conditional parameter sets feature #527
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
Draft
RakeshwarK
wants to merge
7
commits into
microsoft:main
Choose a base branch
from
RakeshwarK:rkambaiahgar/ParameterSets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
539febb
Commit Changes
0a5c90e
Updated UnitTests.csproj
5fe5a97
Merge branch 'microsoft:main' into rkambaiahgar/ParameterSets
RakeshwarK bf6aadd
Merge branch 'microsoft:main' into rkambaiahgar/ParameterSets
RakeshwarK 3a9617b
Updated Tests
6dc2f97
Merge branch 'main' into rkambaiahgar/ParameterSets
RakeshwarK 0321710
Merge branch 'microsoft:main' into rkambaiahgar/ParameterSets
RakeshwarK 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
140 changes: 140 additions & 0 deletions
140
src/VirtualClient/VirtualClient.Common/Contracts/ParameterDictionaryListJsonConverter.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,140 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace VirtualClient.Common.Contracts | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using VirtualClient.Common.Extensions; | ||
|
||
/// <summary> | ||
/// Provides a JSON converter that can handle the serialization/deserialization of | ||
/// <see cref="List{T}"/> objects where T is <see cref="IDictionary{TKey, TValue}"/> with string keys and <see cref="IConvertible"/> values. | ||
/// </summary> | ||
public class ParameterDictionaryListJsonConverter : JsonConverter | ||
{ | ||
private static readonly Type ParameterDictionaryListType = typeof(List<IDictionary<string, IConvertible>>); | ||
private static readonly ParameterDictionaryJsonConverter DictionaryConverter = new ParameterDictionaryJsonConverter(); | ||
|
||
/// <summary> | ||
/// Returns true/false whether the object type is supported for JSON serialization/deserialization. | ||
/// </summary> | ||
/// <param name="objectType">The type of object to serialize/deserialize.</param> | ||
/// <returns> | ||
/// True if the object is supported, false if not. | ||
/// </returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == ParameterDictionaryListType; | ||
} | ||
|
||
/// <summary> | ||
/// Reads the JSON text from the reader and converts it into a <see cref="List{T}"/> of <see cref="IDictionary{TKey, TValue}"/> | ||
/// object instance. | ||
/// </summary> | ||
/// <param name="reader">Contains the JSON text defining the list of dictionaries object.</param> | ||
/// <param name="objectType">The type of object (in practice this will only be a list of dictionaries type).</param> | ||
/// <param name="existingValue">Unused.</param> | ||
/// <param name="serializer">Unused.</param> | ||
/// <returns> | ||
/// A deserialized list of dictionaries object converted from JSON text. | ||
/// </returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
if (reader == null) | ||
{ | ||
throw new ArgumentException("The reader parameter is required.", nameof(reader)); | ||
} | ||
|
||
List<IDictionary<string, IConvertible>> list = new List<IDictionary<string, IConvertible>>(); | ||
if (reader.TokenType == JsonToken.StartArray) | ||
{ | ||
JArray array = JArray.Load(reader); | ||
foreach (JToken item in array) | ||
{ | ||
if (item.Type == JTokenType.Object) | ||
{ | ||
IDictionary<string, IConvertible> dictionary = new Dictionary<string, IConvertible>(); | ||
ReadDictionaryEntries(item, dictionary); | ||
list.Add(dictionary); | ||
} | ||
} | ||
} | ||
|
||
return list; | ||
} | ||
|
||
/// <summary> | ||
/// Writes a list of dictionaries object to JSON text. | ||
/// </summary> | ||
/// <param name="writer">Handles the writing of the JSON text.</param> | ||
/// <param name="value">The list of dictionaries object to serialize to JSON text.</param> | ||
/// <param name="serializer">The JSON serializer handling the serialization to JSON text.</param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
writer.ThrowIfNull(nameof(writer)); | ||
serializer.ThrowIfNull(nameof(serializer)); | ||
|
||
List<IDictionary<string, IConvertible>> list = value as List<IDictionary<string, IConvertible>>; | ||
if (list != null) | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var dictionary in list) | ||
{ | ||
WriteDictionaryEntries(writer, dictionary, serializer); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
} | ||
|
||
private static void ReadDictionaryEntries(JToken jsonObject, IDictionary<string, IConvertible> dictionary) | ||
{ | ||
IEnumerable<JToken> children = jsonObject.Children(); | ||
if (children.Any()) | ||
{ | ||
foreach (JToken child in children) | ||
{ | ||
if (child.Type == JTokenType.Property) | ||
{ | ||
if (child.First != null) | ||
{ | ||
JValue propertyValue = child.First as JValue; | ||
IConvertible settingValue = propertyValue?.Value as IConvertible; | ||
|
||
// JSON properties that have periods (.) in them will have a path representation | ||
// like this: ['this.is.a.path']. We have to account for that when adding the key | ||
// to the dictionary. The key we want to add is 'this.is.a.path' | ||
string key = child.Path; | ||
int lastDotIndex = key.LastIndexOf('.'); | ||
if (lastDotIndex >= 0) | ||
{ | ||
key = key.Substring(lastDotIndex + 1); | ||
} | ||
|
||
dictionary.Add(key, settingValue); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void WriteDictionaryEntries(JsonWriter writer, IDictionary<string, IConvertible> dictionary, JsonSerializer serializer) | ||
{ | ||
writer.WriteStartObject(); | ||
if (dictionary.Count > 0) | ||
{ | ||
foreach (KeyValuePair<string, IConvertible> entry in dictionary) | ||
{ | ||
writer.WritePropertyName(entry.Key); | ||
serializer.Serialize(writer, entry.Value); | ||
} | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
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
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming: ParameterDictionaryCollectionJsonConverter