gen

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ModelTemplates, _       = fs.Sub(templates, "templates/models")
	FactoryTemplates, _     = fs.Sub(templates, "templates/factory")
	MySQLModelTemplates, _  = fs.Sub(mysqlTemplates, "bobgen-mysql/templates/models")
	SQLiteModelTemplates, _ = fs.Sub(sqliteTemplates, "bobgen-sqlite/templates/models")
	PrismaModelTemplates, _ = fs.Sub(prismaTemplates, "bobgen-prisma/templates/models")
)

Functions

func Run added in v0.17.2

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

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

Types

type Aliases

type Aliases map[string]TableAlias

Aliases defines aliases for the generation run

func (Aliases) Table

func (a Aliases) Table(table string) TableAlias

Table gets a table alias, panics if not found.

type Config

type Config struct {
	// Struct tags to generate
	Tags []string `yaml:"tags"`
	// Disable generating factory for models.
	NoFactory bool `yaml:"no_factory"`
	// Disable generated go test files
	NoTests bool `yaml:"no_tests"`
	// Disable back referencing in the loaded relationship structs
	NoBackReferencing bool `yaml:"no_back_referencing"`
	// Delete the output folder (rm -rf) before generation to ensure sanity
	Wipe bool `yaml:"wipe"`
	// 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         drivers.Types `yaml:"types"`         // register custom types
	Aliases       Aliases       `yaml:"aliases"`       // customize aliases
	Constraints   Constraints   `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 map[string]drivers.Constraints

type DBInfoPlugin added in v0.20.0

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

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

type Importer

type Importer map[string]struct{}

func (Importer) Import

func (i Importer) Import(pkgs ...string) string

To be used inside templates to record an import. Always returns an empty string

func (Importer) ImportList

func (i Importer) ImportList(list importers.List) string

func (Importer) ToList

func (i Importer) ToList() importers.List

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 {
	// 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
	// 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(tables []drivers.Table, r orm.Relationship) orm.Relationship

GetInverse returns the Relationship of the other side

type Replace

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

Replace replaces a column type with something else

type State

type State struct {
	Config              Config
	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 interface {
	Plugin
	PlugState(*State) error
}

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

type TableAlias

type TableAlias struct {
	UpPlural     string `yaml:"up_plural,omitempty" toml:"up_plural,omitempty" json:"up_plural,omitempty"`
	UpSingular   string `yaml:"up_singular,omitempty" toml:"up_singular,omitempty" json:"up_singular,omitempty"`
	DownPlural   string `yaml:"down_plural,omitempty" toml:"down_plural,omitempty" json:"down_plural,omitempty"`
	DownSingular string `yaml:"down_singular,omitempty" toml:"down_singular,omitempty" json:"down_singular,omitempty"`

	Columns       map[string]string `yaml:"columns,omitempty" toml:"columns,omitempty" json:"columns,omitempty"`
	Relationships map[string]string `yaml:"relationships,omitempty" toml:"relationships,omitempty" json:"relationships,omitempty"`
}

TableAlias defines the spellings for a table name in Go

func (TableAlias) Column

func (t TableAlias) Column(column string) string

Column get's a column's aliased name, panics if not found.

func (TableAlias) Relationship

func (t TableAlias) Relationship(fkey string) string

Relationship looks up a relationship, panics if not found.

type TemplateData added in v0.20.0

type TemplateData[T any] struct {
	Dialect  string
	Importer Importer

	Table         drivers.Table
	Tables        []drivers.Table
	Enums         []drivers.Enum
	Aliases       Aliases
	Types         drivers.Types
	Relationships Relationships

	// Controls what names are output
	PkgName string

	// Control various generation features
	AddSoftDeletes    bool
	AddEnumTypes      bool
	EnumNullPrefix    string
	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
	ModelsPackage string
}

func (*TemplateData[T]) ResetImports added in v0.20.0

func (t *TemplateData[T]) ResetImports()

type TemplateDataPlugin added in v0.20.0

type TemplateDataPlugin[T any] interface {
	Plugin
	PlugTemplateData(*TemplateData[T]) 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

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
Package importers helps with dynamic imports for templating
Package importers helps with dynamic imports for templating

Jump to

Keyboard shortcuts

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