enum-marshaler

command module
v0.0.0-...-d418278 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2018 License: BSD-3-Clause Imports: 17 Imported by: 0

README

Go Report Card

Enum Marshaler

Enum-marshaler is a tool to automate the creation of methods that satisfy the encoding.TextMarshaler, encoding.TextUnmarshaler, and fmt.Stringer interfaces.

TextMarshaler and TextUnmarshaler are notably used by json or yaml encodings, which makes it easy and natural to use enums in the code with string representation in those encodings.

The initial implementation is a copy of https://godoc.org/golang.org/x/tools/cmd/stringer to which we added the generation of MarshalText and UnmarshalText, as well as an option to strip prefixes from constant names in (un)marshaling functions.

Install

go get -u github.com/orus-io/enum-marshaler
go install github.com/orus-io/enum-marshaler

Usage

Usage is the same as stringer, with only one extra option, and more code generated.

Given this snippet:

package painkiller

type Pill int

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

Running this command:

enum-marshaler -type Pill

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

func (Pill) String() string
func (Pill) MarshalText() []byte, err
func (*Pill) UnmarshalText([]byte) err

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

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

Documentation

Overview

Stringer is a tool to automate the creation of methods that satisfy the fmt.Stringer interface. Given the name of a (signed or unsigned) integer type T that has constants defined, enum-marshaler will create a new self-contained Go source file implementing

func (t T) String() string

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.

Stringer 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

enum-marshaler -type=Pill

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

func (Pill) String() string

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 enum-marshaler -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.

Jump to

Keyboard shortcuts

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