codegen

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package codegen provides shared types and utilities for code generation tools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindTypeAfterGenerateDirective

func FindTypeAfterGenerateDirective(dir, filename, generatorName string) (string, error)

FindTypeAfterGenerateDirective finds the struct type declared immediately after a go:generate directive.

func FindTypeAfterLine

func FindTypeAfterLine(filename string, lineNum int) (string, error)

FindTypeAfterLine finds the struct type declared immediately after the given line number.

Types

type FieldInfo

type FieldInfo struct {
	Name           string
	Type           string   // Full type string (e.g., "[]string", "map[string]any")
	TypeExpr       ast.Expr // Original AST expression
	TypeName       string   // Base type name (e.g., "string", "Tag")
	TypePkg        string   // Package prefix if any (e.g., "time" for time.Time)
	IsPointer      bool     // Field is a pointer type
	IsSlice        bool     // Field is a slice
	IsMap          bool     // Field is a map
	IsStruct       bool     // Field is a named struct type (not basic)
	MapKeyType     string   // Key type for maps
	MapValType     string   // Value type for maps
	SliceType      string   // Element type for slices
	Tag            string   // Struct tag
	NeedsDeep      bool     // Requires deep copy (for copy generator)
	StructTypeName string   // Name of struct type for calling methods
	SliceElemIsPtr bool     // Slice element is pointer to struct
}

FieldInfo holds information about a struct field.

type GeneratorConfig

type GeneratorConfig struct {
	TypeName     string
	SourceFile   string
	SourceDir    string
	SourcePkg    string
	OutputDir    string
	OutputPkg    string
	GenerateTest bool
	GenerateJSON bool // For layerbroker: generate JSON marshalling methods
}

GeneratorConfig holds common configuration for generators.

type ImportInfo

type ImportInfo struct {
	Path  string
	Alias string
}

ImportInfo holds information about an import.

func CollectRequiredImports

func CollectRequiredImports(fields []FieldInfo, fileImports []ImportInfo) []ImportInfo

CollectRequiredImports determines which imports are needed for generated code.

type StructInfo

type StructInfo struct {
	Name       string
	Fields     []FieldInfo
	Imports    []ImportInfo
	SourceFile string // The file where this struct was found (for nested structs)
	Package    string // Package name if this is an external package struct (e.g., "duration")
	ImportPath string // Full import path for external package structs
}

StructInfo holds information about a parsed struct type.

func FindExternalStruct added in v0.0.6

func FindExternalStruct(sourceDir, importPath, typeName string) (*StructInfo, error)

FindExternalStruct finds a struct type in an external package. It resolves the import path relative to the source directory.

func FindNestedStructs

func FindNestedStructs(dir, filename string, info *StructInfo) ([]*StructInfo, error)

FindNestedStructs finds all struct types referenced by the given struct. It searches all .go files in the directory to find nested types. It also finds external package structs and marks them appropriately.

func FindStructInPackage added in v0.0.5

func FindStructInPackage(dir, typeName string) (*StructInfo, error)

FindStructInPackage searches all .go files in the directory for a struct type.

func ParseStruct

func ParseStruct(dir, filename, typeName string) (*StructInfo, error)

ParseStruct parses a Go source file and extracts struct information.

type Subtool

type Subtool interface {
	Name() string
	Description() string
	Run(cfg GeneratorConfig) error
}

Subtool defines the interface for code generation subtools.

type TemplateGenerator

type TemplateGenerator struct {
	FuncMap template.FuncMap
}

TemplateGenerator handles template-based code generation.

func NewTemplateGenerator

func NewTemplateGenerator(customFuncs template.FuncMap) *TemplateGenerator

NewTemplateGenerator creates a new TemplateGenerator with optional custom functions.

func (*TemplateGenerator) GenerateFile

func (g *TemplateGenerator) GenerateFile(outputFile, tmplText string, data any) error

GenerateFile executes a template and writes the formatted output to a file.

Directories

Path Synopsis
Package copy implements the deep copy code generation subtool.
Package copy implements the deep copy code generation subtool.
Package equals implements the equals code generation subtool.
Package equals implements the equals code generation subtool.
Package merge implements the merge code generation subtool.
Package merge implements the merge code generation subtool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL