blueprint

package
v0.37.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package blueprint provides structured types for scaffold configuration that support additive merges from multiple extensions without conflicts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackgroundWorker

type BackgroundWorker struct {
	// Name is a descriptive name for the worker
	Name string
	// FunctionCall is the function to call (e.g., "worker.StartQueue(ctx, queue)")
	FunctionCall string
	// DependsOn lists variables this worker needs
	DependsOn []string
	// Order for deterministic rendering
	Order int
}

BackgroundWorker represents a goroutine to start in main.go

type Blueprint

type Blueprint struct {
	// Tools lists go tool dependencies for the go.mod tool directive
	Tools *OrderedSet

	// Controllers section
	Controllers ControllerSection

	// Routes section
	Routes RouteSection

	// Models section
	Models ModelSection

	// Configuration section
	Config ConfigSection

	// Migrations and database
	Migrations MigrationSection

	// Main holds configuration for the main.go application setup
	Main MainSection

	// Cookies section for router/cookies package
	Cookies CookiesSection
}

Blueprint holds all structured configuration for a scaffold project. Each section supports additive operations that maintain uniqueness and ordering.

func New

func New() *Blueprint

New creates a new Blueprint with initialized sections.

type Builder

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

Builder provides a typed API for adding elements to a blueprint while enforcing uniqueness and maintaining deterministic ordering.

func NewBuilder

func NewBuilder(bp *Blueprint) *Builder

NewBuilder creates a builder wrapping the provided blueprint.

func (*Builder) AddBackgroundWorker

func (b *Builder) AddBackgroundWorker(name, functionCall string, dependsOn ...string) *Builder

AddBackgroundWorker adds a background worker goroutine to main.go.

func (*Builder) AddConfigField

func (b *Builder) AddConfigField(name, typeName string) *Builder

AddConfigField adds a field to the config struct.

func (*Builder) AddControllerConstructor

func (b *Builder) AddControllerConstructor(varName, expression string) *Builder

AddControllerConstructor adds a constructor initialization statement. The fieldName is automatically derived by finding a matching controller field. If no match is found, it capitalizes the first letter of varName. TODO: naming

func (*Builder) AddControllerDependency

func (b *Builder) AddControllerDependency(name, typeName string) *Builder

AddControllerDependency adds a dependency parameter to the controller constructor. The order is automatically assigned based on insertion sequence. Use AddControllerDependencyWithInit to provide an initialization expression.

func (*Builder) AddControllerDependencyWithInit

func (b *Builder) AddControllerDependencyWithInit(name, typeName, initExpr string) *Builder

AddControllerDependencyWithInit adds a dependency with an optional initialization expression. If initExpr is provided, it will be used to initialize the dependency in main.go. If initExpr is empty, the dependency is assumed to be provided externally (like db).

func (*Builder) AddControllerField

func (b *Builder) AddControllerField(name, typeName string) *Builder

AddControllerField adds a field to the Controllers struct.

func (*Builder) AddControllerImport

func (b *Builder) AddControllerImport(importPath string) *Builder

AddControllerImport adds an import path to the controllers section.

func (*Builder) AddCookiesAppField added in v0.36.0

func (b *Builder) AddCookiesAppField(name, typeName string) *Builder

AddCookiesAppField adds a field to the App struct in cookies.

func (*Builder) AddCookiesConstant added in v0.36.0

func (b *Builder) AddCookiesConstant(name, value string) *Builder

AddCookiesConstant adds a constant to the cookies section.

func (*Builder) AddCookiesFunction added in v0.36.0

func (b *Builder) AddCookiesFunction(name, code string) *Builder

AddCookiesFunction adds a function to the cookies section.

func (*Builder) AddCookiesImport added in v0.36.0

func (b *Builder) AddCookiesImport(importPath string) *Builder

AddCookiesImport adds an import path to the cookies section.

func (*Builder) AddEnvVar

func (b *Builder) AddEnvVar(key, configField, defaultValue string) *Builder

AddEnvVar adds an environment variable mapping.

func (*Builder) AddMainImport

func (b *Builder) AddMainImport(importPath string) *Builder

AddMainImport adds an import path to the main.go file.

func (*Builder) AddMainInitialization

func (b *Builder) AddMainInitialization(varName, expression string, dependsOn ...string) *Builder

AddMainInitialization adds an initialization code block to main.go. The varName is the variable name, expression is the initialization code. DependsOn can be used to specify ordering dependencies.

