Skip to content
/ govalid Public

simple code generator implements Validate method on enums

License

Notifications You must be signed in to change notification settings

ufukty/govalid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

govalid

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.

Example

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")
}

Install

go install github.com/ufukty/govalid@latest

About

simple code generator implements Validate method on enums

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages