Categorygithub.com/bombsimon/enum
modulepackage
1.0.0
Repository: https://github.com/bombsimon/enum.git
Documentation: pkg.go.dev

# README

Enum

Auto generate your types and implement various interfaces by only creating constants. All you need to do is add a go generate line above your constant group.

//go:generate go-enum --trim "Post" --format snake
const (
    PostCreate PostType = 0
    PostRead   PostType = 2
    PostUpdate PostType = 4
    PostDelete PostType = 8
)

If you just want an enum and don't care about the value, use Go's iota.

//go:generate go-enum --trim "Direction" --format upper
const (
    DirectionUp Direction = iota
    DirectionDown
    DirectionLeft
    DirectionRight
)

If you want to use the actual constant and it's value but treat it as an enum to support validation and conversion from a fixed type, set the value to a string. The Stringer() interface will be implemented just like other enums but by adding --with-value you implement Value() which returns the constants actual value.

//go:generate go-enum --trim "Answer" --format capitalize-first --no-json --with-value
const (
	AnswerYes   YesOrNo = "Y"
	AnswerNo    YesOrNo = "N"
	AnswerMaybe YesOrNo = "M"
)

You can now create types from either the constant value or the trimmed constant name and also validate the type if created manually. If the constnat value is needed, e.g. while storing you can use Value() but for printing String() will return the full name.

func main() {
	var (
		y, _ = YesOrNoFromString("Y")     // AnswerYes
		m, _ = YesOrNoFromString("Maybe") // AnswerMaybe
		bad  = YesOrNo("Bad")
	)

	fmt.Println(y.String())   // Yes
	fmt.Println(m.Value())    // M
	fmt.Println(m.Valid())    // True
	fmt.Println(bad.Valid())  // False
	fmt.Println(bad.String()) // ""
	fmt.Println(bad.Value())  // ""
}

See the example folder for example generated code.

Installation

go get -u github.com/bombsimon/enum/...

Interfaces

The current interfaces that will be implemented are

  • FromString - Convert a string to the enum type
  • String - Get the string value for the enum
  • Value - Get the actual value of the enum
  • Valid - Returns true or false if the enum is valid
  • MarshalJSON - JSON marshal interface
  • UnmarshalJSON - JSON unmarshal interface

Flags

The following flags can be used.

  • json - Don't generate JSON interface by setting to false
  • value - Set to true to generate Value() and allow FromString() to accept the enums actual value as well
  • format - The way to format the constant.
  • trim - What part of the constant to trim.

Format functions

The string representation of the enum can beformatted in multiple ways. The value will be the name of the constant mins the part that's been trimmed off by setting --trim. Below are examples assuming --trim is set to Prefix.

MethodFlag nameConstantString() value
Snake casesnakePrefixThisStringthis_string
Camel casecamelPrefixSomeValueSomeValue
UpperupperPrefixMyValueMYVALUE
LowerlowerPrefixMoreConstantsmoreconstants
First letterfirstPrefixJustFirstLetterJ (will preserve casing)
First letter upperfirst-upperPrefixSomeValueS (will convert to upper)
First letter lowerfirst-lowerPrefixAnotherValuea (will convert to lower)
Capitalize first wordcapitalize-firstPrefixThisIsWordsThis is words
Capitalzie all wordscapitalize-allPrefixThisIsAlsoWordsThis Is Also Words

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
New will create a new parser to use for a given file.

# Structs

Enum is one enum with a number mapped to a string.
Parser is the parser that will search a file for constants and add each constant as an enum.

# Type aliases

FormatFunc is the function to format the string value of the enum.