internal

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compiler

type Compiler interface {
	CompileBody(m *Method) (body string, needsStrconv bool, err error)
	// Dialect returns the SQL dialect this Compiler was constructed for, so the generator
	// can pick a dialect-specific method template (e.g. Postgres INSERT needs
	// "RETURNING id" + QueryRowContext instead of ExecContext + LastInsertId).
	Dialect() Dialect
}

Compiler compiles SQL template strings into Go source fragments.

func NewCompiler

func NewCompiler(dialect Dialect) Compiler

NewCompiler returns a Compiler for the given dialect.

type Config

type Config struct {
	Dialect      Dialect                     `yaml:"dialect"`
	Repositories map[string]*RepositoryEntry `yaml:"repositories"`
}

func Load

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

type ConfigPath

type ConfigPath string

ConfigPath is the path to the simplesql config file, provided for DI.

type Dialect

type Dialect string
const (
	DialectMySQL    Dialect = "mysql"
	DialectPostgres Dialect = "postgres"
	DialectSQLite   Dialect = "sqlite"
)

type Generator

type Generator interface {
	Generate(packageName string, requests []*Request) ([]byte, error)
}

Generator generates Go source from parsed interface definitions.

func NewGenerator

func NewGenerator(log *zap.Logger, compiler Compiler) Generator

NewGenerator returns a Generator backed by the given compiler.

type Import

type Import struct {
	Path  string
	Alias string
}

Import is a Go import referenced by a parsed interface's param/return types: a path with an optional local name — "" for a normal (unaliased) import, "_" for blank, "." for dot, or any other identifier for an explicit alias.

type Interface

type Interface struct {
	Name        string
	PackageName string
	Imports     []Import
	Methods     []Method
}

Interface holds the parsed interface.

type Method

type Method struct {
	Name        string
	SQL         string // raw SQL template text extracted from comments
	QueryKind   QueryKind
	Params      []Param
	ReturnKind  ReturnKind
	ReturnType  string // element type for single/slice, e.g. "User" (the "*"/"[]" markers are stripped)
	ReturnIsPtr bool   // for ReturnSlice: true for []*T, false for []T. Always true for ReturnSingle (*T is the only supported shape).
}

Method holds everything the generator needs about one interface method.

type Param

type Param struct {
	Name  string
	Type  string // Go type expression, e.g. "*bool", "string", "[]*User"
	IsPtr bool
}

Param is a method parameter.

type Parser

type Parser interface {
	ParseInterface(dir, ifaceName string) (*Interface, error)
}

Parser parses Go source files to extract interface definitions.

func NewParser

func NewParser(log *zap.Logger, reader Reader) Parser

NewParser returns a new Parser.

type QueryKind

type QueryKind string

QueryKind is the SQL statement type detected from the first keyword.

const (
	QuerySelect QueryKind = "SELECT"
	QueryInsert QueryKind = "INSERT"
	QueryUpdate QueryKind = "UPDATE"
	QueryDelete QueryKind = "DELETE"
)

type Reader

type Reader interface {
	ReadDir(dir string) (*token.FileSet, []*ast.File, error)
}

Reader reads Go source files from a directory.

func NewReader

func NewReader(log *zap.Logger) Reader

NewReader returns a new Reader.

type RepositoryEntry

type RepositoryEntry struct {
	Output     string `yaml:"output"`
	StructName string `yaml:"struct_name"`
}

func (*RepositoryEntry) UnmarshalYAML

func (r *RepositoryEntry) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML supports shorthand ("output.go") and full struct form.

type Request

type Request struct {
	Interface  *Interface
	StructName string
}

Request describes one interface to generate.

type ReturnKind

type ReturnKind string

ReturnKind describes what the method returns.

const (
	ReturnNothing  ReturnKind = "nothing"    // ()
	ReturnError    ReturnKind = "error"      // (error)
	ReturnSingle   ReturnKind = "single"     // (*T, error)
	ReturnSlice    ReturnKind = "slice"      // ([]*T, error) or ([]T, error)
	ReturnRowsOrID ReturnKind = "rows_or_id" // (int, error)
)

type Runner

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

Runner orchestrates parsing and code generation for all repositories in a config.

func NewRunner

func NewRunner(p Parser, gen Generator, cfg *Config, cfgPath ConfigPath, log *zap.Logger) *Runner

NewRunner returns a new Runner.

func (*Runner) Run

func (r *Runner) Run() error

Run processes all repositories from the config, writing generated files.

type Tokenizer

type Tokenizer struct{}

Tokenizer splits a SQL template string into a sequence of tokens.

func NewTokenizer

func NewTokenizer() *Tokenizer

NewTokenizer returns a Tokenizer.

Jump to

Keyboard shortcuts

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