dmarkham / enumer

A Go tool to auto generate methods for your enums
Other
411 stars 63 forks source link

Make PillString case insensitive #21

Closed livingsilver94 closed 5 years ago

livingsilver94 commented 5 years ago

It would be useful to perform string -> enum conversion in a case-insensitive fashion. While I could convert my string to lowercase and have all of my values lowercase as well, some of them are acronyms which looks ugly if not written with capital letters.

dmarkham commented 5 years ago

Can you show me a sample of what your trying to do?

livingsilver94 commented 5 years ago
type Pill int
const (
    Placebo Pill = iota
    IDK
)

Now, IDK.String() returns "IDK" if no transformation is performed, and that's OK, but if I wanted to do this:

myPill := PillString("idk")

I would get an error because "idk" is lowercase. Suppose that the string comes from a CLI command: some users could write "idk", while others could write "IDK". I don't want to transform every pill to uppercase though because Placebo must stay as it is. Same rationale for Placebo, even: some could write "Placebo", others "placebo".

dmarkham commented 5 years ago

So I like the feature. I do not yet like my options on how I would implement this.

I want to keep things compatible with people relying on case-sensitivity not matter how silly that sounds.

I'll see what a patch will look like. Unless someone get there first.

dmarkham commented 5 years ago

@livingsilver94 I need more testing on this. But this seems like the cleanest way to pull this together.

livingsilver94 commented 5 years ago

Thank you for implementing it :+1: