dmarkham / enumer

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

Feature req: support string enums #89

Open Proximyst opened 4 months ago

Proximyst commented 4 months ago

Hi! I have a use-case where I want to use enumer to map postgres enums with the -sql flag. There may be conflicts in my enums, hence I cannot declare multiple enums in the same package.

Example:

//go:generate ...
type EnumA int
const (
    Test EnumA = iota
    Cool EnumA
)

//go:generate ...
type EnumB int
const (
    VeryCool EnumB = iota
    Cool EnumB // this will cause issues!
)

It would be neat if we could instead declare the values as strings, e.g.:

//go:generate ...
type EnumA string
const (
    ATest EnumA = "TEST"
    ACool EnumA = "COOL"
)

//go:generate ...
type EnumB string
const (
    BVeryCool EnumB = "VERY_COOL"
    BCool EnumB = "COOL"
)
markuswustenberg commented 1 month ago

You could add a prefix to your consts and then strip it:

//go:generate go run github.com/dmarkham/enumer -type EnumA -trimprefix EnumA
type EnumA int
const (
    EnumATest EnumA = iota
    EnumACool
)

Would that work?