generator

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: MIT Imports: 17 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirectValue added in v0.9.0

func DirectValue(enumType string, val EnumValue) (strResult string)

DirectValue returns the exact value of the enum, not adjusted for iota at all.

func Mapify

func Mapify(e Enum) (ret string, err error)

Mapify returns a map that is all of the indexes for a string value lookup

func Namify added in v0.1.4

func Namify(e Enum) (ret string, err error)

Namify returns a slice that is all of the possible names for an enum in a slice

func Offset added in v0.4.2

func Offset(index int, enumType string, val EnumValue) (strResult string)

func ParseAliases added in v0.3.8

func ParseAliases(aliases []string) (map[string]string, error)

ParseAliases is used to add aliases to replace during name sanitization.

func Stringify

func Stringify(e Enum, forceLower, forceUpper bool) (ret string, err error)

Stringify returns a string that is all of the enum value names concatenated without a separator

func Unmapify

func Unmapify(e Enum, lowercase bool) (ret string, err error)

Unmapify returns a map that is all of the indexes for a string value lookup

func UnmapifyStringEnum added in v0.5.0

func UnmapifyStringEnum(e Enum, lowercase bool) (ret string, err error)

Unmapify returns a map that is all of the indexes for a string value lookup

Types

type Enum

type Enum struct {
	Name    string
	Prefix  string
	Type    string
	Values  []EnumValue
	Comment string
}

Enum holds data for a discovered enum in the parsed source

type EnumValue

type EnumValue struct {
	RawName      string
	Name         string
	PrefixedName string
	ValueStr     string
	ValueInt     any
	Comment      string
}

EnumValue holds the individual data for each enum value within the found enum.

type Generator

type Generator struct {
	Version   string
	Revision  string
	BuildDate string
	BuiltBy   string
	GeneratorConfig
	// contains filtered or unexported fields
}

Generator is responsible for generating validation files for the given in a go source file.

func NewGenerator

func NewGenerator(options ...Option) *Generator

NewGenerator is a constructor method for creating a new Generator with default templates loaded.

func NewGeneratorWithConfig added in v0.8.0

func NewGeneratorWithConfig(config GeneratorConfig) *Generator

NewGeneratorWithConfig is a constructor method for creating a new Generator with a configuration struct instead of using the functional options pattern.

func (*Generator) Generate

func (g *Generator) Generate(f *ast.File) ([]byte, error)

Generate does the heavy lifting for the code generation starting from the parsed AST file.

func (*Generator) GenerateFromFile

func (g *Generator) GenerateFromFile(inputFile string) ([]byte, error)

GenerateFromFile is responsible for orchestrating the Code generation. It results in a byte array that can be written to any file desired. It has already had goimports run on the code before being returned.

type GeneratorConfig added in v0.8.0

type GeneratorConfig struct {
	NoPrefix          bool              `json:"no_prefix"`
	NoIota            bool              `json:"no_iota"`
	LowercaseLookup   bool              `json:"lowercase_lookup"`
	CaseInsensitive   bool              `json:"case_insensitive"`
	Marshal           bool              `json:"marshal"`
	SQL               bool              `json:"sql"`
	SQLInt            bool              `json:"sql_int"`
	Flag              bool              `json:"flag"`
	Names             bool              `json:"names"`
	Values            bool              `json:"values"`
	LeaveSnakeCase    bool              `json:"leave_snake_case"`
	JSONPkg           string            `json:"json_pkg"`
	Prefix            string            `json:"prefix"`
	SQLNullInt        bool              `json:"sql_null_int"`
	SQLNullStr        bool              `json:"sql_null_str"`
	Ptr               bool              `json:"ptr"`
	MustParse         bool              `json:"must_parse"`
	ForceLower        bool              `json:"force_lower"`
	ForceUpper        bool              `json:"force_upper"`
	NoComments        bool              `json:"no_comments"`
	BuildTags         []string          `json:"build_tags"`
	ReplacementNames  map[string]string `json:"replacement_names"`
	TemplateFileNames []string          `json:"template_file_names"`
}

GeneratorConfig holds all the configuration options for the Generator

func NewGeneratorConfig added in v0.8.0

func NewGeneratorConfig() *GeneratorConfig

type Option added in v0.8.0

type Option func(*GeneratorConfig)

Option is a function that modifies a Generator

func WithAliases added in v0.8.0

func WithAliases(aliases map[string]string) Option

WithAliases will set up aliases for the generator.

func WithBuildTags added in v0.8.0

func WithBuildTags(tags ...string) Option

WithBuildTags will add build tags to the generated file.

func WithCaseInsensitiveParse added in v0.8.0

func WithCaseInsensitiveParse() Option

WithCaseInsensitiveParse is used to change the enum const values generated to not have the enum on them.

func WithFlag added in v0.8.0

func WithFlag() Option

WithFlag is used to add flag methods to the enum

func WithForceLower added in v0.8.0

func WithForceLower() Option

WithForceLower is used to force enums names to lower case while keeping variable names the same.

func WithForceUpper added in v0.8.0

func WithForceUpper() Option

WithForceUpper is used to force enums names to upper case while keeping variable names the same.

func WithJsonPkg added in v0.8.0

func WithJsonPkg(pkg string) Option

WithJsonPkg is used to add a custom json package to the imports

func WithLowercaseVariant added in v0.8.0

func WithLowercaseVariant() Option

WithLowercaseVariant is used to change the enum const values generated to not have the enum on them.

func WithMarshal added in v0.8.0

func WithMarshal() Option

WithMarshal is used to add marshalling to the enum

func WithMustParse added in v0.8.0

func WithMustParse() Option

WithMustParse is used to add a method `MustParse` that will panic on failure.

func WithNames added in v0.8.0

func WithNames() Option

WithNames is used to add Names methods to the enum

func WithNoComments added in v0.8.0

func WithNoComments() Option

WithNoComments is used to remove auto generated comments from the enum.

func WithNoIota added in v0.9.0

func WithNoIota() Option

WithNoIota is used to generate enum constants with explicit values instead of using iota.

func WithNoPrefix added in v0.8.0

func WithNoPrefix() Option

WithNoPrefix is used to change the enum const values generated to not have the enum on them.

func WithPrefix added in v0.8.0

func WithPrefix(prefix string) Option

WithPrefix is used to add a custom prefix to the enum constants

func WithPtr added in v0.8.0

func WithPtr() Option

WithPtr adds a way to get a pointer value straight from the const value.

func WithSQLDriver added in v0.8.0

func WithSQLDriver() Option

WithSQLDriver is used to add marshalling to the enum

func WithSQLInt added in v0.8.0

func WithSQLInt() Option

WithSQLInt is used to signal a string to be stored as an int.

func WithSQLNullInt added in v0.8.0

func WithSQLNullInt() Option

WithSQLNullInt is used to add a null int option for SQL interactions.

func WithSQLNullStr added in v0.8.0

func WithSQLNullStr() Option

WithSQLNullStr is used to add a null string option for SQL interactions.

func WithTemplates added in v0.8.0

func WithTemplates(filenames ...string) Option

WithTemplates is used to provide the filenames of additional templates.

func WithValues added in v0.8.0

func WithValues() Option

WithValues is used to add Values methods to the enum

func WithoutSnakeToCamel added in v0.8.0

func WithoutSnakeToCamel() Option

WithoutSnakeToCamel is used to add flag methods to the enum

Jump to

Keyboard shortcuts

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