You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Flag for case sensitive in <Type>String(s string) #40
in my case, i'm converting iota to 1 character string and they includes both lower and upper version of some alphabets. i've read #21 , so maybe add a flag to enable case sensitive in <Type>String(s string).
@dmarkham@rucciva This just caused a bug in an app I built, so I decided to propose some fixes. The current behavior causes a silent bug if there are two consts with the same value other than a case change, like here.
My two ideas to fix are:
Add a -casesensitive flag, which disables all generation of lower-cased values.
This required more code changes up front, and users have to know to add the -casesensitive flag, but the resulting functionality completely eliminates the possibility of choosing the incorrect value.
This method also has the benefit of not changing existing code after users update enumer. Code would only change after adding -casesensitive.
Unfortunately, users may not realize there is a -casesensitive flag and could introduce bugs in their code in the future.
Split _typeNameToValueMap into _typeNameToValueMap and _typeLowerNameToValueMap.
_typeNameToValueMap is checked first with a fallback to the lower-cased map. This lets the current case-insensitivity behavior stay the same, while also choosing the correct value if there are overlaps.
This route seems like a safer option since users don't need to be aware of a flag, but code will be slightly different after people update enumer. Is that ok?
Unfortunately, the golden files will all need to be updated if we go this route. Is there are way to re-generate them? (Used the golden test to regenerate).
I opened a PR for both of these solutions, but only one needs to be merged...although both being merged might be nice. The first solution would be nice for times where casing should explicitly be respected, while the second prevents a bug without requiring a flag.
Just want to note that a workaround with the current behavior is to define consts with upper-case values before the lower-cased ones. Here's a quick repro:
ExampleLowerFirst:
1 EXAMPLE
1 EXAMPLE
ExampleUpperFirst:
0 EXAMPLE
1 example
Both of my PRs correct this behavior, resulting in:
ExampleLowerFirst:
1 EXAMPLE
0 example
ExampleUpperFirst:
0 EXAMPLE
1 example
The more I think about it, the more I'm preferring the second option since it fixes this bug without any developer intervention, other than re-generating.
Activity
dmarkham commentedon Jan 31, 2021
yea I knew someone would not be very happy with this change.. If you have a nice patch for a new flag I'm happy to review.
dmarkham commentedon Apr 6, 2021
I wonder if instead of a flag we just add a new case sensitive method?
<Type>SensitiveString(s string)maybe someone has a better name for it.feat: Add `-casesensitive` flag
feat: Add `-casesensitive` flag
feat: Move lower-cased names to a separate map to improve case support
gabe565 commentedon Oct 11, 2024
@dmarkham @rucciva This just caused a bug in an app I built, so I decided to propose some fixes. The current behavior causes a silent bug if there are two consts with the same value other than a case change, like here.
My two ideas to fix are:
-casesensitiveflag, which disables all generation of lower-cased values.-casesensitiveflag, but the resulting functionality completely eliminates the possibility of choosing the incorrect value.-casesensitive.-casesensitiveflag and could introduce bugs in their code in the future.-casesensitiveflag #96_typeNameToValueMapinto_typeNameToValueMapand_typeLowerNameToValueMap._typeNameToValueMapis checked first with a fallback to the lower-cased map. This lets the current case-insensitivity behavior stay the same, while also choosing the correct value if there are overlaps.Unfortunately, the golden files will all need to be updated if we go this route. Is there are way to re-generate them?(Used the golden test to regenerate).I opened a PR for both of these solutions, but only one needs to be merged...although both being merged might be nice. The first solution would be nice for times where casing should explicitly be respected, while the second prevents a bug without requiring a flag.
Any thoughts?
feat: Move lower-cased names to a separate map to improve case support
dmarkham commentedon Oct 11, 2024
@samiam2013 thoughts?
gabe565 commentedon Oct 11, 2024
Just want to note that a workaround with the current behavior is to define consts with upper-case values before the lower-cased ones. Here's a quick repro:
Click to expand...
The code above results in:
Both of my PRs correct this behavior, resulting in:
The more I think about it, the more I'm preferring the second option since it fixes this bug without any developer intervention, other than re-generating.
Split exact and lower-cased name lookup into two maps