yamlenums

command module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

README

yamlenums

Build Status GoReportCard

yamlenums is a tool to automate the creation of methods that satisfy the yaml.Marshaler and yaml.Unmarshaler interfaces. Given the name of a (signed or unsigned) integer type T that has constants defined, yamlenums will create a new self-contained Go source file implementing

func (t T) MarshalYAML() ([]byte, error)
func (t *T) UnmarshalYAML(unmarshal func(v 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.

yamlenums is a copy and a small rework of https://github.com/campoy/jsonenums.

jsonenums is a simple implementation of a concept and the code might not be the most performant or beautiful to read.

For example, given this snippet,

package painkiller

type Pill int

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

running this command

yamlenums -type=Pill

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

func (r Pill) MarshalYAML() ([]byte, error)
func (r *Pill) UnmarshalYAML(unmarshal func(v interface{}) error) error

MarshalYAML will translate the value of a Pill constant to the []byte representation of the respective constant name, so that the call yaml.Marshal(painkiller.Aspirin) will return the bytes []byte("\"Aspirin\"").

UnmarshalYAML performs the opposite operation; it unmarshals underlying bytes to a string, given the string representation of a Pill constant it will change the receiver to equal the corresponding constant. So given the string "Aspirin" the receiver will change to Aspirin and the returned error will be nil.

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

//go:generate yamlenums -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_yamlenums.go, where t is the lower-cased name of the first type listed. The suffix can be overridden with the -suffix flag and a prefix may be added with the -prefix flag.

Documentation

Overview

YAMLenums is a tool to automate the creation of methods that satisfy the fmt.Stringer, yaml.Marshaler and yaml.Unmarshaler interfaces. Given the name of a (signed or unsigned) integer type T that has constants defined, yamlenums will create a new self-contained Go source file implementing

func (t T) String() string
func (t T) MarshalYAML() ([]byte, error)
func (t *T) UnmarshalYAML([]byte) 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.

YAMLenums is a simple implementation of a concept and the code might not be the most performant or beautiful to read.

For example, given this snippet,

package painkiller

type Pill int

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

running this command

yamlenums -type=Pill

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

func (r Pill) String() string
func (r Pill) MarshalYAML() ([]byte, error)
func (r *Pill) UnmarshalYAML([]byte) error

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 yamlenums -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_yamlenums.go, where t is the lower-cased name of the first type listed. The suffix can be overridden with the -suffix flag and a prefix may be added with the -prefix flag.

Directories

Path Synopsis
Package parser parses Go code and keeps track of all the types defined and provides access to all the constants defined for an int type.
Package parser parses Go code and keeps track of all the types defined and provides access to all the constants defined for an int type.

Jump to

Keyboard shortcuts

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