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 json, bson and text marshaling support.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertLiteralToInt ¶ added in v0.4.0
ConvertLiteralToInt tries to convert a basic literal to an integer value
func EvaluateBinaryExpr ¶ added in v0.4.0
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 (*Generator) Generate ¶
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 ¶
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) SetGenerateGetter ¶ added in v0.3.0
SetGenerateGetter sets the flag to generate getter methods for enum values
func (*Generator) SetLowerCase ¶
SetLowerCase sets the lower case flag for marshal/unmarshal values