Documentation
¶
Overview ¶
Package generator provides code generation tools for value objects.
Package generator provides code generation tools for value objects.
This package contains utilities for generating value object code based on templates and configuration files. It is designed to automate the creation of value objects that follow the Value Object pattern from Domain-Driven Design.
The package provides:
- A Generator type for generating value object code and tests
- Configuration types for specifying value object properties
- Command-line interface for using the generator from the terminal
The generator supports two types of value objects:
- String-based value objects (wrapping a string type)
- Struct-based value objects (containing multiple fields)
Configuration is provided through JSON files that specify:
- The name and package of the value object
- The type of value object (string or struct)
- Fields and their types (for struct-based value objects)
- Validation rules
- Required imports
- Description of the value object
Example usage:
// Create a generator generator := generator.NewGenerator("templates", "output") // Configure a value object config := generator.ValueObjectConfig{ Name: "Email", Package: "contact", Type: generator.StringBased, BaseType: "string", Description: "Represents an email address", Validations: map[string]string{ "value": "ValidateEmail", }, } // Generate the value object code err := generator.Generate(config) if err != nil { // Handle error }
The package also provides a command-line interface through the Execute function, which is used by the valueobject/cmd/generate package to provide a standalone command-line tool.
Package generator provides code generation tools for value objects.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type GenerateCommand ¶
type GenerateCommand struct { // ConfigFile is the path to the configuration file. ConfigFile string // TemplateDir is the directory containing the templates. TemplateDir string // OutputDir is the directory where the generated code will be written. OutputDir string }
GenerateCommand is a command-line tool for generating value objects.
func NewGenerateCommand ¶
func NewGenerateCommand() *GenerateCommand
NewGenerateCommand creates a new GenerateCommand.
func (*GenerateCommand) ParseFlags ¶
func (c *GenerateCommand) ParseFlags()
ParseFlags parses the command-line flags.
type Generator ¶
type Generator struct { // TemplateDir is the directory containing the templates. TemplateDir string // OutputDir is the directory where the generated code will be written. OutputDir string }
Generator generates code for value objects.
func NewGenerator ¶
NewGenerator creates a new Generator.
func (*Generator) Generate ¶
func (g *Generator) Generate(config ValueObjectConfig) error
Generate generates code for a value object.
type ValueObjectConfig ¶
type ValueObjectConfig struct { // Name is the name of the value object (e.g., "Email"). Name string // Package is the package name (e.g., "contact"). Package string // Type is the type of value object (string or struct). Type ValueObjectType // Fields is a map of field names to field types (for struct-based value objects). Fields map[string]string // Validations is a map of field names to validation functions. Validations map[string]string // Imports is a list of additional imports. Imports []string // BaseType is the base type for string-based value objects (e.g., "string"). BaseType string // Description is a description of the value object. Description string }
ValueObjectConfig contains the configuration for generating a value object.
type ValueObjectType ¶
type ValueObjectType string
ValueObjectType represents the type of value object to generate.
const ( // StringBased represents a string-based value object. StringBased ValueObjectType = "string" // StructBased represents a struct-based value object. StructBased ValueObjectType = "struct" )