enumer

package
v0.9.3 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: BSD-2-Clause Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIsNaN    = errors.New("typed value is NaN")
	ErrIsPosInf = errors.New("typed value is +Inf")
	ErrIsNegInf = errors.New("typed value is -Inf")
)
View Source
var (
	MAGIC_MARKER     = regexp.MustCompile(`^//go:enum[ ]?`)
	GEN_ENUMER_FILE  = regexp.MustCompile(`^// Code generated by "go-enumer \(github\.com/mvrahden/go-enumer\)"; DO NOT EDIT\.(?:$|\n)`)
	IS_NUMERIC_VALUE = regexp.MustCompile(`^\-?\d+`)
	IS_TYPED_HEADER  = regexp.MustCompile(`^.+(\(.+\))$`) // e.g. uint32(xyz)
)
View Source
var ExtractCommentString = func(c *ast.Comment) string {
	return c.Text
}

ExtractCommentString is a Noop func, but allows to be intercepted during tests to clean comment texts from test artifacts while keeping production code unaffected.

Functions

func AssignEnumConstBlockToType

func AssignEnumConstBlockToType(node ast.Node, typesInfo *types.Info, genFile *ast.File, enumTypes []*EnumType) (token.Pos, error)

AssignEnumConstBlockToType evaluates the current node for a possible const block spec/enum values. If the given node does not fulfill the requirements for a possible const block spec, it returns zero values. If the given node violates the requirements for possible const block spec, it returns an error and the token position. It otherwise determines all const block values and assigns them to relevant enumtype from the given slice of enum types.

func DetectGeneratedFile

func DetectGeneratedFile(files []*ast.File) (genFile *ast.File)

DetectGeneratedFile determines the generated enumer file. If no such file exists, it will return `nil`.

func TypeToString

func TypeToString(t types.BasicKind) string

Types

type AdditionalData

type AdditionalData struct {
	Headers []*AdditionalDataHeader
	Rows    [][]*AdditionalDataCell
}

type AdditionalDataCell

type AdditionalDataCell struct {
	LiteralValue string // hint: formatted source representation of the value, e.g. literal strings are quoted
	TypedValue   any    // hint: parsed value; actual type depends on header type
}

type AdditionalDataHeader

type AdditionalDataHeader struct {
	Name string          // hint: the column name as-is (from CSV)
	Type types.BasicKind // hint: the type inferred by type syntax
}

type EnumConstBlock

type EnumConstBlock struct {
	Node  *ast.GenDecl
	Specs []*EnumValueSpec // hint: all value specs of a block
}

type EnumType

type EnumType struct {
	Node       *ast.GenDecl
	Config     *EnumTypeConfig
	Spec       *EnumTypeSpec // hint: the specification derived either from const block notation or from a file
	ConstBlock *EnumConstBlock
}

func DetermineEnumType

func DetermineEnumType(node ast.Node, typesInfo *types.Info, genFile *ast.File) (*EnumType, token.Pos, error)

DetermineEnumType evaluates given node for enum types. If the given node is not fulfilling the requirements for a possible enum type declaration it returns zero values. If the given node violates the requirements for a eum declaration it will return an error and the token position.

func (*EnumType) CrossValidateConstBlockWithSpec

func (e *EnumType) CrossValidateConstBlockWithSpec(fset *token.FileSet, typesInfo *types.Info) error

func (*EnumType) DetectMagicComment

func (e *EnumType) DetectMagicComment() (c *ast.Comment)

DetectMagicComment retrieves the magic comment from the list of comments. It assumes that a magic comment exists.

func (*EnumType) GetPkgFS

func (e *EnumType) GetPkgFS(fset *token.FileSet) (fs.FS, bool)

func (*EnumType) GetTypeVia

func (e *EnumType) GetTypeVia(ti *types.Info) types.Type

func (*EnumType) HasFileSpec

func (e *EnumType) HasFileSpec() bool

HasFileSpec indicates whether or not the EnumType is configured with a source file. Its usage is legal for AFTER the config has been loaded.

func (*EnumType) HasSimpleBlockSpec

func (e *EnumType) HasSimpleBlockSpec() bool

func (*EnumType) LoadFileSpec

func (e *EnumType) LoadFileSpec(fset *token.FileSet) error

func (*EnumType) LoadSimpleBlockSpec

func (e *EnumType) LoadSimpleBlockSpec() error

func (*EnumType) LoadSpec

func (e *EnumType) LoadSpec(fset *token.FileSet) error

func (*EnumType) Name

func (e *EnumType) Name() *ast.Ident

func (*EnumType) ParseMagicComment

func (e *EnumType) ParseMagicComment(mc *ast.Comment, opts *config.Options) error

func (*EnumType) ValidateConstBlock

func (e *EnumType) ValidateConstBlock(fset *token.FileSet, typesInfo *types.Info) error

func (*EnumType) ValidateEnumTypeConfig

func (e *EnumType) ValidateEnumTypeConfig(fset *token.FileSet) error

func (*EnumType) ValidateSpec

func (e *EnumType) ValidateSpec(fset *token.FileSet, typesInfo *types.Info) error

type EnumTypeConfig

type EnumTypeConfig struct {
	Node *ast.Comment

	Options    *config.Options
	FromSource string
}

func DefaultConfig

func DefaultConfig(cfg *config.Options) *EnumTypeConfig

type EnumTypeSpec

type EnumTypeSpec struct {
	Type           SpecType
	Values         []*EnumTypeSpecValue
	AdditionalData *AdditionalData
}

type EnumTypeSpecValue

type EnumTypeSpecValue struct {
	ID            uint64
	EnumValue     string
	IsAlternative bool           // hint: an alternative value
	ConstSpec     *EnumValueSpec // hint: if derived from const value
}

type EnumValueSpec

type EnumValueSpec struct {
	Node  *ast.ValueSpec
	Value uint64
}

func (*EnumValueSpec) GetObjectVia

func (e *EnumValueSpec) GetObjectVia(ti *types.Info) types.Object

func (*EnumValueSpec) GetTypeVia

func (e *EnumValueSpec) GetTypeVia(ti *types.Info) types.Type

type SpecType

type SpecType uint8
const (
	SimpleBlockSpec SpecType = iota
	FilebasedSpec
)

Jump to

Keyboard shortcuts

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