-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from AzureCosmosDB/develop
Minor fixes
- Loading branch information
Showing
9 changed files
with
101 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
64 changes: 64 additions & 0 deletions
64
Interfaces/Cosmos.DataTransfer.Interfaces/DataItemExtensions.cs
This file contains 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,64 @@ | ||
using System.Dynamic; | ||
|
||
namespace Cosmos.DataTransfer.Interfaces; | ||
|
||
public static class DataItemExtensions | ||
{ | ||
/// <summary> | ||
/// Given a source IDataItem, builds a dynamic object tree including child objects and arrays | ||
/// </summary> | ||
/// <param name="source"></param> | ||
/// <param name="requireStringId">If true, adds a new GUID "id" field to any top level items where one is not already present.</param> | ||
/// <returns>A dynamic object containing the entire data structure.</returns> | ||
/// <remarks>The returned ExpandoObject can be used directly as an IDictionary.</remarks> | ||
public static ExpandoObject? BuildDynamicObjectTree(this IDataItem? source, bool requireStringId = false) | ||
{ | ||
if (source == null) | ||
return null; | ||
|
||
var fields = source.GetFieldNames().ToList(); | ||
var item = new ExpandoObject(); | ||
if (requireStringId && !fields.Contains("id", StringComparer.CurrentCultureIgnoreCase)) | ||
{ | ||
item.TryAdd("id", Guid.NewGuid().ToString()); | ||
} | ||
foreach (string field in fields) | ||
{ | ||
object? value = source.GetValue(field); | ||
var fieldName = field; | ||
if (string.Equals(field, "id", StringComparison.CurrentCultureIgnoreCase) && requireStringId) | ||
{ | ||
value = value?.ToString(); | ||
fieldName = "id"; | ||
} | ||
else if (value is IDataItem child) | ||
{ | ||
value = BuildDynamicObjectTree(child); | ||
} | ||
else if (value is IEnumerable<object?> array) | ||
{ | ||
value = BuildArray(array); | ||
} | ||
|
||
item.TryAdd(fieldName, value); | ||
} | ||
|
||
return item; | ||
|
||
static object BuildArray(IEnumerable<object?> array) | ||
{ | ||
return array.Select(dataItem => | ||
{ | ||
switch (dataItem) | ||
{ | ||
case IDataItem childObject: | ||
return BuildDynamicObjectTree(childObject); | ||
case IEnumerable<object?> array: | ||
return BuildArray(array); | ||
default: | ||
return dataItem; | ||
} | ||
}).ToArray(); | ||
} | ||
} | ||
} |