generator

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package generator provides a code generator for enum types. It reads Go source files and extracts enum values to generate a new type with text marshaling support by default. Optional flags add SQL, BSON (MongoDB), and YAML support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertLiteralToInt added in v0.4.0

func ConvertLiteralToInt(lit *ast.BasicLit) (int, error)

ConvertLiteralToInt tries to convert a basic literal to an integer value

func EvaluateBinaryExpr added in v0.4.0

func EvaluateBinaryExpr(expr *ast.BinaryExpr, iotaVal int) (value int, usesIota bool, err error)

EvaluateBinaryExpr evaluates binary expressions like iota + 1 Returns: - value: the computed value of the expression - usesIota: whether the expression uses iota - error: any error encountered

Types

type Generator

type Generator struct {
	Type string // the private type name (e.g., "status")
	Path string // output directory path
	// contains filtered or unexported fields
}

Generator holds the data needed for enum code generation

func New

func New(typeName, path string) (*Generator, error)

New creates a new Generator instance

func (*Generator) Generate

func (g *Generator) Generate() error

Generate creates the enum code file. it takes the const values found in Parse and creates a new type with json, sql and text marshaling support. the generated code includes:

  • exported type with private name and value fields (e.g., Status{name: "active", value: 1})
  • string representation (String method)
  • text marshaling (MarshalText/UnmarshalText methods)
  • sql marshaling (Value/Scan methods for driver.Valuer and sql.Scanner)
  • parsing functions (Parse/Must variants)
  • exported const values (e.g., StatusActive)
  • helper functions to get all values and names

func (*Generator) Parse

func (g *Generator) Parse(dir string) error

Parse reads the source directory and extracts enum information. it looks for const values that start with the enum type name, for example if type is "status", it will find all const values that start with "status". The values must use iota and be in sequence. The values map will contain the const name and its iota value, for example: {"statusActive": 1, "statusInactive": 2}

func (*Generator) SetGenerateBSON added in v0.5.0

func (g *Generator) SetGenerateBSON(v bool)

SetGenerateBSON enables or disables generation of BSON interfaces

func (*Generator) SetGenerateGetter added in v0.3.0

func (g *Generator) SetGenerateGetter(generate bool)

SetGenerateGetter sets the flag to generate getter methods for enum values

func (*Generator) SetGenerateSQL added in v0.5.0

func (g *Generator) SetGenerateSQL(v bool)

SetGenerateSQL enables or disables generation of SQL interfaces

func (*Generator) SetGenerateYAML added in v0.5.0

func (g *Generator) SetGenerateYAML(v bool)

SetGenerateYAML enables or disables generation of YAML interfaces

func (*Generator) SetLowerCase

func (g *Generator) SetLowerCase(lower bool)

SetLowerCase sets the lower case flag for marshal/unmarshal values

type Value

type Value struct {
	PrivateName string   // e.g., "statusActive"
	PublicName  string   // e.g., "StatusActive"
	Name        string   // e.g., "Active"
	Index       int      // enum index value
	Aliases     []string // e.g., ["rw", "read-write"] from // enum:alias=rw,read-write
}

Value represents a single enum value

Jump to

Keyboard shortcuts

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