string-enumer

command module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: MIT Imports: 8 Imported by: 0

README

🧵 string-enumer

Go build status GoDoc Go Report Card

String enumer is a golang code generator for enums declared as strings.

The function func (v X) Valid() bool will always be generated on the defined types together with func XValues() []X. But options to generate more code exist. It is especially useful with the --text option, that generates an UnmarshalText function which forces any unmarshaling of the type (via for JSON/XML/etc.) to be limited to the defined types.

The tool is primarily intended to be used with go:generate, but can be used as a separate CLI tool.

Example usage with go generate

//go:generate string-enumer --text -t Country -o ./generated.go .
// or
//go:generate go run github.com/lindell/string-enumer --text -t Country -o ./generated.go .
type Country string

const (
	CountryCanada       Country = "CA"
	CountryChina        Country = "CN"
	CountrySweden       Country = "SE"
	CountryUnitedStates Country = "US"
)

When you run go generate for that package, it will generate:

// Valid validates if a value is a valid Country
func (v Country) Valid() bool {
	...
}

// CountryValues returns a list of all (valid) Country values
func CountryValues() []Country {
	...
}

// UnmarshalText takes a text, verifies that it is a correct Country and unmarshals it
func (v *Country) UnmarshalText(text []byte) error {
	...
}

(Please click this link for a real example at Go Playgrounds example)

CLI Description:

$ string-enumer --help
Usage of string-enumer:
	string-enumer [flags] --type T --type T2 [directory]
	string-enumer [flags] --type T --type T2 files... # Must be a single package
For more information, see:
	https://github.com/lindell/string-enumer
Flags:
  -o, --output string   output file name; default is stdout
  -T, --text            if set, text unmarshaling methods will be generated. Default: false
  -t, --type strings    the type name(s), can be multiple, but at least on must be set

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg
stringenumer
Package stringenumer generate useful code for enums defined as strings type MyEnum string const ( MyEnumThis MyEnum = "this" MyEnumThat MyEnum = "that" ) The code that is generated by default is a Valid function: func (e MyEnum) Valid() bool It can also generate an TextUnmarshaling function for the type that validates any string that is unmarshaled into this type.
Package stringenumer generate useful code for enums defined as strings type MyEnum string const ( MyEnumThis MyEnum = "this" MyEnumThat MyEnum = "that" ) The code that is generated by default is a Valid function: func (e MyEnum) Valid() bool It can also generate an TextUnmarshaling function for the type that validates any string that is unmarshaled into this type.

Jump to

Keyboard shortcuts

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