-
Notifications
You must be signed in to change notification settings - Fork 923
more strict checking for Scan for enum #2224
Unanswered
kontango-bot
asked this question in
Feature Requests & Ideas
-
I think it could be nice to check for the actual values in the generated Scan method:
this is the default:
type EnumType string const ( EnumTypeSuccess EnumType = "success" EnumTypeFailure EnumType = "success" ) func (e *EnumType) Scan(src interface{}) error { switch s := src.(type) { case []byte: *e = EnumType(s) case string: *e = EnumType(s) default: return fmt.Errorf("unsupported scan type for EnumType: %T", src) } return nil }
Could add something like:
type EnumType string const ( EnumTypeSuccess EnumType = "success" EnumTypeFailure EnumType = "success" ) // convenience function for returning all possible values func(e *EnumType) Values() []EnumType { return []EnumType{ EnumTypeSuccess EnumTypeFailure } } func (e *EnumType) Scan(src interface{}) error { switch s := src.(type) { case []byte: *e = EnumType(s) case string: *e = EnumType(s) switch s { case "success" *e = EnumTypeSuccess // etc default: return fmt.Errorf("string %s is not a member of enum EnumType", s) } default: return fmt.Errorf("unsupported scan type for EnumType: %T", src) } return nil }
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment