Added new flag to generate a custom error for stringNameToValueMethod#111
Added new flag to generate a custom error for stringNameToValueMethod#111bincyber wants to merge 2 commits intodmarkham:masterfrom
Conversation
|
Please review @samiam2013 @dmarkham when you get a chance. Thanks in advance |
|
Why use a custom error for every different enumer type you've generated? What if enumer just included a single type |
|
@bincyber giving you an @ so you can see my question above |
|
@bincyber if I don't get a response here, since this seems like an in-demand feature and the other user seemed to have a usecase for non-enum-specific errors (so they work with any enum and you don't have to know a specific error type) I might make a run at this myself. Hope that won't offend |
Sorry for the delayed reply.
This makes sense to me. I'll make the necessary changes shortly. |
…rror types - added a new public package containing a custom error type: InvalidEnumValueError - modified the code to return this new custom error type when -customerror flag is used - removed usage of deprecated ioutil library
721e15b to
b69b324
Compare
|
@samiam2013 I thought about returning the new custom error by default and removing the func DayString(s string) (Day, error) {
if val, ok := _DayNameToValueMap[s]; ok {
return val, nil
}
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, enumer.InvalidEnumValueError
}Like you suggested, this could be modified to use func DayString(s string) (Day, error) {
if val, ok := _DayNameToValueMap[s]; ok {
return val, nil
}
if val, ok := _DayNameToValueMap[strings.ToLower(s)]; ok {
return val, nil
}
return 0, errors.Join(enumer.InvalidEnumValueError, fmt.Errorf("%s does not belong to Day values", s))
}I'm leaning towards the second option. What do you prefer? |
|
I think the second option is great, especially if we can keep the string text and prove it still handles any naive substring or regex check emulating a "sentinel error". I'm worried specifically about anyone with a regex including a specification of I'd like to avoid a breaking change for anyone if possible, so one option is to take the route of joining an exported error but still gating it behind a flag, |
|
I'll close this PR in favor of #112. Let's get this in! |
This PR adds a flag to allow a custom error type to be generated for the stringNameToValueMethod, so that the error type can be used with
errors.Is()Example:
This would generate:
Resolves #110