func (*Builder) AddMigration

func (b *Builder) AddMigration(migration Migration) *Builder

AddMigration adds a migration definition.

func (*Builder) AddModel

func (b *Builder) AddModel(model Model) *Builder

AddModel adds a model definition.

func (*Builder) AddModelImport

func (b *Builder) AddModelImport(importPath string) *Builder

AddModelImport adds an import to the models section.

func (*Builder) AddPreRunHook

func (b *Builder) AddPreRunHook(name, code string) *Builder

AddPreRunHook adds a pre-run hook to execute before the server starts.

func (*Builder) AddRoute

func (b *Builder) AddRoute(route Route) *Builder

AddRoute adds a route definition.

func (*Builder) AddRouteCollection

func (b *Builder) AddRouteCollection(routes ...string) *Builder

AddRouteCollection records route expressions to include in the BuildRoutes literal.

func (*Builder) AddRouteGroup

func (b *Builder) AddRouteGroup(groupName string) *Builder

AddRouteGroup adds a route group name (e.g., "auth" for authRoutes). These are used by the router aggregator template to combine all route groups.

func (*Builder) AddRouteImport

func (b *Builder) AddRouteImport(importPath string) *Builder

AddRouteImport adds an import to the routes section.

func (*Builder) AddRouteRegistration

func (b *Builder) AddRouteRegistration(method, routeVariable, controllerRef string, middleware ...string) *Builder

AddRouteRegistration adds a route registration entry for the registrar function. The method is the HTTP method constant (e.g., "http.MethodGet"), routeVariable is the route variable (e.g., "routes.Health"), controllerRef is the controller method reference (e.g., "ctrls.API.Health"), and middleware is optional middleware to apply. If a registration function is currently being built (via StartRouteRegistrationFunction), the registration is added to that function. Otherwise, it's added to the main registrations.

func (*Builder) AddTool

func (b *Builder) AddTool(tool string) *Builder

AddTool registers a go tool binary to include in the go.mod tool directive.

func (*Builder) Blueprint

func (b *Builder) Blueprint() *Blueprint

Blueprint returns the underlying blueprint.

func (*Builder) EndRouteRegistrationFunction

func (b *Builder) EndRouteRegistrationFunction() *Builder

EndRouteRegistrationFunction completes the current registration function and adds it to the blueprint.

func (*Builder) Merge

func (b *Builder) Merge(other *Blueprint) error

Merge combines another blueprint into this one, maintaining uniqueness and order. Items from the other blueprint are added after existing items.

func (*Builder) SetCookiesCreateSessionCode added in v0.36.0

func (b *Builder) SetCookiesCreateSessionCode(code string) *Builder

SetCookiesCreateSessionCode sets the code to execute when creating a session.

func (*Builder) SetCookiesGetSessionCode added in v0.36.0

func (b *Builder) SetCookiesGetSessionCode(code string) *Builder

SetCookiesGetSessionCode sets the code to execute when getting session data.

func (*Builder) StartRouteRegistrationFunction

func (b *Builder) StartRouteRegistrationFunction(functionName string) *Builder

StartRouteRegistrationFunction begins building a registration function. All subsequent AddRouteRegistration calls will be added to this function until EndRouteRegistrationFunction is called.

type ConfigSection

type ConfigSection struct {
	// Config struct fields
	Fields []Field

	// Environment variable mappings
	EnvVars []EnvVar
}

ConfigSection holds application configuration.

func (*ConfigSection) SortedEnvVars

func (cs *ConfigSection) SortedEnvVars() []EnvVar

SortedEnvVars returns environment variables sorted by order.

func (*ConfigSection) SortedFields

func (cs *ConfigSection) SortedFields() []Field

SortedFields returns config fields sorted by order.

type Constant added in v0.36.0

type Constant struct {
	Name  string
	Value string
	Order int
}

Constant represents a const declaration

type Constructor

type Constructor struct {
	// VarName is the variable name on the left side (e.g., "pages")
	VarName string
	// FieldName is the struct field this variable should be assigned to (e.g., "Pages")
	FieldName string
	// Expression is the right-hand side (e.g., "newPages(db, cache)")
	Expression string
	// Order for deterministic rendering
	Order int
}

Constructor represents an initialization statement in a constructor.

type ControllerSection

