Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 96aa019

Browse files
committed
Add more dictionary to Poco tests
1 parent 3c0c728 commit 96aa019

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
156156
if (from is IEnumerable<KeyValuePair<string, object>> objDict)
157157
return objDict.FromObjectDictionary(toType);
158158

159+
if (from is IEnumerable<KeyValuePair<string, string>> strDict)
160+
return strDict.ToObjectDictionary().FromObjectDictionary(toType);
161+
159162
var to = toType.CreateInstance();
160163
return to.PopulateWithNonDefaultValues(from);
161164
}
@@ -950,6 +953,14 @@ public static object TryConvertCollections(Type fromType, Type toType, object fr
950953
return ret ?? fromValue;
951954
}
952955
}
956+
else if (fromType.IsClass &&
957+
(typeof(IDictionary).IsAssignableFrom(toType) ||
958+
typeof(IEnumerable<KeyValuePair<string,object>>).IsAssignableFrom(toType) ||
959+
typeof(IEnumerable<KeyValuePair<string,string>>).IsAssignableFrom(toType)))
960+
{
961+
var fromDict = fromValue.ToObjectDictionary();
962+
return TryConvertCollections(fromType.GetType(), toType, fromDict);
963+
}
953964

954965
var listResult = TranslateListWithElements.TryTranslateCollections(fromType, toType, fromValue);
955966
return listResult ?? fromValue;

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,5 +1147,101 @@ public void Can_convert_between_structs_with_explicit_casts()
11471147
d = c.ConvertTo<D>();
11481148
Assert.That(d.Id, Is.EqualTo(c.Id));
11491149
}
1150+
1151+
[Test]
1152+
public void Can_Convert_from_ObjectDictionary_Containing_Another_Object_Dictionary()
1153+
{
1154+
var map = new Dictionary<string, object>
1155+
{
1156+
{ "name", "Foo" },
1157+
{ "otherNames", new List<object>
1158+
{
1159+
new Dictionary<string, object> { { "name", "Fu" } },
1160+
new Dictionary<string, object> { { "name", "Fuey" } }
1161+
}
1162+
}
1163+
};
1164+
1165+
var fromDict = map.ConvertTo<AutoMappingObjectDictionaryTests.PersonWithIdentities>();
1166+
1167+
Assert.That(fromDict.Name, Is.EqualTo("Foo"));
1168+
Assert.That(fromDict.OtherNames.Count, Is.EqualTo(2));
1169+
Assert.That(fromDict.OtherNames.First().Name, Is.EqualTo("Fu"));
1170+
Assert.That(fromDict.OtherNames.Last().Name, Is.EqualTo("Fuey"));
1171+
1172+
var toDict = fromDict.ConvertTo<Dictionary<string,object>>();
1173+
Assert.That(toDict["Name"], Is.EqualTo("Foo"));
1174+
Assert.That(toDict["OtherNames"], Is.EqualTo(fromDict.OtherNames));
1175+
1176+
var toObjDict = fromDict.ConvertTo<ObjectDictionary>();
1177+
Assert.That(toObjDict["Name"], Is.EqualTo("Foo"));
1178+
Assert.That(toObjDict["OtherNames"], Is.EqualTo(fromDict.OtherNames));
1179+
}
1180+
1181+
class Person
1182+
{
1183+
public int Id { get; set; }
1184+
public string Name { get; set; }
1185+
1186+
protected bool Equals(Person other) => Id == other.Id && string.Equals(Name, other.Name);
1187+
1188+
public override bool Equals(object obj)
1189+
{
1190+
if (ReferenceEquals(null, obj)) return false;
1191+
if (ReferenceEquals(this, obj)) return true;
1192+
return obj.GetType() == this.GetType() && Equals((Person) obj);
1193+
}
1194+
1195+
public override int GetHashCode()
1196+
{
1197+
unchecked
1198+
{
1199+
return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0);
1200+
}
1201+
}
1202+
}
1203+
1204+
[Test]
1205+
public void Can_convert_between_Poco_and_ObjectDictionary()
1206+
{
1207+
var person = new Person { Id = 1, Name = "foo" };
1208+
1209+
Assert.That(person.ConvertTo<Dictionary<string, object>>(), Is.EqualTo(new Dictionary<string, object> {
1210+
{"Id", 1},
1211+
{"Name", "foo"},
1212+
}));
1213+
Assert.That(new Dictionary<string, object> {
1214+
{"Id", 1},
1215+
{"Name", "foo"},
1216+
}.ConvertTo<Person>(), Is.EqualTo(person));
1217+
1218+
Assert.That(person.ConvertTo<ObjectDictionary>(), Is.EqualTo(new ObjectDictionary {
1219+
{"Id", 1},
1220+
{"Name", "foo"},
1221+
}));
1222+
Assert.That(new ObjectDictionary {
1223+
{"Id", 1},
1224+
{"Name", "foo"},
1225+
}.ConvertTo<Person>(), Is.EqualTo(person));
1226+
1227+
Assert.That(person.ConvertTo<Dictionary<string, string>>(), Is.EqualTo(new Dictionary<string, string> {
1228+
{"Id", "1"},
1229+
{"Name", "foo"},
1230+
}));
1231+
1232+
Assert.That(new Dictionary<string, string> {
1233+
{"Id", "1"},
1234+
{"Name", "foo"},
1235+
}.ConvertTo<Person>(), Is.EqualTo(person));
1236+
1237+
Assert.That(person.ConvertTo<StringDictionary>(), Is.EqualTo(new StringDictionary {
1238+
{"Id", "1"},
1239+
{"Name", "foo"},
1240+
}));
1241+
Assert.That(new StringDictionary {
1242+
{"Id", "1"},
1243+
{"Name", "foo"},
1244+
}.ConvertTo<Person>(), Is.EqualTo(person));
1245+
}
11501246
}
11511247
}

0 commit comments

Comments
 (0)