-
Notifications
You must be signed in to change notification settings - Fork 67
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
Flag for case sensitive in <Type>String(s string) #40
Comments
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. |
I wonder if instead of a flag we just add a new case sensitive method? |
@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:
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? |
@samiam2013 thoughts? |
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...package main
import "fmt"
//go:generate enumer -type ExampleLowerFirst -linecomment
type ExampleLowerFirst uint8
const (
LFLower ExampleLowerFirst = iota // example
LFUpper // EXAMPLE
)
//go:generate enumer -type ExampleUpperFirst -linecomment
type ExampleUpperFirst uint8
const (
UFUpper ExampleUpperFirst = iota // EXAMPLE
UFLower // example
)
func main() {
fmt.Println("ExampleLowerFirst:")
var e1 ExampleLowerFirst
e1, _ = ExampleLowerFirstString("EXAMPLE")
fmt.Println(uint8(e1), e1)
e1, _ = ExampleLowerFirstString("example")
fmt.Println(uint8(e1), e1)
fmt.Println("ExampleUpperFirst:")
var e2 ExampleUpperFirst
e2, _ = ExampleUpperFirstString("EXAMPLE")
fmt.Println(uint8(e2), e2)
e2, _ = ExampleUpperFirstString("example")
fmt.Println(uint8(e2), e2)
} 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. |
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)
.The text was updated successfully, but these errors were encountered: