Summary

Class:WireMock.Extensions.DictionaryExtensions
Assembly:WireMock.Net
File(s):C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Extensions\DictionaryExtensions.cs
Covered lines:0
Uncovered lines:34
Coverable lines:34
Total lines:63
Line coverage:0%
Branch coverage:0%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
ToExpandoObject(...)11800

File(s)

C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Extensions\DictionaryExtensions.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using System.Dynamic;
 4
 5namespace WireMock.Extensions
 6{
 7    /// <summary>
 8    /// Dictionary Extensions
 9    /// </summary>
 10    public static class DictionaryExtensions
 11    {
 12        /// <summary>
 13        /// Converts IDictionary to an ExpandObject.
 14        /// </summary>
 15        /// <typeparam name="T"></typeparam>
 16        /// <param name="dictionary">The dictionary.</param>
 17        /// <returns></returns>
 18        public static dynamic ToExpandoObject<T>(this IDictionary<string, T> dictionary)
 019        {
 020            dynamic expando = new ExpandoObject();
 021            var expandoDic = (IDictionary<string, object>)expando;
 22
 23            // go through the items in the dictionary and copy over the key value pairs)
 024            foreach (var kvp in dictionary)
 025            {
 26                // if the value can also be turned into an ExpandoObject, then do it!
 027                var value = kvp.Value as IDictionary<string, object>;
 028                 if (value != null)
 029                {
 030                    var expandoValue = value.ToExpandoObject();
 031                    expandoDic.Add(kvp.Key, expandoValue);
 032                }
 033                 else if (kvp.Value is ICollection)
 034                {
 35                    // iterate through the collection and convert any strin-object dictionaries
 36                    // along the way into expando objects
 037                    var itemList = new List<object>();
 038                    foreach (var item in (ICollection)kvp.Value)
 039                    {
 040                        var objects = item as IDictionary<string, object>;
 041                         if (objects != null)
 042                        {
 043                            var expandoItem = objects.ToExpandoObject();
 044                            itemList.Add(expandoItem);
 045                        }
 46                        else
 047                        {
 048                            itemList.Add(item);
 049                        }
 050                    }
 51
 052                    expandoDic.Add(kvp.Key, itemList);
 053                }
 54                else
 055                {
 056                    expandoDic.Add(kvp.Key, kvp.Value);
 057                }
 058            }
 59
 060            return expando;
 061        }
 62    }
 63}