Documentation
¶
Overview ¶
Package genstruct provides an importable package for generating static golang struct arrays.
Index ¶
Constants ¶
const ( LevelDebug = "debug" LevelInfo = "info" LevelWarn = "warn" LevelError = "error" )
Verbosity levels
Variables ¶
This section is empty.
Functions ¶
func GetLogger ¶ added in v0.0.5
GetLogger returns the default logger or initializes a new one if it doesn't exist
func InitLogger ¶ added in v0.0.5
InitLogger initializes the logger with command line flags
Types ¶
type Config ¶
type Config struct {
// PackageName defines the target package name
// If not provided, defaults to "generated"
PackageName string
// TypeName is the name of the struct type to generate
// If not provided, inferred from the struct type in the data
TypeName string
// ConstantIdent is the prefix for constants (e.g., "Post" for "PostMyPostID")
// If not provided, defaults to the TypeName
ConstantIdent string
// VarPrefix is the prefix for variables (e.g., "Post" for "PostMyPost")
// If not provided, defaults to the TypeName
VarPrefix string
// OutputFile is the output file name
// If not provided, defaults to lowercase(typename_generated.go)
OutputFile string
// IdentifierFields are the fields to try using for naming, in priority order
// If not provided, defaults to ["ID", "Name", "Slug", "Title", "Key", "Code"]
IdentifierFields []string
// CustomVarNameFn is a custom function to generate variable names (optional)
// If provided, this takes precedence over IdentifierFields
CustomVarNameFn func(structValue reflect.Value) string
// Logger is the slog.Logger instance to use for logging
// If not provided, defaults to a no-op logger
Logger *slog.Logger
}
Config holds the configuration for code generation of static structs and arrays. Many fields are optional and will be automatically inferred if not specified.
type Generator ¶
type Generator struct {
Config Config
Data any // The primary array of structs to generate code for
Refs map[string]any // Additional arrays that can be referenced
File *jen.File
}
Generator is responsible for generating code for static struct arrays
func NewGenerator ¶
NewGenerator creates a new generator instance with support for struct references
Parameters:
- config: Configuration options for code generation
- data: The primary array of structs to generate code for
- refs: Optional additional arrays that can be referenced by the primary data
The refs parameters enable the use of struct tags with the `structgen` tag to reference data between structs. For example, a Post struct with a TagSlugs field can reference Tag structs using the tag `structgen:"TagSlugs"`.
When Generate() is called, it will: 1. Generate constants, variables, and a slice for the primary data 2. Generate constants, variables, and a slice for each referenced data set 3. Create references between the primary data and the referenced data
Reference fields can be either direct structs or pointers to structs:
- []Tag `structgen:"TagSlugs"` - Direct struct references
- []*Tag `structgen:"TagSlugs"` - Pointer-based struct references (recommended)
Example usage:
generator, err := genstruct.NewGenerator(config, posts, tags)
if err != nil {
// handle error
}
Many configuration options can be omitted and will be inferred automatically:
- TypeName: Inferred from the struct type in the data slice
- ConstantIdent: Defaults to TypeName if not specified
- VarPrefix: Defaults to TypeName if not specified
- OutputFile: Defaults to lowercase(typename_generated.go) if not specified
- IdentifierFields: Has reasonable defaults if not specified
Returns an error if:
- The data is not a slice or array
- The data is empty (no elements to analyze)
- The data elements are not structs
- Required fields couldn't be inferred
func (*Generator) Generate ¶
Generate performs the code generation for both primary data and reference data
This method generates: 1. Constants for the primary data's IDs 2. Variables for each item in the primary data 3. A slice containing all primary data items 4. Constants for each reference data set's IDs 5. Variables for each item in each reference data set 6. A slice for each reference data set 7. Creates references between primary data and reference data as specified by structgen tags
All generated code is written to a single output file specified in the Config.
type InvalidTypeError ¶
InvalidTypeError is returned when the type of the data is not a struct.
func (InvalidTypeError) Error ¶
func (e InvalidTypeError) Error() string
Error returns the error message
type NonSliceOrArrayError ¶
NonSliceOrArrayError is returned when the data is not a slice or array.
func (NonSliceOrArrayError) Error ¶
func (e NonSliceOrArrayError) Error() string
Error returns the error message
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
blog-posts-tags
command
|
|
|
logging-circus-show
command
Package main shows how to use the generator with a custom logger.
|
Package main shows how to use the generator with a custom logger. |
|
zoo
command
|