Skip to content

Commit

Permalink
+ new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Oct 20, 2018
1 parent 717a770 commit 1ff7902
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
16 changes: 16 additions & 0 deletions ObjectDictionaryMapper.Tests/SimpleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ public void SimpleDefaultConfigTestTwo()
Assert.That(simpleObjectTwo.StringssProperty, Has.One.Contains("Test012"));
Assert.That(simpleObjectTwo.SimpleObject, Is.Not.Null);
Assert.That(simpleObjectTwo.SimpleObject.BoolProperty, Is.EqualTo(true));

Assert.That(simpleObjectTwo.DictProperty, Is.Not.Null);
Assert.That(simpleObjectTwo.DictProperty, Does.ContainKey("Test").And.ContainValue(123));
Assert.That(simpleObjectTwo.DictProperty, Does.ContainKey("Test2").And.ContainValue(456));
}

[Test]
public void SimpleDictionaryTest()
{
Dictionary<SimpleObject, string> TestMap = new Dictionary<SimpleObject, string>()
{
[new SimpleObject()] = "test"
};

var dictionary = Mapper.ToDictionary(TestMap);
Assert.That(dictionary, Is.Not.Null);
}
}
}
4 changes: 4 additions & 0 deletions ObjectDictionaryMapper.Tests/TestClasses/SimpleObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ public class SimpleObject
public double DoubleProperty { get; set; } = 123.321;
public DateTime DateTimeProperty { get; set; } = DateTime.MaxValue;
public bool BoolProperty { get; set; } = true;
public short ShortProperty { get; set; } = 123;
public byte ByteProperty { get; set; } = 123;
public long LongProperty { get; set; } = 123;
public uint UIntProperty { get; set; } = 123;
}
}
7 changes: 7 additions & 0 deletions ObjectDictionaryMapper.Tests/TestClasses/SimpleObjectTwo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ public class SimpleObjectTwo
{
public SimpleObject SimpleObject { get; set; } = new SimpleObject();
public string StringProperty { get; set; } = "Test321";

public Dictionary<String, int> DictProperty { get; set; } = new Dictionary<string, int>()
{
["Test"] = 123,
["Test2"] = 456
};

public List<string> StringsProperty { get; set; } = new List<string> {
"Test123",
"Test456"
Expand Down
28 changes: 15 additions & 13 deletions ObjectDictionaryMapper/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Mapper
{
private MapperConfiguration _configuration;

public static Dictionary<string, object> ToDictionary(Object src)
public static Dictionary<object, object> ToDictionary(Object src)
{
return new Mapper().ToDict(src);
}
Expand Down Expand Up @@ -46,7 +46,7 @@ private object ToObj(IDictionary dictionary, Type type)
return result;
}

private object MapList(IEnumerable entryValue, Type propertyInfoPropertyType)
private object MapList(object entryValue, Type propertyInfoPropertyType)
{
if (typeof(IDictionary).IsAssignableFrom(propertyInfoPropertyType))
{
Expand All @@ -58,7 +58,7 @@ private object MapList(IEnumerable entryValue, Type propertyInfoPropertyType)
resultType = propertyInfoPropertyType.GetGenericArguments().First();
var result = (IList)Activator.CreateInstance(propertyInfoPropertyType);

foreach (var o in entryValue)
foreach (var o in (IEnumerable) entryValue)
{
if(o == null) continue;
result.Add(MapDictValueToObj(o, resultType));
Expand Down Expand Up @@ -91,38 +91,40 @@ private object MapDictValueToObj(object value, Type destType)
if (destType.IsInstanceOfType(value))
return value;
if (typeof(IEnumerable).IsAssignableFrom(destType))
return MapList(value as IEnumerable, destType);
return MapList(value, destType);
return ToObj((IDictionary)value, destType);
}

public Dictionary<string, object> ToDict(object src)
public Dictionary<object, object> ToDict(object src)
{
if (src == null)
return null;
Dictionary<string, object> result = new Dictionary<string, object>();
Dictionary<object, object> result = new Dictionary<object, object>();

var type = src.GetType();
if (src is IDictionary dict)
foreach (DictionaryEntry o in dict)
result.Add(o.Key.ToString(), ToDict(o.Value));
{
result.Add(MapPropertyToDict(o.Key.GetType(), o.Key), MapPropertyToDict(o.Value.GetType(), o.Value));
}
else
foreach (var propertyInfo in type.GetProperties())
result.Add(propertyInfo.Name, MapPropertyToDict(propertyInfo, src));
result.Add(propertyInfo.Name, MapPropertyToDict(propertyInfo.PropertyType, propertyInfo.GetValue(src)));

return result;
}

private object MapPropertyToDict(PropertyInfo propertyInfo, object src)
private object MapPropertyToDict(Type srcType, object src)
{
var firstOrDefault = _configuration.TypeMappers.FirstOrDefault(t => t.CanHandle(propertyInfo.PropertyType));
var value = propertyInfo.GetValue(src);
return firstOrDefault != null ? firstOrDefault.ToDictionaryType(value) : MapObjectToDict(value);
var firstOrDefault = _configuration.TypeMappers.FirstOrDefault(t => t.CanHandle(srcType));
return firstOrDefault != null ? firstOrDefault.ToDictionaryType(src) : MapObjectToDict(src);
}

private object MapObjectToDict(object value)
{
if (value == null) return null;
if (!(value is IEnumerable enumerable)) return ToDict(value);
if (value is IDictionary dictionary) return ToDict(dictionary);

IList list = new List<object>();
foreach (var o in enumerable)
Expand All @@ -132,7 +134,7 @@ private object MapObjectToDict(object value)
var firstOrDefault = _configuration.TypeMappers.FirstOrDefault(t => t.CanHandle(type));
list.Add(firstOrDefault != null ? firstOrDefault.ToDictionaryType(o) : MapObjectToDict(o));
}

return list;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ObjectDictionaryMapper/MapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ObjectDictionaryMapper
{
public class MapperConfiguration
{
public static MapperConfiguration Default { get; set; } = new MapperConfiguration();
public static MapperConfiguration Default { get; } = new MapperConfiguration();

public IntTypeMapper IntTypeMapper { get; } = new IntTypeMapper();
public StringTypeMapper StringTypeMapper { get; } = new StringTypeMapper();
Expand Down
5 changes: 4 additions & 1 deletion ObjectDictionaryMapper/TypeMapping/IntTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public class IntTypeMapper : ITypeMapper

public bool CanHandle(Type type)
{
return type == typeof(int) || type == typeof(short) || type == typeof(byte) || type == typeof(long);
return type == typeof(int) || type == typeof(uint)
|| type == typeof(short) || type == typeof(ushort)
|| type == typeof(byte)
|| type == typeof(long) || type == typeof(ulong);
}

public object ToDictionaryType(object src)
Expand Down

0 comments on commit 1ff7902

Please sign in to comment.