type ControllerSection struct {
	// Import paths needed by controllers
	Imports *OrderedSet

	// Controller dependencies (parameters for New function)
	Dependencies []Dependency

	// Controller fields to add to the Controllers struct
	Fields []Field

	// Constructor initializations (e.g., "pages := newPages(db, cache)")
	Constructors []Constructor
}

ControllerSection holds controller-related configuration.

func (*ControllerSection) SortedConstructors

func (cs *ControllerSection) SortedConstructors() []Constructor

SortedConstructors returns constructors sorted by order.

func (*ControllerSection) SortedDependencies

func (cs *ControllerSection) SortedDependencies() []Dependency

SortedDependencies returns controller dependencies sorted by order.

func (*ControllerSection) SortedFields

func (cs *ControllerSection) SortedFields() []Field

SortedFields returns controller fields sorted by order.

type CookiesSection added in v0.36.0

type CookiesSection struct {
	Imports           *OrderedSet
	Constants         []Constant
	AppFields         []Field
	Functions         []Function
	CreateSessionCode string
	GetSessionCode    string
}

CookiesSection holds cookies package configuration

func (*CookiesSection) SortedAppFields added in v0.36.0

func (cs *CookiesSection) SortedAppFields() []Field

SortedAppFields returns cookies app fields sorted by order.

func (*CookiesSection) SortedConstants added in v0.36.0

func (cs *CookiesSection) SortedConstants() []Constant

SortedConstants returns cookies constants sorted by order.

func (*CookiesSection) SortedFunctions added in v0.36.0

func (cs *CookiesSection) SortedFunctions() []Function

SortedFunctions returns cookies functions sorted by order.

type Dependency

type Dependency struct {
	Name string
	Type string
	// InitExpr is the optional initialization expression (e.g., "queue.NewInMemoryQueue()")
	// If empty, the dependency is assumed to be provided externally (like db)
	InitExpr string
	// ImportPath is the import path needed when InitExpr is provided
	// (e.g., "myapp/queue" for "queue.NewInMemoryQueue()")
	ImportPath string
	// Order for deterministic rendering
	Order int
}

Dependency represents a constructor parameter.

type EnvVar

type EnvVar struct {
	Key          string
	ConfigField  string
	DefaultValue string
	// Order for deterministic rendering
	Order int
}

EnvVar represents an environment variable mapping.

type Field

type Field struct {
	Name string
	Type string
	// Order for deterministic rendering
	Order int
}

Field represents a struct field.

type Function added in v0.36.0

type Function struct {
	Name  string
	Code  string
	Order int
}

Function represents a function definition

type Initialization

type Initialization struct {
	// VarName is the variable name (e.g., "emailSender")
	VarName string
	// Expression is the initialization code (e.g., "email.NewMailpit()")
	Expression string
	// DependsOn lists variable names this depends on (for ordering)
	DependsOn []string
	// Order for deterministic rendering
	Order int
}

Initialization represents a service/dependency initialization in main.go

type MainSection

type MainSection struct {
	// Import paths needed in main.go (beyond controller dependencies)
	Imports *OrderedSet

	// Initialization code blocks (e.g., service creation)
	Initializations []Initialization

	// Background workers to start
	BackgroundWorkers []BackgroundWorker

	// Pre-run hooks executed before server starts
	PreRunHooks []PreRunHook
}

MainSection holds application startup configuration.

func (*MainSection) SortedBackgroundWorkers

func (ms *MainSection) SortedBackgroundWorkers() []BackgroundWorker

SortedBackgroundWorkers returns background workers sorted by order.

func (*MainSection) SortedInitializations

func (ms *MainSection) SortedInitializations() []Initialization

SortedInitializations returns initializations sorted by order.

func (*MainSection) SortedPreRunHooks

func (ms *MainSection) SortedPreRunHooks() []PreRunHook

SortedPreRunHooks returns pre-run hooks sorted by order.

type Migration

type Migration struct {
	Name      string
	Timestamp string
	Path      string
	// Order for deterministic rendering (usually by timestamp)
	Order int
}

Migration represents a database migration.

type MigrationSection

type MigrationSection struct {
	// Migration file paths
	Migrations []Migration
}

MigrationSection holds database migration information.

func (*MigrationSection) SortedMigrations

func (ms *MigrationSection) SortedMigrations() []Migration

SortedMigrations returns migrations sorted by order.

type Model

type Model struct {
	Name   string
	Fields []Field
	// Order for deterministic rendering
	Order int
}

