gen

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LoaderMysql    = "mysql"
	LoaderPostgres = "postgres"
)
View Source
const (
	TplModeSingle = "single"
	TplModeMulti  = "multi"
)
View Source
const (
	Bool    Type = "bool"
	Binary       = "binary"
	Bit          = "bit"
	Int8         = "int8"
	Uint8        = "uint8"
	Int16        = "int16"
	Uint16       = "uint16"
	Int32        = "int32"
	Uint32       = "uint32"
	Int64        = "int64"
	Uint64       = "uint64"
	Float32      = "float32"
	Float64      = "float64"
	String       = "string"
	Time         = "time"
	Enum         = "enum"
	UUID         = "uuid"
	JSON         = "json"
)
View Source
const (
	DefaultIDName = "id"
)

Variables

View Source
var (
	Funcs = template.FuncMap{
		"receiver": receiver,
		"snake":    snake,
		"pascal":   pascal,
		"camel":    camel,
		"plural":   plural,
		"singular": singular,
		"attrs":    attrs,
	}
)

Functions

func MustParse

func MustParse(t *template.Template, err error) *template.Template

Types

type Attr added in v0.1.4

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

func NewAttr added in v0.1.4

func NewAttr(name string, value any) *Attr

func (*Attr) Name added in v0.1.4

func (a *Attr) Name() string

func (*Attr) Value added in v0.1.4

func (a *Attr) Value() any

type Binder

type Binder struct {
	Dialect string
}

func (*Binder) Rebind

func (b *Binder) Rebind(query string) string

type Config

type Config struct {
	Project   string         `yaml:"project" mapstructure:"project"` // the name of the project.
	Package   string         `yaml:"package" mapstructure:"package"` // the name of the generated package.
	Header    string         `yaml:"header" mapstructure:"header"`
	Dialect   string         `yaml:"dialect" mapstructure:"dialect"` // the name of the dialect.
	DSN       string         `yaml:"dsn" mapstructure:"dsn"`
	Overwrite bool           `yaml:"overwrite" mapstructure:"overwrite"`
	Delim     Delim          `yaml:"delim" mapstructure:"delim"`     // 模板变量标识符
	Root      string         `yaml:"root" mapstructure:"root"`       // 模板根目录
	GenRoot   string         `yaml:"genRoot" mapstructure:"genRoot"` // 生成根目录
	Attrs     map[string]any `yaml:"attrs" mapstructure:"attrs"`     // 其他配置项

	// Templates 所有的 Template Path 需要保证唯一,实际模板文件路径仅为更好的组织文件
	Templates []*Template `yaml:"templates" mapstructure:"templates"`

	Tables []*Table `yaml:"tables" mapstructure:"tables"`
}

type Delim

type Delim struct {
	Left  string `yaml:"left" mapstructure:"left"`
	Right string `yaml:"right" mapstructure:"right"`
}

type Field

type Field struct {
	Name       string         `yaml:"name" mapstructure:"name"`
	Type       Type           `yaml:"type" mapstructure:"type"` // 指定字段类型,优先级高于数据库定义
	Nullable   bool           `yaml:"nullable" mapstructure:"nullable"`
	Optional   bool           `yaml:"optional" mapstructure:"optional"`
	Comment    string         `yaml:"comment" mapstructure:"comment"`
	Order      int            `yaml:"order" mapstructure:"order"` // Order 字段顺序
	Alias      string         `yaml:"alias" mapstructure:"alias"`
	Skip       bool           `yaml:"skip" mapstructure:"skip"` // Skip 忽略表
	Sortable   bool           `yaml:"sortable" mapstructure:"sortable"`
	Filterable *bool          `yaml:"filterable" mapstructure:"filterable"`
	Operations []string       `yaml:"operations" mapstructure:"operations"`
	Remote     bool           `yaml:"remote" mapstructure:"remote"`
	Relation   *Relation      `yaml:"relation" mapstructure:"relation"`
	Attrs      map[string]any `yaml:"attrs" mapstructure:"attrs"` // 其他配置项
}

type Generator

type Generator struct {
	Cfg *Config

	Loader cre.Loader
	Binder *Binder
	// contains filtered or unexported fields
}

func NewGenerator

func NewGenerator(cfg *Config, loader cre.Loader) (*Generator, error)

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context) error

func (*Generator) Schema added in v0.1.2

func (g *Generator) Schema() *spec.Schema

func (*Generator) Template

func (g *Generator) Template(name string, v any) (string, error)

type JoinTable

type JoinTable struct {
	Name     string         `yaml:"name" mapstructure:"name"`
	Table    string         `yaml:"table" mapstructure:"table"`
	RefTable string         `yaml:"ref_table" mapstructure:"ref_table"`
	Field    string         `yaml:"field" mapstructure:"field"`
	RefField string         `yaml:"ref_field" mapstructure:"ref_field"`
	Attrs    map[string]any `yaml:"attrs" mapstructure:"attrs"` // 其他配置项
}

type Relation

type Relation struct {
	Name      string         `yaml:"name" mapstructure:"name"`
	Type      string         `yaml:"type" mapstructure:"type"`
	Field     string         `yaml:"field" mapstructure:"field"`
	RefTable  string         `yaml:"ref_table" mapstructure:"ref_table"`
	RefField  string         `yaml:"ref_field" mapstructure:"ref_field"`
	JoinTable *JoinTable     `yaml:"join_table" mapstructure:"join_table"` // 当 Type 为 ManyToMany 时, JoinTable 不为空
	Inverse   bool           `yaml:"inverse" mapstructure:"inverse"`
	Attrs     map[string]any `yaml:"attrs" mapstructure:"attrs"` // 其他配置项
}

Relation represents a Relation definition.

type Table

type Table struct {
	Name   string         `yaml:"name" mapstructure:"name"`
	Skip   bool           `yaml:"skip" mapstructure:"skip"` // Skip 忽略表
	Fields []*Field       `yaml:"fields" mapstructure:"fields"`
	Attrs  map[string]any `yaml:"attrs" mapstructure:"attrs"` // 其他配置项
}

type Template

type Template struct {
	Path    string `yaml:"path" mapstructure:"path"`       // Path 模板相对路径,相对于Root
	GenPath string `yaml:"genPath" mapstructure:"genPath"` // GenPath 生成路径,相对于GenRoot
	Format  string `yaml:"format" mapstructure:"format"`   // Format 生成文件名格式
	// Mode 生成模式, 可选值: "single", "multi"
	// 默认: single 模式下, 所有表数据生成一个文件
	// multi 模式下, 每个表数据生成一个文件
	Mode string `yaml:"mode" mapstructure:"mode"`
	// M2M 是否 Many To Many 模板
	M2M bool `yaml:"m2m" mapstructure:"m2m"`
}

type Type added in v0.1.2

type Type string

Jump to

Keyboard shortcuts

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