codegen

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultHeader = "// Code generated by mmapforge. DO NOT EDIT."

DefaultHeader is used when Config.Header is empty.

Variables

View Source
var GraphTemplates = []GraphTemplate{}

GraphTemplates is the list of graph-wide templates to execute.

View Source
var TypeTemplates = []TypeTemplate{
	{
		Name: "store",
		Format: func(t *Type) string {
			return strings.ToLower(t.Name) + "_store.go"
		},
	},
	{
		Name: "store_test",
		Format: func(t *Type) string {
			return strings.ToLower(t.Name) + "_store_test.go"
		},
	},
}

TypeTemplates is the list of per-type templates to execute.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Target is the output directory for generated files.
	Target string

	// Package is the Go package for generated code.
	Package string

	// Header is an optional file header. Defaults to the DefaultHeader constant.
	Header string

	// Templates specifics optional external templates to execute
	// or override the defaults.
	Templates []*Template

	// Hooks holds an optional list of Hooks to apply on the graph
	// before/after code generation.
	Hooks []Hook
}

Config holds the global codegen configuration, shared between all generated nodes.

type Field

type Field struct {
	mmapforge.FieldLayout
}

Field wraps mmapforge.FieldLayout and adds template helper methods.

func (*Field) GetterName

func (f *Field) GetterName() string

GetterName returns the name for the getter method

func (*Field) GoType

func (f *Field) GoType() string

GoType returns the Go type string for this field.

func (*Field) IsBool

func (f *Field) IsBool() bool

IsBool reports if the field is a bool.

func (*Field) IsBytes

func (f *Field) IsBytes() bool

IsBytes reports if the field is a []byte.

func (*Field) IsNumeric

func (f *Field) IsNumeric() bool

IsNumeric reports if the field is a numeric type.

func (*Field) IsString

func (f *Field) IsString() bool

IsString reports if the field is a string.

func (*Field) IsVarLen

func (f *Field) IsVarLen() bool

IsVarLen reports if the field is variable-length.

func (*Field) ReadCall

func (f *Field) ReadCall() string

ReadCall returns the Store.Read* method call expression for this field.

func (*Field) SetterName

func (f *Field) SetterName() string

SetterName returns the name for the setter method

func (*Field) TestValue

func (f *Field) TestValue() string

TestValue returns a Go literal for a representative test value.

func (*Field) TypeConstant

func (f *Field) TypeConstant() int

TypeConstant returns the fmmap.FieldType integer for template use.

func (*Field) WriteCall

func (f *Field) WriteCall() string

WriteCall returns the Store.Write* method call using "val" as the value arg.

func (*Field) WriteCallRec

func (f *Field) WriteCallRec() string

WriteCallRec returns the Store.Write* method call using "rec.<GoName>" as the value.

type GenerateFunc

type GenerateFunc func(*Graph) error

GenerateFunc adapts an ordinary function to the Generator interface.

func (GenerateFunc) Generate

func (f GenerateFunc) Generate(g *Graph) error

Generate calls f(g).

type Generator

type Generator interface {
	Generate(*Graph) error
}

Generator is the interface for codegen from a Graph.

type Graph

type Graph struct {
	*Config

	Nodes []*Type
}

Graph holds all Type nodes and derive code generation.

func NewGraph

func NewGraph(c *Config, schemas []StructSchema) (*Graph, error)

NewGraph builds a Graph from parsed schemas and config. It computes layouts, builds rich Type/Field objects, and validates.

func (*Graph) Gen

func (g *Graph) Gen() error

Gen generates all artifacts. Hooks wrap the core generation

type GraphTemplate

type GraphTemplate struct {
	Name   string            // matches a {{ define "name" }} in a .tmpl file
	Skip   func(*Graph) bool // optional: skip if returns true
	Format string            // output file name
}

GraphTemplate is executed once for the whole Graph.

type Hook

type Hook func(Generator) Generator

Hook is "generate middleware" - wraps a Generator to inject logic

type StructSchema

type StructSchema struct {
	// Name in the Go struct name
	Name string

	// Package is the Go package name
	Package string

	// Fields are the parsed field definitions.
	Fields []mmapforge.FieldDef

	// SchemaVersion is from the version=N directive.
	SchemaVersion uint32
}

StructSchema is the parsed output of a Go struct annotated with // mmapforge:schema version=N. It carries everything the emitter

func ParseFile

func ParseFile(path string) ([]StructSchema, error)

ParseFile parses a Go source file and extracts all structs annotated with // mmapforge:schema version=N.

type Template

type Template struct {
	*template.Template
	FuncMap template.FuncMap
	// contains filtered or unexported fields
}

func MustParse

func MustParse(t *Template, err error) *Template

func NewTemplate

func NewTemplate(name string) *Template

func (*Template) AddParseTree

func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error)

func (*Template) Funcs

func (t *Template) Funcs(funcMap template.FuncMap) *Template

func (*Template) Parse

func (t *Template) Parse(text string) (*Template, error)

func (*Template) ParseFS

func (t *Template) ParseFS(fsys fs.FS, patterns ...string) (*Template, error)

func (*Template) ParseFiles

func (t *Template) ParseFiles(filenames ...string) (*Template, error)

func (*Template) SkipIf

func (t *Template) SkipIf(cond func(*Graph) bool) *Template

type Type

type Type struct {
	*Config

	// Name is the Go struct name from the schema
	Name string

	// Package is the Go package name for the generated file.
	Package string

	// Fields holds the computed field layouts for this type.
	Fields []*Field

	// SchemaVersion is the schema migration version.
	SchemaVersion uint32

	// RecordSize is the total byte size of one record.
	RecordSize uint32
}

Type represents a single mmapforge schema node — the central object

func (*Type) HasBytesField

func (t *Type) HasBytesField() bool

HasBytesField reports if any field is a bytes field.

func (*Type) HasStringField

func (t *Type) HasStringField() bool

HasStringField reports if any field is a string.

func (*Type) HasVarLenField

func (t *Type) HasVarLenField() bool

HasVarLenField reports if any field is variable-length (string or bytes).

func (*Type) Header

func (t *Type) Header() string

Header returns the file header for generated code.

func (*Type) Label

func (t *Type) Label() string

Label returns the snake_case name of the type

func (*Type) LayoutFuncName

func (t *Type) LayoutFuncName() string

LayoutFuncName returns the name of the Layout() func

func (*Type) NewStoreFuncName

func (t *Type) NewStoreFuncName() string

NewStoreFuncName returns the name for CreateStore

func (*Type) OpenStoreFuncName

func (t *Type) OpenStoreFuncName() string

OpenStoreFuncName returns the name for OpenStore

func (*Type) Receiver

func (t *Type) Receiver() string

Receiver returns a short receiver variable name for store methods.

func (*Type) RecordName

func (t *Type) RecordName() string

RecordName returns the generated record struct name

func (*Type) StoreName

func (t *Type) StoreName() string

StoreName returns the generated store struct name

type TypeTemplate

type TypeTemplate struct {
	Cond   func(*Type) bool
	Format func(*Type) string
	Name   string
}

TypeTemplate is executed once per Type node.

Jump to

Keyboard shortcuts

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