enum

package
v1.2.113 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 19 Imported by: 1

Documentation

Overview

go-enum is a tool to automate the creation of methods that satisfy such interfaces:

fmt         ==>  fmt.Stringer
binary      ==>  encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
json        ==>  encoding/json.MarshalJSON and encoding/json.UnmarshalJSON
text        ==>  encoding.TextMarshaler and encoding.TextUnmarshaler
sql         ==>  database/sql.Scanner and database/sql/driver.Valuer
yaml        ==>  gopkg.in/yaml.v3:yaml.Marshaler and gopkg.in/yaml.v3:yaml.Unmarshaler

Given the name of a (signed or unsigned) integer type T that has constants defined, stringer will create a new self-contained Go source file implementing

fmt         ==>  fmt.Stringer
	func (t T) String() string
binary      ==>  encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
	func (t T) MarshalBinary() (data []byte, err error)
	func (t *T) UnmarshalBinary(data []byte) error
json        ==>  encoding/json.MarshalJSON and encoding/json.UnmarshalJSON
	func (t T) MarshalJSON() ([]byte, error)
	func (t *T) UnmarshalJSON(data []byte) error
text        ==>  encoding.TextMarshaler and encoding.TextUnmarshaler
	func (t T) MarshalText() ([]byte, error)
	func (t *T) UnmarshalText(text []byte) error
sql         ==>  database/sql.Scanner and database/sql/driver.Valuer
	func (t T) Value() (driver.Value, error)
	func (t *T) Scan(value interface{}) error
yaml        ==>  gopkg.in/yaml.v3:yaml.Marshaler and gopkg.in/yaml.v3:yaml.Unmarshaler
	func (t T) MarshalYAML() (interface{}, error)
	func (t *T) UnmarshalYAML(unmarshal func(interface{}) error) error

The file is created in the same package and directory as the package that defines T. It has helpful defaults designed for use with go generate.

go-enum works best with constants that are consecutive values such as created using iota, but creates good code regardless. In the future it might also provide custom support for constant sets that are bit patterns.

For example, given this snippet,

package painkiller

type Pill int

const (
	Placebo Pill = iota
	Aspirin
	Ibuprofen
	Paracetamol
	Acetaminophen = Paracetamol
)

running this command

go-enum -type=Pill

in the same directory will create the file pill_string.go, in package painkiller, containing a definition of interfaces mentioned.

That method will translate the value of a Pill constant to the string representation of the respective constant name, so that the call fmt.Print(painkiller.Aspirin) will print the string "Aspirin".

Typically this process would be run using go generate, like this:

//go:generate go-enum -type=Pill

If multiple constants have the same value, the lexically first matching name will be used (in the example, Acetaminophen will print as "Paracetamol").

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

The -type flag accepts a comma-separated list of types so a single run can generate methods for multiple types. The default output file is t_string.go, where t is the lower-cased name of the first type listed. It can be overridden with the -output flag.

The -linecomment flag tells stringer to generate the text of any line comment, trimmed of leading spaces, instead of the constant name. For instance, if the constants above had a Pill prefix, one could write

PillAspirin // Aspirin

to suppress it in the output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main()

func ParseCommandLine

func ParseCommandLine(def bool) *flag.FlagSet

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File holds a single parsed file and associated data.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator holds the state of the analysis. Primarily used to buffer the output for format.Source.

func (*Generator) Printf

func (g *Generator) Printf(format string, args ...interface{})

Printf format & write to the buf in this generator

type NameInfo

type NameInfo struct {
	// contains filtered or unexported fields
}

type Package

type Package struct {
	// contains filtered or unexported fields
}

Package holds a single parsed package and associated files and ast files.

type TypeInfo

type TypeInfo struct {
	// contains filtered or unexported fields
}

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value represents a declared constant. const NAME TYPE = VALUE

func (*Value) String

func (v *Value) String() string

type ValueInfo

type ValueInfo struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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