Model represents a model struct definition.

type ModelSection

type ModelSection struct {
	// Model imports
	Imports *OrderedSet

	// Model struct definitions
	Models []Model
}

ModelSection holds model configuration.

func (*ModelSection) SortedModels

func (ms *ModelSection) SortedModels() []Model

SortedModels returns models sorted by order.

type OrderedSet

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

OrderedSet maintains a collection of unique strings in insertion order with optional canonical sorting.

func NewOrderedSet

func NewOrderedSet() *OrderedSet

NewOrderedSet creates an empty ordered set.

func (*OrderedSet) Add

func (os *OrderedSet) Add(item string) bool

Add inserts an item into the set if not already present. Returns true if the item was added, false if it already existed.

func (*OrderedSet) Contains

func (os *OrderedSet) Contains(item string) bool

Contains reports whether the item is in the set.

func (*OrderedSet) Items

func (os *OrderedSet) Items() []string

Items returns a copy of the current items in insertion order.

func (*OrderedSet) Len

func (os *OrderedSet) Len() int

Len returns the number of unique items in the set.

func (*OrderedSet) Merge

func (os *OrderedSet) Merge(other *OrderedSet)

Merge adds all items from another ordered set while maintaining uniqueness.

func (*OrderedSet) SortedItems

func (os *OrderedSet) SortedItems() []string

SortedItems returns a sorted copy of the items.

type PreRunHook

type PreRunHook struct {
	// Name is a descriptive name for the hook
	Name string
	// Code is the Go code to execute (e.g., "if err := migrate(db); err != nil { return err }")
	Code string
	// Order for deterministic rendering
	Order int
}

PreRunHook represents setup code to run before starting the server

type RegistrationFunction

type RegistrationFunction struct {
	// FunctionName is the name of the registration function (e.g., "registerAuthRoutes")
	FunctionName string
	// Registrations contains all route registrations for this function
	Registrations []RouteRegistration
	// Order for deterministic rendering
	Order int
}

RegistrationFunction represents a named registration function that groups route registrations.

type Route

type Route struct {
	Name             string
	Path             string
	Controller       string
	ControllerMethod string
	Method           string
	IncludeInSitemap bool
	// Order for deterministic rendering
	Order int
}

Route represents a route definition.

type RouteCollection

type RouteCollection struct {
	Routes []string
	// Order for deterministic rendering
	Order int
}

RouteCollection represents grouped route variables for BuildRoutes.

type RouteRegistration

type RouteRegistration struct {
	// Method is the HTTP method (e.g., "http.MethodGet")
	Method string
	// RouteVariable is the route variable name (e.g., "routes.Health")
	RouteVariable string
	// ControllerRef is the controller method reference (e.g., "ctrls.API.Health")
	ControllerRef string
	// Middleware is optional middleware to apply to this route
	Middleware []string
	// Order for deterministic rendering
	Order int
}

RouteRegistration represents a route registration entry for the registrar function.

type RouteSection

type RouteSection struct {
	// Route definitions
	Routes []Route

	// Route groups (e.g., "auth" for authRoutes, "admin" for adminRoutes)
	// Used to aggregate routes in the router_routes_routes.tmpl aggregator
	RouteGroups *OrderedSet

	// RouteCollections holds grouped route expressions to include in BuildRoutes.
	RouteCollections []RouteCollection

	// Route group imports (for middleware, etc.)
	Imports *OrderedSet

	// RouteRegistrations holds route registration entries for the registrar function
	Registrations []RouteRegistration

	// RegistrationFunctions holds grouped registration functions
	RegistrationFunctions []RegistrationFunction
}

RouteSection holds routing configuration.

func (*RouteSection) SortedRegistrationFunctions

func (rs *RouteSection) SortedRegistrationFunctions() []RegistrationFunction

SortedRegistrationFunctions returns registration functions sorted by order.

func (*RouteSection) SortedRegistrations

func (rs *RouteSection) SortedRegistrations() []RouteRegistration

SortedRegistrations returns route registrations sorted by order.

func (*RouteSection) SortedRouteCollections

func (rs *RouteSection) SortedRouteCollections() []RouteCollection

SortedRouteCollections returns grouped route expressions sorted by order.

func (*RouteSection) SortedRoutes

func (rs *RouteSection) SortedRoutes() []Route

SortedRoutes returns routes sorted by order.

Jump to

Keyboard shortcuts

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