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

Please consider supporting bitwise OR operators as a valid separator when parsing enum types with a [flag] attribute. #679

Closed
jamesra opened this issue Feb 17, 2022 · 1 comment

Comments

@jamesra
Copy link

jamesra commented Feb 17, 2022

This is a minor issue but it tripped me up for a few minutes. I figured out how to encode flag values in enums when I tried to create a repro in the Fiddle, which was useful to have BTW.

Lets say I have the following flag enum type (simplified from the fiddle site)

[Flags]
public enum Experiences
{
  None = 0,
  TreeChopping = 1 << 0,
  CabinLiving = 1 << 1,
  BeardGrowing = 1 << 2,
  FlewInAirplane = 1 << 3
}

class Person{
  public Experience LifeExperiences {get;set;}
}

In my yaml settings file my first instinct was to encode as I would initialize the enum in C#, with bitwise OR operators indicating which flags were set:

LifeExperiences: TreeChopping | CabinLiving | BeardGrowing

When that produced a parsing error I tried a list hoping each entry would set the appropriate flag:

LifeExperiences: 
  - TreeChopping
  - CabinLiving
  - BeardGrowing

Which does not report a parse error. It simply fails to populate the enum (not sure if that behavior would be considered a bug or not).

Finally I noticed the serializer at the top of the Fiddle example and caught on that commas are used for multiple values in a flag enumeration:

LifeExperiences: TreeChopping, CabinLiving, BeardGrowing

My request is to recognize the | operator as a delimiter as that is how flag values are commonly set in code:
var lifeXP = Experiences.TreeChopping | Experiences.CabinLiving;

They are also represented with | delimiters in the Visual Studio debugger.

It is minor but it might save folks a few minutes here and there. Thanks for this useful library.

@EdwardCooke
Copy link
Collaborator

To convert it from the value in the YAML file to the enum, Enum.Parse is called which is a core .Net method. Currently we don't do any work on the string or value if the destination type is an enum. I guess we could do some string manipulation to convert pipes to commas for enums, though, not sure we want to do that and it would probably be better to leave it the way it is. Just for completeness, if you do a ToString on a flagged enum it returns commas and not pipes.

I'm going to close this for now, if you feel otherwise reopen and leave a comment.

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

2 participants