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

FR: alternative EnumCache and extensions to De/Humanize enums #1490

Open
KillyMXI opened this issue Mar 16, 2024 · 0 comments
Open

FR: alternative EnumCache and extensions to De/Humanize enums #1490

KillyMXI opened this issue Mar 16, 2024 · 0 comments

Comments

@KillyMXI
Copy link

KillyMXI commented Mar 16, 2024

My understanding is that there is currently DehumanizeTo extension that is intended to work exactly in reverse of Humanize.
At the same time, it is not necessarily what I expect as a user of the library.

public enum ExampleEnum
{
    [Display(Name = "The foo", Description = "something about what effect Foo makes")]
    Foo,
    [Display(Name = "The bar", Description = "something about what effect Bar makes")]
    Bar,
}

I don't expect to parse input string in the format of description.
I expect the input script to be the best approximation of a parseable name.

And so, I think the reasonable string-to-value dictionary may consist of normalized strings taken from ToString value, display name, description, whatever else can be located in the attributes...

Current implementation prioritizes Description and takes only one string:

static bool TryGetDescription(MemberInfo member, [NotNullWhen(true)] out string? description)
{
var displayAttribute = member.GetCustomAttribute<DisplayAttribute>();
if (displayAttribute != null)
{
description = displayAttribute.GetDescription() ?? displayAttribute.GetName();
return description != null;
}

I don't understand the utility of this. It breaks valid usage scenarios.
And it seems that for a workaround I have to reimplement a custom dehumanizer from scratch.

This is probably related to #700.

By extension, if I need the Humanize function - I will face the same issue - I don't necessarily need the description - I may prefer to:

  • use Name attribute if it is available;
  • or humanize ToString value.

To resolve both problems, alternative implementation to current EnumCache and extensions might be conceived, probably in parallel to the existing one to avoid breaking change.
It may be not exactly what I expect it to be to solve each direction issue, but I'd like this to be a started for a discussion at least.

Seems like Humanizer might've been around before System.ComponentModel.DataAnnotations.DisplayAttribute and is still based on more limited assumptions.

From DisplayAttribute.cs:

Property Intended purpose
Name The name is generally used as the field label for a UI element bound to the member bearing this attribute.
ShortName The short name is generally used as the grid column label for a UI element bound to the member bearing this attribute.
Description Description is generally used as a tool tip or description a UI element bound to the member bearing this attribute.
Prompt A prompt is generally used as a prompt or watermark for a UI element bound to the member bearing this attribute.
GroupName A group name is used for grouping fields into the UI.

Of those, I only really see Name and ShortName as well suitable for de/humanization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant