Skip to content

A linter for Go that verifies a piece of code handles all the possible values of an enum

License

Notifications You must be signed in to change notification settings

reillywatson/enumcover

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

-CircleCI -codecov

enumcover is a linter for Go to check if a piece of code handles all versions of an enum.

Background: Go enums are typically defined either as ints, or as strings. Here's an example from the Go reflect package:

type Kind uint

const (
	Invalid Kind = iota
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	Array
	Chan
	Func
	Interface
	Map
	Ptr
	Slice
	String
	Struct
	UnsafePointer
)

Here's another one, defined as a string:

type HttpVerb string

const (
	HttpGet     = HttpVerb("GET")
	HttpHead    = HttpVerg("HEAD")
	HttpPost    = HttpVerb("POST")
	HttpPut     = HttpVerb("PUT")
	HttpPatch   = HttpVerb("PATCH")
	HttpDelete  = HttpVerb("DELETE")
	HttpConnect = HttpVerb("CONNECT")
	HttpOptions = HttpVerb("OPTIONS")
	HttpTrace   = HttpVerb("TRACE")
)

You might have a function that tries to deal with one of these enums. It would be nice to know that your code is guaranteed to handle all the possible values of it, even if more get added! Enter enumcover.

Simply prepend your function (or switch statement, or slice, or whatever) with a comment like // enumcover:HttpVerb, and this will check that all consts of that type are explicitly handled.

About

A linter for Go that verifies a piece of code handles all the possible values of an enum

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages