Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)TestObjects\SimpleObjectWithEnum.cs" />
<Compile Include="$(MSBuildThisFileDirectory)JsonPropertyAttributeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestObjects\SimpleObjectWithJsonPropertyAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TestObjects\SimpleObjectWithEnumerableEnum.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)TestObjects\" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Xunit;
using Couchbase.Lite.Mapping.Tests.TestObjects;
using System.Collections.Generic;

namespace Couchbase.Lite.Mapping.Tests
{
Expand Down Expand Up @@ -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> { 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
Expand Down
32 changes: 32 additions & 0 deletions src/Couchbase.Lite.Mapping.Shared.Tests/SimpleEnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,37 @@ public void TestToSimpleEnum()

Assert.Equal(SimpleEnum.Enum_2, simpleObject.SimpleEnum);
}

[Fact]
public void TestToSimpleArrayEnum()
{
var dictionary = new Dictionary<string, object>
{
["ArraySimpleEnums"] = new[] { "EnumValue_1", "EnumValue_2" }
};

var mutableDocument = new MutableDocument("test_id", dictionary);

var simpleObjectWithEnumerableEnum = mutableDocument.ToObject<SimpleObjectWithEnumerableEnum>();

Assert.Equal(SimpleEnum.Enum_1, simpleObjectWithEnumerableEnum.ArraySimpleEnums[0]);
Assert.Equal(SimpleEnum.Enum_2, simpleObjectWithEnumerableEnum.ArraySimpleEnums[1]);
}

[Fact]
public void TestToSimpleListEnum()
{
var dictionary = new Dictionary<string, object>
{
["ListSimpleEnums"] = new[] { "EnumValue_2", "EnumValue_3" }
};

var mutableDocument = new MutableDocument("test_id", dictionary);

var simpleObjectWithEnumerableEnum = mutableDocument.ToObject<SimpleObjectWithEnumerableEnum>();

Assert.Equal(SimpleEnum.Enum_2, simpleObjectWithEnumerableEnum.ListSimpleEnums[0]);
Assert.Equal(SimpleEnum.Enum_3, simpleObjectWithEnumerableEnum.ListSimpleEnums[1]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<SimpleEnum> ListSimpleEnums { get; set; }
}
}
40 changes: 28 additions & 12 deletions src/Couchbase.Lite.Mapping/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,9 @@ static Dictionary<string, object> 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<EnumMemberAttribute>();
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;
Expand Down Expand Up @@ -111,8 +102,28 @@ static void AddDictionaryValue(ref Dictionary<string, object> 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<string>();
foreach (object @object in propertyValue as IEnumerable)
{
EnumMemberAttribute itemAttribute = enumType.GetMember(@object.ToString()).FirstOrDefault()?.GetCustomAttribute<EnumMemberAttribute>();
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;
Expand All @@ -138,6 +149,11 @@ static void AddDictionaryValue(ref Dictionary<string, object> dictionary,
}
else if (propertyType.IsEnum)
{
var attribute = propertyType.GetMember(propertyValue.ToString()).FirstOrDefault()?.GetCustomAttribute<EnumMemberAttribute>();
if (attribute != null)
{
propertyValue = attribute.Value;
}
dictionary[propertyName] = propertyValue.ToString();
}
else
Expand Down