enums

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

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

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 9 Imported by: 0

README

enums

godocs.io

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. Enums supports what Go refers to as basic literals (strings, numbers) and 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 AllFlags 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())
}

Using with structs

We need a way to uniquely identify values in a struct, so the identifier will still have to be a basic literal, and this is done with the enums:"identifier" tag.

type DefaultFlag struct {
    Name string `enums:"identifier"`
    IsOn bool
}

License

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

Documentation

Overview

Package enums helps you find instances when you find cases where you have not updated code to handle all enums you have. This is not a fully automated like exhaustive(https://github.com/nishanths/exhaustive) but rather intended to be used in cases where you might want to apply your own logic while still being warned when new cases pop up.

Enums supports basic literals and structs tagged with `enum:"identifier"`.

The entry point for enums is the All function which takes a package path and a type and will return a Collection of found instances of that type. A collection can produce a Diff given a slice of objects.

There is a helper for the standard case of "match all of this type" in enumstest.NoDiff.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection struct {
	Type      string // the import path of the type
	FieldName string // if the underlying type is a struct this value is the name of the field that is used to distinguish flags
	Enums     []Enum // all distinct values found
}

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

func All

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

All finds variables of typ in pkg.

Example:

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

func (Collection) Diff

func (c 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 {
	Name  string
	Value string
}

Enum represents a value for a matched type.

Example:

var MyFlag Flag = "Hello"

Is equivalent to:

Enum{Name: "MyFlag", Value: "Hello"}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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