generator

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirectValue

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

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

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

func ParseAliases

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

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

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

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

func NewGeneratorConfig() *GeneratorConfig

type Option

type Option func(*GeneratorConfig)

Option is a function that modifies a Generator

func WithAliases

func WithAliases(aliases map[string]string) Option

WithAliases will set up aliases for the generator.

func WithBuildTags

func WithBuildTags(tags ...string) Option

WithBuildTags will add build tags to the generated file.

func WithCaseInsensitiveParse

func WithCaseInsensitiveParse() Option

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

func WithFlag

func WithFlag() Option

WithFlag is used to add flag methods to the enum

func WithForceLower

func WithForceLower() Option

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

func WithForceUpper

func WithForceUpper() Option

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

func WithJsonPkg

func WithJsonPkg(pkg string) Option

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

func WithLowercaseVariant

func WithLowercaseVariant() Option

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

func WithMarshal

func WithMarshal() Option

WithMarshal is used to add marshalling to the enum

func WithMustParse

func WithMustParse() Option

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

func WithNames

func WithNames() Option

WithNames is used to add Names methods to the enum

func WithNoComments

func WithNoComments() Option

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

func WithNoIota

func WithNoIota() Option

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

func WithNoPrefix

func WithNoPrefix() Option

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

func WithPrefix

func WithPrefix(prefix string) Option

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

func WithPtr

func WithPtr() Option

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

func WithSQLDriver

func WithSQLDriver() Option

WithSQLDriver is used to add marshalling to the enum

func WithSQLInt

func WithSQLInt() Option

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

func WithSQLNullInt

func WithSQLNullInt() Option

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

func WithSQLNullStr

func WithSQLNullStr() Option

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

func WithTemplates

func WithTemplates(filenames ...string) Option

WithTemplates is used to provide the filenames of additional templates.

func WithValues

func WithValues() Option

WithValues is used to add Values methods to the enum

func WithoutSnakeToCamel

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