Documentation
¶
Overview ¶
Package codegen provides YAML-driven code generation primitives for GoFastr.
Typical CLI-style usage discovers or loads a Config, registers built-in generators and extension commands on a Registry, runs generation into a FileSet, then writes files with WriteFiles. Embedders can register in-process generators and extensions directly and may skip command extension registration entirely.
Index ¶
- Constants
- func CleanEnabled(cfg CodegenConfig) bool
- func EnsureNoSymlinkLeaf(path string) error
- func EnsureNoSymlinkPath(path string) error
- func HasCodegenSection(path string) (bool, error)
- func LoadSource(projectDir string, source SourceConfig) (any, error)
- func SafeOutputRoot(root string) (string, error)
- func SafeRelativePath(path string) (string, error)
- func WriteFiles(files *FileSet, opts WriteOptions) error
- type CodegenConfig
- type Config
- type Context
- type Diagnostic
- type Discovery
- type Extension
- type ExtensionConfig
- type ExtensionRequest
- type ExtensionResponse
- type FileSet
- type GeneratedFile
- type Generator
- type GeneratorConfig
- type JSONDocument
- type Registry
- func (r *Registry) RegisterCommandExtensions(cfg CodegenConfig, stderrWriter io.Writer) error
- func (r *Registry) RegisterExtension(ext Extension) error
- func (r *Registry) RegisterGenerator(gen Generator) error
- func (r *Registry) Run(ctx context.Context, projectDir string, cfg Config) (*Context, error)
- type SourceConfig
- type WriteOptions
Constants ¶
const ManifestName = ".codegen-manifest.json"
ManifestName is the generated-file ownership manifest written under output roots.
const ProtocolVersion = 1
ProtocolVersion is the current external extension protocol version.
Variables ¶
This section is empty.
Functions ¶
func CleanEnabled ¶
func CleanEnabled(cfg CodegenConfig) bool
CleanEnabled reports whether clean mode is enabled, defaulting to true.
func EnsureNoSymlinkLeaf ¶
EnsureNoSymlinkLeaf rejects path when the final component is an existing symlink.
func EnsureNoSymlinkPath ¶
EnsureNoSymlinkPath rejects a path whose existing components include symlinks.
func HasCodegenSection ¶
HasCodegenSection reports whether path has a top-level codegen key.
func LoadSource ¶
func LoadSource(projectDir string, source SourceConfig) (any, error)
LoadSource loads the source configured for a generator.
func SafeOutputRoot ¶
SafeOutputRoot validates a root that may be cleaned.
func SafeRelativePath ¶
SafeRelativePath validates a generated path.
func WriteFiles ¶
func WriteFiles(files *FileSet, opts WriteOptions) error
WriteFiles writes generated files and records a manifest for future cleans.
Types ¶
type CodegenConfig ¶
type CodegenConfig struct {
Output string `json:"output"`
Clean *bool `json:"clean,omitempty"`
Generators []GeneratorConfig `json:"generators,omitempty"`
Extensions []ExtensionConfig `json:"extensions,omitempty"`
}
CodegenConfig configures one generator run.
type Config ¶
type Config struct {
Version int `json:"version"`
Codegen CodegenConfig `json:"codegen"`
}
Config is the top-level YAML code generation configuration.
func DecodeConfig ¶
DecodeConfig decodes a parsed YAML node into Config.
func LoadConfig ¶
LoadConfig reads and validates a YAML codegen configuration file.
type Context ¶
type Context struct {
ProjectDir string
Config Config
Metadata map[string]any
Inputs map[string]any
Files *FileSet
Diagnostics []Diagnostic
}
Context is the shared generation context passed to generators/extensions.
type Diagnostic ¶
Diagnostic is a machine-readable message produced during generation.
type Discovery ¶
Discovery is the result of looking for a codegen configuration file.
func DiscoverConfig ¶
DiscoverConfig finds the project codegen config. Dedicated codegen config files win; gofastr.yml is only considered when it has a codegen section.
type Extension ¶
type Extension interface {
Name() string
RunPhase(ctx context.Context, phase string, genCtx *Context, gen GeneratorConfig, ext ExtensionConfig) (ExtensionResponse, error)
}
Extension participates in code generation through named phases.
type ExtensionConfig ¶
type ExtensionConfig struct {
Name string `json:"name"`
Command []string `json:"command,omitempty"`
Config map[string]any `json:"config,omitempty"`
}
ExtensionConfig describes an extension process or in-process extension.
type ExtensionRequest ¶
type ExtensionRequest struct {
ProtocolVersion int `json:"protocol_version"`
Phase string `json:"phase"`
ProjectDir string `json:"project_dir"`
Generator GeneratorConfig `json:"generator"`
Extension ExtensionConfig `json:"extension"`
Source any `json:"source,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Files []GeneratedFile `json:"files,omitempty"`
}
ExtensionRequest is the JSON payload sent to external command extensions.
type ExtensionResponse ¶
type ExtensionResponse struct {
ProtocolVersion int `json:"protocol_version,omitempty"`
Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
Files []GeneratedFile `json:"files,omitempty"`
Deletes []string `json:"deletes,omitempty"`
}
ExtensionResponse is the JSON payload returned by extensions.
type FileSet ¶
type FileSet struct {
// contains filtered or unexported fields
}
FileSet stores pending generated files and rejects silent collisions.
func NewFileSet ¶
func NewFileSet() *FileSet
NewFileSet creates an empty pending generated-file collection.
func (*FileSet) Add ¶
func (fs *FileSet) Add(file GeneratedFile) error
Add inserts one pending generated file and rejects path/content collisions.
func (*FileSet) All ¶
func (fs *FileSet) All() []GeneratedFile
All returns pending files in stable path order.
func (*FileSet) DeleteOwned ¶
DeleteOwned removes a pending generated file only if it is owned by owner.
type GeneratedFile ¶
type GeneratedFile struct {
Path string `json:"path"`
Content string `json:"content"`
Owner string `json:"owner,omitempty"`
}
GeneratedFile is a pending file produced by a generator or extension.
type Generator ¶
type Generator interface {
Name() string
Generate(ctx context.Context, genCtx *Context, cfg GeneratorConfig) ([]GeneratedFile, error)
}
Generator emits files for one configured generator entry.
type GeneratorConfig ¶
type GeneratorConfig struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Extension string `json:"extension,omitempty"`
Source SourceConfig `json:"source,omitempty"`
Output string `json:"output,omitempty"`
Config map[string]any `json:"config,omitempty"`
}
GeneratorConfig configures one generator invocation.
type JSONDocument ¶
JSONDocument is one JSON file loaded by a json_dir source.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds available in-process generators and extensions.
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry creates an empty generator/extension registry.
func (*Registry) RegisterCommandExtensions ¶
func (r *Registry) RegisterCommandExtensions(cfg CodegenConfig, stderrWriter io.Writer) error
RegisterCommandExtensions registers command-backed extensions from config.
func (*Registry) RegisterExtension ¶
RegisterExtension adds one in-process extension by name.
func (*Registry) RegisterGenerator ¶
RegisterGenerator adds one in-process generator by name.
type SourceConfig ¶
type SourceConfig struct {
Type string `json:"type,omitempty"`
Path string `json:"path,omitempty"`
Config map[string]any `json:"config,omitempty"`
}
SourceConfig describes structured input consumed by a generator.
type WriteOptions ¶
WriteOptions controls writing a FileSet to disk.