gen

package
v0.41.1 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 22 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BaseTemplates   = buildTemplatesFromKnownDirStructure(templates, "")
	MySQLTemplates  = buildTemplatesFromKnownDirStructure(mysqlTemplates, "bobgen-mysql")
	PSQLTemplates   = buildTemplatesFromKnownDirStructure(psqlTemplates, "bobgen-psql")
	SQLiteTemplates = buildTemplatesFromKnownDirStructure(sqliteTemplates, "bobgen-sqlite")
)

Functions

func NormalizeType added in v0.28.0

func NormalizeType(val string) string

func Run added in v0.17.2

func Run[T, C, I any](ctx context.Context, s *State[C], driver drivers.Interface[T, C, I], plugins ...Plugin) error

Run executes the templates and outputs them to files based on the state given.

Types

type ColumnFilter added in v0.39.0

type ColumnFilter struct {
	Name      *string `yaml:"name"`
	DBType    *string `yaml:"db_type"`
	Type      *string `yaml:"type"`
	Default   *string `yaml:"default"`
	Comment   *string `yaml:"comment"`
	Nullable  *bool   `yaml:"nullable"`
	Generated *bool   `yaml:"generated"`
	AutoIncr  *bool   `yaml:"autoincr"`

	// DomainName is the domain type name associated to the column. See here:
	// https://www.postgresql.org/docs/16/extend-type-system.html
	DomainName *string `yaml:"domain_name"`
}

ColumnFilter is used to filter columns in the config file. It should mirror the fields of drivers.Column

func (ColumnFilter) IsEmpty added in v0.39.0

func (f ColumnFilter) IsEmpty() bool

func (ColumnFilter) Matches added in v0.39.0

func (f ColumnFilter) Matches(column drivers.Column) bool

Matches determines if a drivers.Column matches all the specified criteria (logical AND).

String fields are matched case-insensitively and by regex.

type Config

type Config[ConstraintExtra any] struct {
	// System to use to create null and optional types
	// available options are:
	// - "github.com/aarondl/opt" (default)
	//    * Uses null.Val[T] for optional values
	//    * Uses null.Null[T] for nullable values
	// - "database/sql"
	//    * Uses pointers for optional values
	//	  * Uses sql.Null[T] for nullable values
	TypeSystem string `yaml:"type_system"`
	// Struct tags to generate
	Tags []string `yaml:"tags"`
	// Disable generating go test files
	NoTests bool `yaml:"no_tests"`
	// Disable back referencing in the loaded relationship structs
	NoBackReferencing bool `yaml:"no_back_referencing"`
	// Decides the casing for go structure tag names. camel, title or snake (default snake)
	StructTagCasing string `yaml:"struct_tag_casing"`
	// Relationship struct tag name
	RelationTag string `yaml:"relation_tag"`
	// List of column names that should have tags values set to '-' (ignored during parsing)
	TagIgnore []string `yaml:"tag_ignore"`

	Types         map[string]drivers.Type      `yaml:"types"`         // register custom types
	Aliases       drivers.Aliases              `yaml:"aliases"`       // customize aliases
	Constraints   Constraints[ConstraintExtra] `yaml:"constraints"`   // define additional constraints
	Relationships Relationships                `yaml:"relationships"` // define additional relationships

	Replacements []Replace   `yaml:"replacements"`
	Inflections  Inflections `yaml:"inflections"`

	// Customize the generator name in the top level comment of generated files
	// >>   Code generated by **GENERATOR NAME**. DO NOT EDIT.
	// defaults to "BobGen [driver] [version]"
	Generator string `yaml:"generator"`
}

Config for the running of the commands

type Constraints added in v0.23.0

type Constraints[C any] map[string]drivers.Constraints[C]

type DBInfoPlugin added in v0.20.0

type DBInfoPlugin[T, C, I any] interface {
	Plugin
	PlugDBInfo(*drivers.DBInfo[T, C, I]) error
}

DBInfoPlugin is called immediately after the database information is assembled from the driver

