gen

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package gen implements Python module introspection and Go code generation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoFuncName

func GoFuncName(pyName string) string

GoFuncName converts snake_case to PascalCase. Also strips module prefixes (dots) and handles PascalCase input.

func GoParamName

func GoParamName(pyName string) string

GoParamName converts snake_case to camelCase, escaping Go reserved words.

func ScaffoldInit

func ScaffoldInit(modules []string, pythonConstraint string) error

ScaffoldInit generates pyproject.toml and pyffi-gen.yaml.

func ScaffoldSync

func ScaffoldSync() error

ScaffoldSync runs uv sync in the current directory.

Types

type ClassInfo

type ClassInfo struct {
	PythonName string
	GoName     string
	Init       *FuncInfo // __init__ → NewXxx; nil if absent
	Methods    []FuncInfo
	Properties []PropInfo
	Doc        string
}

ClassInfo describes a Python class.

type Config

type Config struct {
	Module        string            `yaml:"module"`
	Package       string            `yaml:"package"`
	Out           string            `yaml:"out"`
	Include       []string          `yaml:"include"`
	Exclude       []string          `yaml:"exclude"`
	TypeOverrides map[string]string `yaml:"type_overrides"`
	DocStyle      string            `yaml:"doc_style"`
	Test          bool              `yaml:"test"`
	Python        string            `yaml:"python"`
	Dependencies  []string          `yaml:"dependencies"`
}

Config represents the pyffi-gen.yaml configuration.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads a YAML config file.

func (*Config) Defaults

func (c *Config) Defaults()

Defaults fills in unset fields with sensible defaults.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks for required fields.

type EnumInfo

type EnumInfo struct {
	PythonName string
	GoName     string
	Members    []EnumMember
}

EnumInfo describes a Python Enum class.

type EnumMember

type EnumMember struct {
	PythonName string
	GoName     string
	Value      string // Go literal
}

EnumMember is a single enum value.

type FuncInfo

type FuncInfo struct {
	PythonName string
	GoName     string // PascalCase
	Params     []ParamInfo
	Returns    TypeInfo
	Doc        string
}

FuncInfo describes a Python function or method.

type Generator

type Generator struct {
	Config *Config
	Mapper *Mapper
}

Generator orchestrates code generation.

func NewGenerator

func NewGenerator(cfg *Config) *Generator

NewGenerator creates a generator from config.

func (*Generator) DryRun

func (g *Generator) DryRun() error

DryRun prints what would be generated to stdout.

func (*Generator) Generate

func (g *Generator) Generate() error

Generate runs the full pipeline: introspect → map → template → write.

type Introspector

type Introspector struct {
	// contains filtered or unexported fields
}

Introspector runs Python to extract module metadata.

func NewIntrospector

func NewIntrospector(opts ...pyffi.Option) (*Introspector, error)

NewIntrospector creates a Python runtime and loads helper functions.

func (*Introspector) Close

func (i *Introspector) Close() error

Close releases the Python runtime.

func (*Introspector) Introspect

func (i *Introspector) Introspect(moduleName string, include, exclude []string, mapper *Mapper) (*ModuleInfo, error)

Introspect extracts metadata from the named Python module.

type Mapper

type Mapper struct {
	Overrides map[string]string
}

Mapper converts Python type annotations to Go types.

func NewMapper

func NewMapper(overrides map[string]string) *Mapper

NewMapper creates a mapper with optional type overrides.

func (*Mapper) Map

func (m *Mapper) Map(pyAnnotation string) TypeInfo

Map converts a Python annotation string to a TypeInfo.

type ModuleInfo

type ModuleInfo struct {
	PythonModule string
	GoPackage    string
	Functions    []FuncInfo
	Classes      []ClassInfo
	Enums        []EnumInfo
	Doc          string
	ClassNames   map[string]bool // set of Go class names generated in this package
}

ModuleInfo holds the complete introspection result for a Python module.

type ParamInfo

type ParamInfo struct {
	PythonName string
	GoName     string // camelCase
	Type       TypeInfo
	HasDefault bool
	IsKwOnly   bool
}

ParamInfo describes a function parameter.

type PropInfo

type PropInfo struct {
	PythonName string
	GoName     string
	Type       TypeInfo
}

PropInfo describes a Python property.

type TypeInfo

type TypeInfo struct {
	Python string // original annotation, e.g. "list[int]"
	Go     string // mapped Go type, e.g. "[]int64"
	IsPtr  bool   // Optional → pointer
	IsAny  bool   // unresolvable → any
}

TypeInfo describes a mapped type.

Jump to

Keyboard shortcuts

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