From fd3302bf4ad591b945c69926ac115c108aaed599 Mon Sep 17 00:00:00 2001 From: Riad Lakehal-Ayat Date: Fri, 10 Jan 2020 12:08:12 +0100 Subject: [PATCH 1/2] Save value of EnumMember when saving an enum enumerable --- ...hbase.Lite.Mapping.Enterprise.Tests.csproj | 3 ++ ...chbase.Lite.Mapping.Shared.Tests.projitems | 1 + .../PropertyNameConversionTests.cs | 41 +++++++++++++++++++ .../SimpleEnumTests.cs | 32 +++++++++++++++ .../TestObjects/SimpleEnum.cs | 6 ++- .../SimpleObjectWithEnumerableEnum.cs | 13 ++++++ .../ObjectExtensions.cs | 40 ++++++++++++------ 7 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleObjectWithEnumerableEnum.cs diff --git a/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj b/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj index 2a622e3..f6ec539 100644 --- a/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj +++ b/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj @@ -17,5 +17,8 @@ + + + diff --git a/src/Couchbase.Lite.Mapping.Shared.Tests/Couchbase.Lite.Mapping.Shared.Tests.projitems b/src/Couchbase.Lite.Mapping.Shared.Tests/Couchbase.Lite.Mapping.Shared.Tests.projitems index 9d8b04a..6fe9add 100644 --- a/src/Couchbase.Lite.Mapping.Shared.Tests/Couchbase.Lite.Mapping.Shared.Tests.projitems +++ b/src/Couchbase.Lite.Mapping.Shared.Tests/Couchbase.Lite.Mapping.Shared.Tests.projitems @@ -27,6 +27,7 @@ + diff --git a/src/Couchbase.Lite.Mapping.Shared.Tests/PropertyNameConversionTests.cs b/src/Couchbase.Lite.Mapping.Shared.Tests/PropertyNameConversionTests.cs index 661b64f..fc33c65 100644 --- a/src/Couchbase.Lite.Mapping.Shared.Tests/PropertyNameConversionTests.cs +++ b/src/Couchbase.Lite.Mapping.Shared.Tests/PropertyNameConversionTests.cs @@ -1,6 +1,7 @@ using System; using Xunit; using Couchbase.Lite.Mapping.Tests.TestObjects; +using System.Collections.Generic; namespace Couchbase.Lite.Mapping.Tests { @@ -84,6 +85,46 @@ public void TestEnumToMutableDocument() Assert.True(result, "Property Name is not getting converted correctly."); Assert.Equal("EnumValue_2", enumValue); } + + [Fact] + public void TestArrayEnumToMutableDocument() + { + var simpleObject = new SimpleObjectWithEnumerableEnum + { + ArraySimpleEnums = new[] { SimpleEnum.Enum_1, SimpleEnum.Enum_2 } + }; + + var mutableDocument = simpleObject.ToMutableDocument(); + + var result = mutableDocument.Keys.Contains("ArraySimpleEnums"); + var enums = mutableDocument.GetArray("ArraySimpleEnums"); + var enumValue1 = enums.GetString(0); + var enumValue2 = enums.GetString(1); + + Assert.True(result, "Property Name is not getting converted correctly."); + Assert.Equal("EnumValue_1", enumValue1); + Assert.Equal("EnumValue_2", enumValue2); + } + + [Fact] + public void TestListEnumToMutableDocument() + { + var simpleObject = new SimpleObjectWithEnumerableEnum + { + ListSimpleEnums = new List { SimpleEnum.Enum_2, SimpleEnum.Enum_3 } + }; + + var mutableDocument = simpleObject.ToMutableDocument(); + + var result = mutableDocument.Keys.Contains("ListSimpleEnums"); + var enums = mutableDocument.GetArray("ListSimpleEnums"); + var enumValue1 = enums.GetString(0); + var enumValue2 = enums.GetString(1); + + Assert.True(result, "Property Name is not getting converted correctly."); + Assert.Equal("EnumValue_2", enumValue1); + Assert.Equal("EnumValue_3", enumValue2); + } } public class CustomPropertyNameConverter : IPropertyNameConverter diff --git a/src/Couchbase.Lite.Mapping.Shared.Tests/SimpleEnumTests.cs b/src/Couchbase.Lite.Mapping.Shared.Tests/SimpleEnumTests.cs index 0c5900e..8fe0b5f 100644 --- a/src/Couchbase.Lite.Mapping.Shared.Tests/SimpleEnumTests.cs +++ b/src/Couchbase.Lite.Mapping.Shared.Tests/SimpleEnumTests.cs @@ -21,5 +21,37 @@ public void TestToSimpleEnum() Assert.Equal(SimpleEnum.Enum_2, simpleObject.SimpleEnum); } + + [Fact] + public void TestToSimpleArrayEnum() + { + var dictionary = new Dictionary + { + ["ArraySimpleEnums"] = new[] { "EnumValue_1", "EnumValue_2" } + }; + + var mutableDocument = new MutableDocument("test_id", dictionary); + + var simpleObjectWithEnumerableEnum = mutableDocument.ToObject(); + + Assert.Equal(SimpleEnum.Enum_1, simpleObjectWithEnumerableEnum.ArraySimpleEnums[0]); + Assert.Equal(SimpleEnum.Enum_2, simpleObjectWithEnumerableEnum.ArraySimpleEnums[1]); + } + + [Fact] + public void TestToSimpleListEnum() + { + var dictionary = new Dictionary + { + ["ListSimpleEnums"] = new[] { "EnumValue_2", "EnumValue_3" } + }; + + var mutableDocument = new MutableDocument("test_id", dictionary); + + var simpleObjectWithEnumerableEnum = mutableDocument.ToObject(); + + Assert.Equal(SimpleEnum.Enum_2, simpleObjectWithEnumerableEnum.ListSimpleEnums[0]); + Assert.Equal(SimpleEnum.Enum_3, simpleObjectWithEnumerableEnum.ListSimpleEnums[1]); + } } } diff --git a/src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleEnum.cs b/src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleEnum.cs index 0852868..fc4dfab 100644 --- a/src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleEnum.cs +++ b/src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleEnum.cs @@ -7,6 +7,8 @@ public enum SimpleEnum [EnumMember(Value = "EnumValue_1")] Enum_1, [EnumMember(Value = "EnumValue_2")] - Enum_2 - } + Enum_2, + [EnumMember(Value = "EnumValue_3")] + Enum_3 + } } diff --git a/src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleObjectWithEnumerableEnum.cs b/src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleObjectWithEnumerableEnum.cs new file mode 100644 index 0000000..5bf13c7 --- /dev/null +++ b/src/Couchbase.Lite.Mapping.Shared.Tests/TestObjects/SimpleObjectWithEnumerableEnum.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Couchbase.Lite.Mapping.Tests.TestObjects +{ + public class SimpleObjectWithEnumerableEnum + { + [MappingPropertyName("ArraySimpleEnums")] + public SimpleEnum[] ArraySimpleEnums { get; set; } + + [MappingPropertyName("ListSimpleEnums")] + public List ListSimpleEnums { get; set; } + } +} diff --git a/src/Couchbase.Lite.Mapping/ObjectExtensions.cs b/src/Couchbase.Lite.Mapping/ObjectExtensions.cs index 259d64f..f9f7166 100644 --- a/src/Couchbase.Lite.Mapping/ObjectExtensions.cs +++ b/src/Couchbase.Lite.Mapping/ObjectExtensions.cs @@ -56,18 +56,9 @@ static Dictionary GetDictionary(object obj, IPropertyNameConvert var propertyValue = propertyInfo.GetValue(obj); var propertyType = propertyInfo.PropertyType; - if (propertyType.IsEnum) - { - var attribute = propertyInfo.PropertyType.GetMember(propertyValue.ToString()).FirstOrDefault()?.GetCustomAttribute(); - if (attribute != null) - { - propertyValue = attribute.Value; - } - } - if (propertyValue != null) { - if (propertyInfo.CustomAttributes?.Count() > 0 && + if (propertyInfo.CustomAttributes?.Count() > 0 && propertyInfo.GetCustomAttribute(typeof(MappingPropertyName)) is MappingPropertyName mappingProperty) { propertyName = mappingProperty.Name; @@ -111,8 +102,28 @@ static void AddDictionaryValue(ref Dictionary dictionary, { if (typeof(IEnumerable).IsAssignableFrom(propertyType)) { - if (propertyType.IsArray && propertyType.GetElementType().IsSimple() - || (!propertyType.IsArray && propertyValue is IList + if (propertyType.IsArray && propertyType.GetElementType().IsEnum + || (!propertyType.IsArray && propertyValue is IList + && propertyValue.GetType().GetTypeInfo().GenericTypeArguments[0].IsEnum)) + { + Type enumType = propertyType.IsArray ? + propertyType.GetElementType() : + propertyValue.GetType().GetTypeInfo().GenericTypeArguments[0]; + + var propertyValueEnumList = new List(); + foreach (object @object in propertyValue as IEnumerable) + { + EnumMemberAttribute itemAttribute = enumType.GetMember(@object.ToString()).FirstOrDefault()?.GetCustomAttribute(); + if (itemAttribute != null) + { + propertyValueEnumList.Add(itemAttribute.Value); + } + } + dictionary[propertyName] = propertyValueEnumList; + } + + else if (propertyType.IsArray && propertyType.GetElementType().IsSimple() + || (!propertyType.IsArray && propertyValue is IList && propertyValue.GetType().GetTypeInfo().GenericTypeArguments[0].IsSimple())) { dictionary[propertyName] = propertyValue; @@ -138,6 +149,11 @@ static void AddDictionaryValue(ref Dictionary dictionary, } else if (propertyType.IsEnum) { + var attribute = propertyType.GetMember(propertyValue.ToString()).FirstOrDefault()?.GetCustomAttribute(); + if (attribute != null) + { + propertyValue = attribute.Value; + } dictionary[propertyName] = propertyValue.ToString(); } else From 4a80736b91992745e35f31057bfb2112298384c1 Mon Sep 17 00:00:00 2001 From: Riad Lakehal-Ayat Date: Tue, 14 Jan 2020 10:16:41 +0100 Subject: [PATCH 2/2] Remove not needed information from Enterprise.Tests.csproj --- .../Couchbase.Lite.Mapping.Enterprise.Tests.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj b/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj index f6ec539..2a622e3 100644 --- a/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj +++ b/src/Couchbase.Lite.Mapping.Enterprise.Tests/Couchbase.Lite.Mapping.Enterprise.Tests.csproj @@ -17,8 +17,5 @@ - - -