enums

package module
v1.0.0-beta.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2021 License: MIT Imports: 5 Imported by: 0

README

enums

Uses the AST to find all variables of a certain type in a package. This is useful for cases where you need to ensure that all values of a type is accounted for somewhere. There is an overlap with the exhaustive project which does this for switch statements.

For the sake of this package an enum is a named type with multiple values which you're operating on as a collection. They could be strings, ints, and even complex types like functions or structs.

Example

For example, I have an enum type for feature flags. For each request I call a 3rdparty service to check the state of each flag, so whenever a new flag is created it needs to be added to the All function.

type Flag string

const (
    DeployAllTheThings Flag = "deploy-all-the-things"
    // This flag is new and we need to remember to update AllFlags below
    DeployOneThing     Flag = "deploy-one-thing"
)

func AllFlags() []Flag {
    return []Flag{
        DeployAllTheThings,
    }
}

Using enums we can test that we haven't missed out on a new flag:

func TestAllFlagsCovered(t *testing.T) {
    enumstest.NoDiff(t, "./feature", "feature.Flag", full.AllFlags())
}

License

See the LICENSE file for license rights and limitations (MIT).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection []Enum

Collection contains found matches from All and can be diffed against values

func All

func All(pkg string, typ string) (Collection, error)

All finds all variables of typ in pkg

pkg is a full or relative path.
typ is a type in the form: pkgname.Type

Example: All("./feature", "feature.Flag")

func (Collection) Diff

func (e Collection) Diff(actual interface{}) Diff

Diff indicates differences between a collection and any slice Because a Collection stores all values as strings the difference is calculated based on the string representation of the value.

type Diff

type Diff struct {
	Missing Collection
	Extra   []string
}

Diff contains the result of checking the difference between a Collection and a list of values

func (Diff) String

func (d Diff) String() string

String outputs a human summary of the values in the diff

func (Diff) Zero

func (d Diff) Zero() bool

Zero returns whether there is nothing in the diff

type Enum

type Enum struct {
	Type  string
	Name  string
	Value string // all values are represented as strings from the AST
}

Enum represents a a match of a type found in the AST for a package

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL