Documentation
¶
Overview ¶
Package genstruct provides an importable package for generating static golang struct arrays.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// PackageName defines the Target package name (optional)
// If not provided, the package name will be the same as the given struct.
PackageName string
TypeName string // The name of the struct type to generate
ConstantIdent string // Prefix for constants (e.g., "Post" for "PostMyPostID")
VarPrefix string // Prefix for variables (e.g., "Post" for "PostMyPost")
OutputFile string // Output file name
IdentifierFields []string // Fields to try using for naming, in priority order (optional)
// Custom function to generate variable names (optional)
// If provided, this takes precedence over IdentifierFields
CustomVarNameFn func(structValue reflect.Value) string
}
Config holds the configuration for code generation of static structs and arrays.
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"`.
Example usage:
generator := genstruct.NewGenerator(config, posts, tags)
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