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

Provide a centralised solution for setting a display text on enum values #1440

Closed
adecler opened this issue Oct 20, 2022 · 3 comments · Fixed by #1441
Closed

Provide a centralised solution for setting a display text on enum values #1440

adecler opened this issue Oct 20, 2022 · 3 comments · Fixed by #1441
Assignees
Labels
type:feature New capability or enhancement

Comments

@adecler
Copy link
Member

adecler commented Oct 20, 2022

Description:

Currently enum values are shown as they are defined in the code. For example, Central Europe will show as CentralEurope since we cannot have spaces in an enum name.

To solve that, I have been using the Description attribute to store a UI friendly text representation along with my own methods to convert enums to and from text. As the number of cases for this solution grows, it would be better to switch to a centralised solution.

It is however important to not that, within the BHoM, the Description attribute has already been used on enums to provide actual descriptions and cannot therefore be used to contain the display text.

image

I suggest to use a new attribute: DisplayTextAttribute that will serve that single purpose. The ParseEnum method in the engine will need to be updated to support that new attribute. We will also need to update teh ToText method so it now looks for the DisplayText attribute on enum values.

@adecler adecler added the type:feature New capability or enhancement label Oct 20, 2022
@adecler adecler self-assigned this Oct 20, 2022
@FraserGreenroyd
Copy link
Contributor

Hi @adecler just wondering if there's any benefit to just adding the ability to split an enum value at a capital letter and space it out to avoid another attribute to look after?

var enumOptions = Enum.GetValues(typeof(NonProjectOption));

var r = new Regex(@"
    (?<=[A-Z])(?=[A-Z][a-z]) |
    (?<=[^A-Z])(?=[A-Z]) |
    (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);

    List<string> options = new List<string>();

    foreach (var option in enumOptions)
        options.Add(r.Replace(option.ToString(), " "));

This is something I did for a similar use case - however, I do accept this only allows us to change CentralEurope to Central Europe whereas I'm guessing your approach would allow us to change it to be Central EU - covering the mainland countries (i.e. being able to add more text 'description' to the value)?

@adecler
Copy link
Member Author

adecler commented Oct 20, 2022

@FraserGreenroyd , that is not always that simple.
I have other cases where the enum needs to look something like this: 1. Substructure for example.
Any special character is causing problems so it is simpler to let the coder define exactly how the enum needs to look instead of trying to come up with conventions for each special case.

@FraserGreenroyd
Copy link
Contributor

Makes sense 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New capability or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants