govalid
is a code generator that implements a Validate method on a given type. The method returns an error when it is called; if the current value is not one of those constants declared in a given file in code generation.
Input (you provide):
type Planet string
const (
Earth = Planet("earth")
Mars = Planet("mars")
)
Command:
govalid -in filename.go -type Planet
Output:
func (p Planet) Validate() error {
switch p {
case Earth:
return nil
case Mars:
return nil
}
return fmt.Errorf("invalid value")
}
go install github.com/ufukty/govalid@latest