ast

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package ast provides types and logic for building Go ASTs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectImports

func CollectImports(typeInfos []TypeInfo) map[string]string

CollectImports gathers all unique imports from type information

func ExportedIdentifier

func ExportedIdentifier(raw string) string

ExportedIdentifier converts raw input into a public Go identifier using the legacy (no-initialisms, no-rename) rules. See Namer for the configurable path.

func FileName

func FileName(raw string) string

FileName converts the raw name into a snake_case file name segment.

func UnexportedIdentifier

func UnexportedIdentifier(raw string) string

UnexportedIdentifier converts raw input into a private Go identifier using the legacy (no-initialisms) rules. See Namer for the configurable path.

func UniqueName

func UniqueName(base string, used map[string]int) (string, error)

UniqueName ensures returned identifier does not collide with previous values.

Types

type Builder

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

Builder constructs Go AST files for code generation outputs.

func New

func New(options Options) *Builder

New returns a builder configured with the provided options.

func (*Builder) Build

func (b *Builder) Build(ctx context.Context, catalog *model.Catalog, analyses []analyzer.Result) ([]File, error)

Build produces the Go AST files for the provided catalog and analyses.

type BulkInsertOptions added in v0.11.0

type BulkInsertOptions struct {
	// MultiValues is true when the dialect supports a runtime multi-row VALUES
	// statement (SQLite/MySQL). False gates :copyfrom with UnsupportedReason.
	MultiValues bool
	// Placeholder is the positional parameter placeholder used inside each VALUES
	// tuple (e.g. "?"). Only meaningful when MultiValues is true.
	Placeholder string
	// UnsupportedReason is the diagnostic emitted when MultiValues is false.
	UnsupportedReason string
}

BulkInsertOptions describes how the builder should emit :copyfrom bulk inserts for the active dialect. It mirrors engine.BulkInsertStrategy without importing the engine package into the AST builder.

type File

type File struct {
	Path string
	Node *goast.File
	Raw  []byte
}

File represents an AST file ready for rendering.

type Namer added in v0.12.0

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

Namer derives Go identifiers from SQL names (column, table, and parameter tokens). The zero value reproduces the legacy ExportedIdentifier / UnexportedIdentifier output byte-for-byte, so construction sites that do not opt in are unchanged.

func NewNamer added in v0.12.0

func NewNamer(initialisms bool, rename map[string]string) Namer

NewNamer returns a Namer with the given initialism and rename behavior.

func (Namer) Exported added in v0.12.0

func (n Namer) Exported(raw string) string

Exported converts a raw SQL name into a public Go identifier, honoring the rename map first and the initialism table second.

func (Namer) Unexported added in v0.12.0

func (n Namer) Unexported(raw string) string

Unexported converts a raw SQL name into a private Go identifier using the legacy camelCase derivation. Initialisms and rename deliberately do NOT apply: issue #4 scopes them to names that become EXPORTED identifiers, and keeping arg names legacy makes Exported(Unexported(x)) re-derivation stable — an initialism-cased intermediate like "apiURLID" cannot be re-segmented into API/URL/ID.

type Options

