Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle Display attribute specially #333

Merged
merged 3 commits into from
Sep 10, 2014
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ You can even configure the name of the property of attibute to use as descriptio

`Configurator.EnumDescriptionPropertyLocator = p => p.Name == "Info"`

How ever prior possibilities do not provide a way to localize the descriptions, if you need this, please use `DisplayAttribute` data annotation, which is build to support localization and is utilized for this use case.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this description a bit confusing. Can you please change it? How about "If you need to provide localised descriptions you can use DisplayAttribute data annotation instead."


```C#
public enum EnumUnderTest
{
[Display(Description = "EnumUnderTest_Member", ResourceType = typeof(Project.Resources))]
Member
}
```

You will get:

```C#
EnumUnderTest.Member.Humanize() => "content" // from Project.Resources found under "EnumUnderTest_Member" resource key
```

Hopefully this will help avoid littering enums with unnecessary attributes!

###<a id="dehumanize-enums">Dehumanize Enums</a>
Expand Down
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- [#320](https://github.com/MehdiK/Humanizer/pull/320): Fixed Dehumanize actually humanizing an already dehumanized string
- [#322](https://github.com/MehdiK/Humanizer/pull/322): DefaultFormatter.TimeSpanHumanize throws a more meaningful exception when called with TimeUnits larger than TimeUnit.Week
- [#314](https://github.com/MehdiK/Humanizer/pull/314): Added ByteRate class and supporting members to facilitate calculation of byte transfer rates
- [#333](https://github.com/MehdiK/Humanizer/pull/333): Added support to humanize enums from resource strings

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.28.0...master)

Expand Down
14 changes: 14 additions & 0 deletions src/Humanizer.Tests/DehumanizeToEnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ public void AllCapitalMembersAreReturnedAsIs()
Assert.Equal(EnumUnderTest.ALLCAPITALS, EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo(typeof(EnumUnderTest)));
}

[Fact]
public void HonorsDisplayAttribute()
{
Assert.Equal(EnumUnderTest.MemberWithDisplayAttribute, EnumTestsResources.MemberWithDisplayAttribute.DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.MemberWithDisplayAttribute, EnumTestsResources.MemberWithDisplayAttribute.DehumanizeTo(typeof(EnumUnderTest)));
}

[Fact]
public void HonorsLocalizedDisplayAttribute()
{
Assert.Equal(EnumUnderTest.MemberWithLocalizedDisplayAttribute, EnumTestsResources.MemberWithLocalizedDisplayAttribute.DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.MemberWithLocalizedDisplayAttribute, EnumTestsResources.MemberWithLocalizedDisplayAttribute.DehumanizeTo(typeof(EnumUnderTest)));
}

struct DummyStructWithEnumInterfaces : IComparable, IFormattable, IConvertible
{
public int CompareTo(object obj)
Expand Down
12 changes: 12 additions & 0 deletions src/Humanizer.Tests/EnumHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,17 @@ public void AllCapitalMembersAreReturnedAsIs()
{
Assert.Equal(EnumUnderTest.ALLCAPITALS.ToString(), EnumUnderTest.ALLCAPITALS.Humanize());
}

[Fact]
public void HonorsDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithDisplayAttribute, EnumUnderTest.MemberWithDisplayAttribute.Humanize());
}

[Fact]
public void HonorsLocalizedDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithLocalizedDisplayAttribute, EnumUnderTest.MemberWithLocalizedDisplayAttribute.Humanize());
}
}
}
9 changes: 8 additions & 1 deletion src/Humanizer.Tests/EnumUnderTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Humanizer.Tests
{
Expand All @@ -16,7 +17,11 @@ public enum EnumUnderTest
[CustomProperty(EnumTestsResources.MemberWithCustomPropertyAttribute)]
MemberWithCustomPropertyAttribute,
MemberWithoutDescriptionAttribute,
ALLCAPITALS
ALLCAPITALS,
[Display(Description = EnumTestsResources.MemberWithDisplayAttribute)]
MemberWithDisplayAttribute,
[Display(Description = "MemberWithLocalizedDisplayAttribute", ResourceType = typeof(EnumTestsResources))]
MemberWithLocalizedDisplayAttribute
}

public class EnumTestsResources
Expand All @@ -29,6 +34,8 @@ public class EnumTestsResources
public const string MemberWithoutDescriptionAttributeSentence = "Member without description attribute";
public const string MemberWithoutDescriptionAttributeTitle = "Member Without Description Attribute";
public const string MemberWithoutDescriptionAttributeLowerCase = "member without description attribute";
public const string MemberWithDisplayAttribute = "Description from Display attribute";
public static string MemberWithLocalizedDisplayAttribute { get { return "Localized description from Display attribute"; } }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, pretty cool :)

}

public class ImposterDescriptionAttribute : Attribute
Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<HintPath>..\packages\ApprovalUtilities.3.0.5\lib\net35\ApprovalUtilities.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
9 changes: 9 additions & 0 deletions src/Humanizer/EnumHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace Humanizer
/// </summary>
public static class EnumHumanizeExtensions
{
private const string DisplayAttributeTypeName = "System.ComponentModel.DataAnnotations.DisplayAttribute";
private const string DisplayAttributeGetDescriptionMethodName = "GetDescription";

private static readonly Func<PropertyInfo, bool> StringTypedProperty = p => p.PropertyType == typeof(string);

/// <summary>
Expand Down Expand Up @@ -42,6 +45,12 @@ private static string GetCustomDescription(MemberInfo memberInfo)
foreach (var attr in attrs)
{
var attrType = attr.GetType();
if (attrType.FullName == DisplayAttributeTypeName)
{
var method = attrType.GetMethod(DisplayAttributeGetDescriptionMethodName);
if (method != null)
return method.Invoke(attr, new object[0]).ToString();
}
var descriptionProperty =
attrType.GetProperties()
.Where(StringTypedProperty)
Expand Down