go-enumerator

command module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2022 License: MIT Imports: 1 Imported by: 0

README

Description

go-enumerator is a code generation tool designed for making constants behave more like enums. The generated methods allow users to:

  1. Convert numeric constants to string representations using fmt.Print(x)
  2. Parse string representations using fmt.Scan("Name", &x)
  3. Check if variables hold valid enum values using x.Defined()
  4. Iterate through all defined enum values using x.Next()

go-enumerator is designed to be invoked by go generate, but it can be used as a command-line tool as well.


Go Reference
Additional documentation available at pkg.go.dev

Installation

Installation is easy, just install the package using the go install tool.

go install github.com/ajjensen13/go-enumerator

Overview

Below is an example of the intended use for go-enumerate. All command line arguments are optional go generate. The tool will use the $GOFILE, $GOPACKAGE, and $GOLINE environment variables to find the type declaration immediately following to //go:generate comment.

//go:generate go-enumerator
type Kind int

const (
	Kind1
	Kind2
)

In this case, we found the Kind type, which is a suitable type for generating an enum definition for. The following methods are created in a new file with the default file name.

// String implements fmt.Stringer
func (k Kind) String() string { /* omitted for brevity */ }

// Scan implements fmt.Scanner
func (k *Kind) Scan(ss fmt.ScanState, verb rune) error { /* omitted for brevity */ }

// Defined returns true if k holds a defined value
func (k Kind) Defined() bool { /* omitted for brevity */ }

// Next returns the next defined value after k
func (k Kind) Next() Kind { /* omitted for brevity */ }

String() and Scan() can be used in conjunction with the fmt package to parse and encode values into human-friendly representations.

Next() can be used to loop through all defined values for an enum.

Defined() can be used to ensure that a given variable holds a defined value.

Remarks

  • go-enumerator was inspired by stringer, which is a better String() generator. If all you need is a String() method for a numeric constant, consider using that tool instead.
  • Examples for how to use the generated code can be found at https://pkg.go.dev/github.com/ajjensen13/go-enumerator/example
  • If you find this tool useful, give the repo a star! Feel free leave issues and/or suggest fixes or improvements as well 🙂

Documentation

Overview

go-enumerator is a tool designed to be called by go:generate for generating enum-like code from constants.

Be default, go-enumerator will look for a type definition immediately following the go:generate statement from which it was called, and will generate methods for that type.

For example, given code similar to what is shown below

//go:generate go-enumerator
type Kind int

const (
	Kind1
	Kind2
)

go-enumerator will generate implementations for the following methods

// String implements fmt.Stringer
func (k Kind) String() string { /* omitted for brevity */ }

// Scan implements fmt.Scanner
func (k *Kind) Scan(ss fmt.ScanState, verb rune) error { /* omitted for brevity */ }

// Defined returns true if k holds a defined value
func (k Kind) Defined() bool { /* omitted for brevity */ }

// Next returns the next defined value after k
func (k Kind) Next() Kind { /* omitted for brevity */ }

Hopefully, the default behavior will serve your needs, but if not it can be changed by supplying command-line arguments to the program. For help with the cli, run with the --help argument.

go-enumerator --help

Enjoy 😀

Directories

Path Synopsis
internal
cmd

Jump to

Keyboard shortcuts

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