type Options struct {
	Package             string
	Database            config.Database
	EmitJSONTags        bool
	EmitEmptySlices     bool
	EmitPointersForNull bool
	// EmitDBTags adds a db:"<column_name>" struct tag to model/result fields,
	// alongside any json tag. Default false reproduces the pre-G7 output (G7).
	EmitDBTags bool
	// JSONTagsCaseStyle controls json struct-tag value casing: "snake", "camel",
	// "pascal", or "none" (omit json tags). Empty preserves today's exact tags (G7).
	JSONTagsCaseStyle string
	// EmitResultStructPointers makes :one return *Row and :many return []*Row.
	// Default false keeps value result returns (G7).
	EmitResultStructPointers bool
	// EmitParamsStructPointers passes the grouped <Method>Params argument as a
	// *<Method>Params. Default false keeps the value argument (G7).
	EmitParamsStructPointers bool
	// SuppressInterface, when true, skips generating the Querier interface file
	// (querier.gen.go). The default (false) emits the interface, matching the
	// pre-G7 always-emit behavior, so every existing call site is unchanged. The
	// flag is inverted (suppress rather than emit) precisely so the zero value
	// preserves emission (G7).
	SuppressInterface bool
	// Initialisms renders SQL-derived identifier segments with the golint
	// initialism table (user_id → UserID, url_suffix → URLSuffix). It applies
	// wherever a SQL name (column, table, parameter) becomes a Go identifier;
	// user-chosen query names pass through unchanged. Default false keeps the
	// legacy casing so existing output is byte-identical.
	Initialisms bool
	// Rename maps an exact SQL name (e.g. "ip_address") to the exported Go
	// identifier to use for it (e.g. "IPAddress"). It wins over Initialisms and
	// applies regardless of that flag. Lookup-only; never iterated.
	Rename          map[string]string
	Prepared        PreparedOptions
	TypeResolver    *TypeResolver
	ColumnOverrides []config.ColumnOverride
	// ModelsPackage, when non-empty, emits the table-model structs into a
	// separate sub-package/sub-directory of this name (relative to the output
	// directory). The main package files reference the split model types
	// qualified by this selector and import ModelsImport. Empty preserves the
	// single-package layout byte-for-byte.
	ModelsPackage string
	// ModelsImport is the full Go import path of the models sub-package.
	ModelsImport string
	// BulkInsert carries the engine-derived strategy for :copyfrom bulk inserts.
	// The decision originates in internal/engine (see engine.BulkInsertStrategy);
	// the builder is a pure consumer. The zero value gates :copyfrom with the
	// default unsupported reason.
	BulkInsert BulkInsertOptions
}

Options configures the AST builder.

type PreparedOptions

type PreparedOptions struct {
	Enabled     bool
	EmitMetrics bool
	ThreadSafe  bool
}

PreparedOptions captures prepared-query generation toggles.

type TypeInfo

type TypeInfo struct {
	GoType      string
	UsesSQLNull bool
	Import      string // For custom types
	Package     string // For custom types
	// JSON reports that the column/param is bound to GoType as a JSON document:
	// generated code scans via json.Unmarshal into a []byte holder and binds via
	// json.Marshal. Set only for custom types whose mapping has json=true.
	JSON bool
}

TypeInfo describes a resolved Go type.

type TypeResolver

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

TypeResolver handles mapping between SQL types and Go types.

func NewTypeResolver

func NewTypeResolver(transformer *transform.Transformer) *TypeResolver

NewTypeResolver creates a new TypeResolver with optional custom type support.

func NewTypeResolverFull

func NewTypeResolverFull(transformer *transform.Transformer, database config.Database, emitPointersForNull bool) *TypeResolver

NewTypeResolverFull creates a TypeResolver with all options.

func NewTypeResolverWithDatabase

func NewTypeResolverWithDatabase(transformer *transform.Transformer, database config.Database) *TypeResolver

NewTypeResolverWithDatabase creates a TypeResolver for a specific database.

func NewTypeResolverWithOptions

func NewTypeResolverWithOptions(transformer *transform.Transformer, emitPointersForNull bool) *TypeResolver

NewTypeResolverWithOptions creates a TypeResolver with all options configured.

func (*TypeResolver) GetRequiredImports

func (r *TypeResolver) GetRequiredImports() map[string]string

GetRequiredImports returns the imports needed for the resolved types.

func (*TypeResolver) GetSemanticMapper

func (r *TypeResolver) GetSemanticMapper() *types.SQLiteMapper

GetSemanticMapper returns the semantic type mapper for the current database.

func (*TypeResolver) IsJSONGoType added in v0.11.0

func (r *TypeResolver) IsJSONGoType(goType string) bool

IsJSONGoType reports whether the resolved Go type goType corresponds to a custom type whose mapping declared json=true. goType is matched in its resolved, package-qualified form (e.g. "myapp.Profile"), tolerating a leading pointer (a nullable JSON column resolves to "*myapp.Profile"). It lets the AST builder recognize a JSON-bound column/param from the already-resolved type the analyzer emitted, without threading a separate flag through the analyzer.

func (*TypeResolver) Map

func (r *TypeResolver) Map(sqlType string, nullable bool) types.SemanticType

Map implements the types.Mapper interface for semantic type mapping.

func (*TypeResolver) ResolveType

func (r *TypeResolver) ResolveType(typeOrSQLType string, nullable bool) TypeInfo

ResolveType determines the Go type for a given SQL type or existing Go type.

Jump to

Keyboard shortcuts

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