jmattheis / goverter

Generate type-safe Go converters by simply defining an interface
https://goverter.jmattheis.de/
MIT License
487 stars 46 forks source link

Strict enum support #61

Closed jmattheis closed 5 months ago

jmattheis commented 1 year ago

It would be cool if goverter could strictly convert named types / enums.

// goverter:converter
type Converter interface {
    Convert(InputColor) OutputColor
}

type InputColor int
const (
    InputGreen InputColor = iota
    InputBlue
    InputRed
)

type OutputColor string
const (
    OutputGreen OutputColor = "green"
    OutputBlue  OutputColor = "blue"
    OutputRed   OutputColor = "red"
)

Goverter should fail if the target enum is missing options from the source type.

Currently, goverter just converts the underlying value for named types. So a named string type will be converted to another named string type, without checking if the value is valid.

Unknowns:

Please :+1: this issue if you like this functionality

jmattheis commented 5 months ago

Fixed with v1.4.0. See Guide: Enum.