Documentation ¶
Overview ¶
Package enumgen provides functions for generating enum methods for enum types.
Index ¶
- Constants
- Variables
- func Generate(config *config.Config) error
- func SplitIntoRuns(values []Value) [][]Value
- func Usize(n int) int
- type ByValue
- type File
- type Generator
- func (g *Generator) AddPackage(pkg *packages.Package)
- func (g *Generator) BuildBasicExtras(runs [][]Value, typeName string, isBitFlag bool, runsThreshold int)
- func (g *Generator) BuildBitFlagMethods(runs [][]Value, typeName string)
- func (g *Generator) BuildJSONMethods(runs [][]Value, typeName string, runsThreshold int)
- func (g *Generator) BuildMap(runs [][]Value, typeName string, isBitFlag bool)
- func (g *Generator) BuildMultipleRuns(runs [][]Value, typeName string, isBitFlag bool)
- func (g *Generator) BuildNoOpOrderChangeDetect(runs [][]Value, typeName string)
- func (g *Generator) BuildOneRun(runs [][]Value, typeName string, isBitFlag bool)
- func (g *Generator) BuildTextMethods(runs [][]Value, typeName string, runsThreshold int)
- func (g *Generator) BuildYAMLMethods(runs [][]Value, typeName string, runsThreshold int)
- func (g *Generator) CreateIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string)
- func (g *Generator) CreateLowerIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string)
- func (g *Generator) DeclareIndexAndNameVar(run []Value, typeName string)
- func (g *Generator) DeclareIndexAndNameVars(runs [][]Value, typeName string)
- func (g *Generator) DeclareNameVars(runs [][]Value, typeName string, suffix string)
- func (g *Generator) FindEnumTypes() error
- func (g *Generator) Format() ([]byte, error)
- func (g *Generator) Generate() error
- func (g *Generator) InspectForType(n ast.Node) (bool, error)
- func (g *Generator) ParsePackage() error
- func (g *Generator) PrefixValueNames(values []Value, c *config.Config)
- func (g *Generator) PrintDescMap(runs [][]Value, typeName string)
- func (g *Generator) PrintDescSlice(runs [][]Value, typeName string)
- func (g *Generator) PrintHeader()
- func (g *Generator) PrintNamesSlice(runs [][]Value, typeName string, runsThreshold int)
- func (g *Generator) PrintValueMap(runs [][]Value, typeName string, runsThreshold int)
- func (g *Generator) Printf(format string, args ...any)
- func (g *Generator) TransformValueNames(values []Value, c *config.Config) error
- func (g *Generator) TrimValueNames(values []Value, c *config.Config)
- func (g *Generator) Write() error
- type Package
- type Type
- type Value
Constants ¶
const ( // StringMethodName is the name of the String method StringMethodName = `String` // StringMethodComment is the comment for the String method. // Arguments to format are: // // [1]: type name StringMethodComment = `// String returns the string representation // of this %[1]s value.` // BitIndexStringMethodName is the name of the BitIndexString method BitIndexStringMethodName = `BitIndexString` // BitIndexStringMethodComment is the comment for the BitIndexString method. // Arguments to format are: // // [1]: type name BitIndexStringMethodComment = `` /* 174-byte string literal not displayed */ )
const JSONMethods = `` /* 432-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringBelongsMethodLoop = `` /* 192-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringBitFlag = `` /* 289-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringDescMethod = `` /* 158-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringDescsMethod = `` /* 216-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringHasBitFlagMethod = `` /* 158-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringInt64Method = `// Int64 returns the %[1]s value as an int64.
func (i %[1]s) Int64() int64 {
return int64(i)
}
`
Arguments to format are:
[1]: type name
const StringIsValidMethod = `` /* 143-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringMap = `` /* 147-byte string literal not displayed */
Arguments to format are:
[1]: type name [2]: method name (String or BitIndexString) [3]: method comment
const StringNConstant = `//%[1]sN is the highest valid value
// for type %[1]s, plus one.
const %[1]sN %[1]s = %[2]d
`
Arguments to format are:
[1]: type name [2]: number of constants for type
const StringOneRun = `` /* 195-byte string literal not displayed */
Arguments to format are:
[1]: type name [2]: size of index element (8 for uint8 etc.) [3]: less than zero check (for signed types) [4]: method name (String or BitIndexString) [5]: method comment
const StringOneRunWithOffset = `` /* 217-byte string literal not displayed */
Arguments to format are:
[1]: type name [2]: lowest defined value for type, as a string [3]: size of index element (8 for uint8 etc.) [4]: less than zero check (for signed types) [5]: method name (String or BitIndexString) [6]: method comment
const StringSetBitFlagMethod = `` /* 343-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringSetInt64Method = `// SetInt64 sets the %[1]s value from an int64.
func (i *%[1]s) SetInt64(in int64) {
*i = %[1]s(in)
}
`
Arguments to format are:
[1]: type name
const StringSetStringBitFlagMethod = `` /* 497-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringSetStringMethod = `` /* 384-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringStringsMethod = `` /* 227-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringValuesGlobal = `` /* 230-byte string literal not displayed */
Arguments to format are:
[1]: type name
const StringValuesMethod = `` /* 286-byte string literal not displayed */
Arguments to format are:
[1]: type name
const TextMethods = `` /* 306-byte string literal not displayed */
Arguments to format are:
[1]: type name
const YAMLMethods = `` /* 323-byte string literal not displayed */
Arguments to format are:
[1]: type name
Variables ¶
var AllowedEnumTypes = map[string]bool{"int": true, "int64": true, "int32": true, "int16": true, "int8": true, "uint": true, "uint64": true, "uint32": true, "uint16": true, "uint8": true}
AllowedEnumTypes are the types that can be used for enums that are not bit flags (bit flags can only be int64s). It is stored as a map for quick and convenient access.
Functions ¶
func Generate ¶
Generate generates enum methods using the given configuration object. It reads all Go files in the config source directory and writes the result to the config output file. It is a simple entry point to enumgen that does all of the steps; for more specific functionality, create a new Generator with NewGenerator and call methods on it.
func SplitIntoRuns ¶
SplitIntoRuns breaks the values into runs of contiguous sequences. For example, given 1,2,3,5,6,7 it returns {1,2,3},{5,6,7}. The input slice is known to be non-empty.
Types ¶
type ByValue ¶
type ByValue []Value
ByValue is a sorting method that sorts the constants into increasing order. We take care in the Less method to sort in signed or unsigned order, as appropriate.
type File ¶
type File struct { Pkg *Package // Package to which this file belongs. File *ast.File // Parsed AST. // These fields are reset for each type being generated. TypeName string // The name of the constant type we are currently looking for. BitFlag bool // Whether the constant type we are currently looking for is a bitflag. Values []Value // Accumulator for constant values of that type. Config *config.Config // The configuration information }
File holds a single parsed file and associated data.
type Generator ¶
type Generator struct { Config *config.Config // The configuration information Buf bytes.Buffer // The accumulated output. Pkg *Package // The package we are scanning. Types []Type // The enum types HasBitFlag bool // Whether there is any bit flag enum type in the package (used for determining imports) }
Generator holds the state of the generator. It is primarily used to buffer the output.
func NewGenerator ¶
NewGenerator returns a new generator with the given configuration information.
func (*Generator) AddPackage ¶
AddPackage adds a type-checked Package and its syntax files to the generator.
func (*Generator) BuildBasicExtras ¶
func (g *Generator) BuildBasicExtras(runs [][]Value, typeName string, isBitFlag bool, runsThreshold int)
BuildBasicExtras builds methods common to all types, like Desc and SetString.
func (*Generator) BuildBitFlagMethods ¶
BuildBitFlagMethods builds methods specific to bit flag types.
func (*Generator) BuildJSONMethods ¶
func (*Generator) BuildMap ¶
BuildMap handles the case where the space is so sparse a map is a reasonable fallback. It's a rare situation but has simple code.
func (*Generator) BuildMultipleRuns ¶
BuildMultipleRuns generates the variables and String method for multiple runs of contiguous values. For this pattern, a single Printf format won't do.
func (*Generator) BuildNoOpOrderChangeDetect ¶
BuildNoOpOrderChangeDetect lets the compiler and the user know if the order/value of the enum values has changed.
func (*Generator) BuildOneRun ¶
BuildOneRun generates the variables and String method for a single run of contiguous values.
func (*Generator) BuildTextMethods ¶
func (*Generator) BuildYAMLMethods ¶
func (*Generator) CreateIndexAndNameDecl ¶
func (g *Generator) CreateIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string)
CreateIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var".
func (*Generator) CreateLowerIndexAndNameDecl ¶
func (g *Generator) CreateLowerIndexAndNameDecl(run []Value, typeName string, suffix string) (string, string)
createIndexAndNameDecl returns the pair of declarations for the run. The caller will add "const" and "var".
func (*Generator) DeclareIndexAndNameVar ¶
DeclareIndexAndNameVar is the single-run version of declareIndexAndNameVars
func (*Generator) DeclareIndexAndNameVars ¶
DeclareIndexAndNameVars declares the index slices and concatenated names strings representing the runs of values.
func (*Generator) DeclareNameVars ¶
DeclareNameVars declares the concatenated names string representing all the values in the runs.
func (*Generator) FindEnumTypes ¶
FindEnumTypes goes through all of the types in the package and finds all integer (signed or unsigned) types labeled with enums:enum or enums:bitflag. It stores the resulting types in [Generator.Types].
func (*Generator) Format ¶
Format returns the contents of the Generator's buffer ([Generator.Buf]) with goimports applied.
func (*Generator) Generate ¶
Generate produces the enum methods for the types stored in [Generator.Types].
func (*Generator) InspectForType ¶
InspectForType looks at the given AST node and adds it to [Generator.Types] if it is marked with an appropriate comment directive. It returns whether the AST inspector should continue, and an error if there is one. It should only be called in ast.Inspect.
func (*Generator) ParsePackage ¶
ParsePackage parses the single package located in the configuration directory.
func (*Generator) PrefixValueNames ¶
PrefixValueNames adds the prefix specified in config.Config.AddPrefix to each name of the given values.
func (*Generator) PrintDescMap ¶
PrintDescMap prints the map of values to descriptions
func (*Generator) PrintDescSlice ¶
PrintDescSlice prints the slice of descriptions
func (*Generator) PrintHeader ¶
func (g *Generator) PrintHeader()
PrintHeader prints the header and package clause to the accumulated output
func (*Generator) PrintNamesSlice ¶
PrintNamesSlice prints the slice of names
func (*Generator) PrintValueMap ¶
PrintValueMap prints the map between name and value
func (*Generator) Printf ¶
Printf prints the formatted string to the accumulated output in [Generator.Buf]
func (*Generator) TransformValueNames ¶
TransformValueNames transforms the names of the given values according to the transform method specified in config.Config.Transform
func (*Generator) TrimValueNames ¶
TrimValueNames removes the prefixes specified in config.Config.TrimPrefix from each name of the given values.
type Package ¶
type Package struct { Dir string Name string Defs map[*ast.Ident]types.Object Files []*File TypesPkg *types.Package }
Package holds information about a Go package
type Type ¶
type Type struct { Type *ast.TypeSpec // The standard AST type value IsBitFlag bool // Whether the type is a bit flag type Config *config.Config // Configuration information set in the comment directive for the type; is initialized to generator config info first }
Type represents a parsed enum type.
type Value ¶
type Value struct { OriginalName string // The name of the constant before transformation Name string // The name of the constant after transformation (i.e. camel case => snake case) Desc string // The comment description of the constant // The Value is stored as a bit pattern alone. The boolean tells us // whether to interpret it as an int64 or a uint64; the only place // this matters is when sorting. // Much of the time the str field is all we need; it is printed // by Value.String. Value uint64 // Will be converted to int64 when needed. Signed bool // Whether the constant is a signed type. Str string // The string representation given by the "go/constant" package. }
Value represents a declared constant.