type Inflections

type Inflections struct {
	Plural        map[string]string `yaml:"plural"`
	PluralExact   map[string]string `yaml:"plural_exact"`
	Singular      map[string]string `yaml:"singular"`
	SingularExact map[string]string `yaml:"singular_exact"`
	Irregular     map[string]string `yaml:"irregular"`
}

type Output

type Output struct {
	// If true, new files are not generated, but existing files are deleted
	Disabled bool

	// The key has to be unique in a gen.State
	// it also makes it possible to target modifing a specific output
	Key string

	PkgName                 string
	OutFolder               string
	Templates               []fs.FS
	SeparatePackageForTests bool
	// contains filtered or unexported fields
}

type Plugin added in v0.20.0

type Plugin interface {
	Name() string
}

type Relationships added in v0.23.0

type Relationships map[string][]orm.Relationship

func (Relationships) Get added in v0.23.0

func (r Relationships) Get(table string) []orm.Relationship

func (Relationships) GetInverse added in v0.23.0

func (rs Relationships) GetInverse(r orm.Relationship) orm.Relationship

GetInverse returns the Relationship of the other side

func (Relationships) GlobalKey added in v0.35.0

func (rs Relationships) GlobalKey(r orm.Relationship) string

type Replace

type Replace struct {
	Tables  []string     `yaml:"tables"`
	Match   ColumnFilter `yaml:"match"`
	Replace string       `yaml:"replace"`
}

Replace replaces a column type with something else

type State

type State[ConstraintExtra any] struct {
	Config              Config[ConstraintExtra]
	Outputs             []*Output
	CustomTemplateFuncs template.FuncMap
}

State holds the global data needed by most pieces to run

type StatePlugin added in v0.20.0

type StatePlugin[ConstraintExtra any] interface {
	Plugin
	PlugState(*State[ConstraintExtra]) error
}

This is called at the very beginning if there are any changes to be made to the state

type TemplateData added in v0.20.0

type TemplateData[T, C, I any] struct {
	Dialect  string
	Importer language.Importer

	Table         drivers.Table[C, I]
	Tables        drivers.Tables[C, I]
	QueryFile     drivers.QueryFile
	QueryFolder   drivers.QueryFolder
	QueryFolders  []drivers.QueryFolder
	Enums         []drivers.Enum
	Aliases       drivers.Aliases
	Types         drivers.Types
	Relationships Relationships

	// Controls what names are output
	PkgName string

	// Control various generation features
	NoTests           bool
	NoBackReferencing bool

	// Tags control which tags are added to the struct
	Tags []string
	// RelationTag controls the value of the tags for the Relationship struct
	RelationTag string
	// Generate struct tags as camelCase or snake_case
	StructTagCasing string
	// Contains field names that should have tags values set to '-'
	TagIgnore map[string]struct{}

	// Supplied by the driver
	ExtraInfo T

	// Package information
	CurrentPackage string            // the current package being generated
	OutputPackages map[string]string // map of output keys to package paths

	// Driver is the module name of the underlying `database/sql` driver
	Driver   string
	Language language.Language
}

type TemplateDataPlugin added in v0.20.0

type TemplateDataPlugin[T, C, I any] interface {
	Plugin
	PlugTemplateData(*TemplateData[T, C, I]) error
}

TemplateDataPlugin is called right after assembling the template data, before generating them for each output. NOTE: The PkgName field is overwritten for each output, so mofifying it in a plugin will have no effect. Use a StatePlugin instead

type Templates added in v0.40.0

type Templates struct {
	Enums    fs.FS
	Models   fs.FS
	Factory  fs.FS
	Queries  fs.FS
	DBErrors fs.FS
	Where    fs.FS
	Loaders  fs.FS
	Joins    fs.FS
	DBInfo   fs.FS
}

Directories

Path Synopsis
Package drivers talks to various database backends and retrieves table, column, type, and foreign key information
Package drivers talks to various database backends and retrieves table, column, type, and foreign key information

Jump to

Keyboard shortcuts

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