tpl

package
v0.0.0-...-6dcb45d Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GormFieldTag_Type          = "type"
	GormFieldTag_Size          = "size"
	GormFieldTag_Primary       = "primary"
	GormFieldTag_Unique        = "unique"
	GormFieldTag_Default       = "default"
	GormFieldTag_Precision     = "precision"
	GormFieldTag_NotNull       = "not_null"
	GormFieldTag_AutoIncrement = "auto_increment"
	GormFieldTag_Index         = "index"
	GormFieldTag_UniqueIndex   = "unique_index"
)
View Source
const (
	// 0单字段、1数组、2子结构体
	ColumnCfgTypeSimple = 0
	ColumnCfgTypeArray  = 1
	ColumnCfgTypeStruct = 2
)
View Source
const (
	ColumnTypeInt8    = "int8"
	ColumnTypeUInt8   = "uint8"
	ColumnTypeInt16   = "int16"
	ColumnTypeUInt16  = "uint16"
	ColumnTypeInt32   = "int32"
	ColumnTypeUInt32  = "uint32"
	ColumnTypeInt64   = "int64"
	ColumnTypeUInt64  = "uint64"
	ColumnTypeFloat32 = "float32"
	ColumnTypeFloat64 = "float64"

	ColumnTypeBool    = "bool"
	ColumnTypeVarchar = "varchar"
	ColumnTypeBinary  = "blob"
	ColumnTypeDecimal = "decimal"

	ColumnTypeTime     = "time"
	ColumnTypeDate     = "date"
	ColumnTypeDateTime = "datetime"
)

Constants for database(mysql) types

Variables

View Source
var Debug = false

Debug level logging

Functions

func ExecuteShell

func ExecuteShell(cmdName string, cmdArgs []string) (ret string, err error)

执行命令

func FormatFieldName

func FormatFieldName(s string) string

FormatFieldName formats a string as a struct key

Example:

FormatFieldName("foo_id")

Output: FooID

func GenerateTemplate

func GenerateTemplate(tmplFile, outFile string, data interface{}) error

func PathExists

func PathExists(path string) (bool, error)

Types

type Attribute

type Attribute map[string]interface{}

func (Attribute) Exists

func (a Attribute) Exists(key string) bool

func (Attribute) GetAllGormTag

func (a Attribute) GetAllGormTag() (res string)

func (Attribute) GetBool

func (a Attribute) GetBool(key string) bool

func (Attribute) GetGormTag

func (a Attribute) GetGormTag(key string) string

func (Attribute) GetString

func (a Attribute) GetString(key string) string

type AutoIndex

type AutoIndex struct {
	Name   string    // 索引名
	Fields []*Column `json:"fields"` // 所有字段
}

type Column

type Column struct {
	Name  string     `json:"name"`  // 字段名字
	Note  string     `json:"note"`  // 字段说明
	Type  ColumnType `json:"type"`  // 字段类型
	Attrs Attribute  `json:"attrs"` // 字段属性
}

func (*Column) FillDefault

func (o *Column) FillDefault()

填充默认值

func (*Column) FormattedName

func (o *Column) FormattedName() string

func (*Column) GoTypeName

func (o *Column) GoTypeName() string

func (*Column) GormTag

func (o *Column) GormTag() string

func (*Column) JsonTag

func (o *Column) JsonTag() string

type ColumnCfg

type ColumnCfg struct {
	// "name", "type", "comment", "primary", "notnull", "unique", "auto_increment", "default"
	Name          string // 字段名
	Type          string // 在yaml文件中的字段名
	Comment       string `yaml:"comment,omitempty"`       // 字段说明,从属性表中抽出来的
	Primary       bool   `yaml:"primary,omitempty"`       // 是否是主键
	NotNull       bool   `yaml:"notnull,omitempty"`       // 是否不允许为空
	Unique        bool   `yaml:"unique,omitempty"`        // 是否必须唯一
	AutoIncrement bool   `yaml:"autoIncrement,omitempty"` // 是否自增
	Default       string `yaml:"default,omitempty"`       // 默认值
	GoType        string `yaml:"goType,omitempty"`        // 生成go代码时的type名
	GoImport      string `yaml:"goImport,omitempty"`      // 生成go代码时的import路径
	FieldType     int    `yaml:"fieldType,omitempty"`     // 0单字段、1数组、2子结构体

	//Type  ColumnType `json:"type"`
	// column: name type comment default primary notnull unique auto_increment
	Attrs Attribute `yaml:"-" json:"attrs"`

	Children map[string]*ColumnCfg `yaml:"children,omitempty"` // 如果是子结构体,则包含子结构体的字段
}

字段配置

func NewColumnCfg

func NewColumnCfg() *ColumnCfg

func (*ColumnCfg) GetGormTag

func (c *ColumnCfg) GetGormTag() string

获取当前字段的gorm tag

func (*ColumnCfg) GoImportsArray

func (c *ColumnCfg) GoImportsArray(imports []string) []string

获取goimports导入数组

func (*ColumnCfg) GoImportsString

func (c *ColumnCfg) GoImportsString() string

获取goimports导入字符串,用换行符分割

func (*ColumnCfg) GoTypeString

func (c *ColumnCfg) GoTypeString() string

获取类型,优先使用GoType,次选Type

type ColumnCfgType

type ColumnCfgType int

type ColumnDict

type ColumnDict map[string]*ColumnCfg

字段的数据字典

type ColumnType

type ColumnType string

type IndexCfg

type IndexCfg struct {
	Name    string   // 索引名
	Keys    []string // 简单、结构体、数组
	Comment string   // 说明
	Type    string   `yaml:"type,omitempty"` // 索引类型, index或unique
}

索引配置

type ModelCfg

type ModelCfg struct {
	// Struct  StructCfg  // model中的字段定义
	Name       string                // 表明
	Comment    string                // 表的说明
	Columns    []*ColumnCfg          `yaml:"column"` // model中的字段定义
	ColumnDict map[string]*ColumnCfg `yaml:"-"`      // model中的字段定义
	Indexes    []*IndexCfg           `yaml:"index"`  // model中的索引定义
	IndexDict  map[string]*IndexCfg  `yaml:"-"`      // model中的索引定义
	Query      []*QueryCfg           `yaml:"query"`  // 包中的查询配置
	QueryMap   QueryMap              `yaml:"-"`      // 包中的查询配置
}

model定义

func NewModelCfg

func NewModelCfg() *ModelCfg

func (*ModelCfg) GetGormIndexTag

func (m *ModelCfg) GetGormIndexTag(columnName string) string

获取当前字段的gorm tag

func (*ModelCfg) GetGormTag

func (m *ModelCfg) GetGormTag(columnName string) string

获取字段的gorm tag,包含本身以及索引的

func (*ModelCfg) GoImportsArray

func (m *ModelCfg) GoImportsArray() []string

获取goimports导入数组

func (*ModelCfg) GoImportsString

func (m *ModelCfg) GoImportsString() string

获取goimports导入字符串,用换行符分割

type ModelMap

type ModelMap map[string]*ModelCfg

model按表名进行组织

type PackageCfg

type PackageCfg struct {
	Name       string       // 包名
	Type       []*TypeCfg   `yaml:"type"`  // 类型字典
	TypeDict   TypeDict     `yaml:"-"`     // 类型字典
	Column     []*ColumnCfg `yaml:"dict"`  // 字段字典
	ColumnDict ColumnDict   `yaml:"-"`     // 字段字典
	Model      []*ModelCfg  `yaml:"model"` // 包中的model配置
	ModelMap   ModelMap     `yaml:"-"`     // 包中的model配置

	CurrentModel *ModelCfg `yaml:"-"` // 包中的查询配置
	CurrentQuery *QueryCfg `yaml:"-"` // 包中的查询配置
}

package 定义

func NewPackageCfg

func NewPackageCfg() *PackageCfg

func ParseConfigure

func ParseConfigure(file string) (*PackageCfg, error)

type QueryCfg

type QueryCfg struct {
	Name    string     // 查询的名字
	Comment string     `yaml:"comment,omitempty"` // 查询的说明
	SQLstr  string     `yaml:"sql,omitempty"`     // 查询用到的sql语句
	Pager   bool       `yaml:"pager,omitempty"`   // 当sql为查询时,且输出为数组时,是否启用分页,默认不启用
	In      *StructCfg `yaml:"inputs,omitempty"`  // 查询的输入参数
	Out     *StructCfg `yaml:"outputs,omitempty"` // 查询的输出参数
}

查询

func (*QueryCfg) FormatCountSQL

func (q *QueryCfg) FormatCountSQL() SqlRes

func (*QueryCfg) FormatSQL

func (q *QueryCfg) FormatSQL() SqlRes

func (*QueryCfg) GoImportsArray

func (q *QueryCfg) GoImportsArray(imports []string) []string

获取goimports导入数组

func (*QueryCfg) GoImportsString

func (q *QueryCfg) GoImportsString() string

获取goimports导入字符串,用换行符分割

type QueryMap

type QueryMap map[string]*QueryCfg

query按查询名进行组织

type SqlRes

type SqlRes struct {
	SQL   string
	Names []string
}

type StructCfg

type StructCfg struct {
	Name       string                // 结构体名
	Array      bool                  // 输出是否为数组
	Columns    []*ColumnCfg          `yaml:"column"` // 包含的字段
	ColumnDict map[string]*ColumnCfg `yaml:"-"`      // 包含的字段
}

结构体配置

func NewStructCfg

func NewStructCfg() *StructCfg

func (*StructCfg) GoImportsArray

func (c *StructCfg) GoImportsArray(imports []string) []string

获取goimports导入数组

func (*StructCfg) GoImportsString

func (c *StructCfg) GoImportsString() string

获取goimports导入字符串,用换行符分割

type Table

type Table struct {
	Name        string   `json:"name"`   // 表名
	Note        string   `json:"note"`   // 表的说明
	Fields      []Column `json:"fields"` // 所有字段
	PrimaryKeys []string // 主键名数组,从Fields中提取出来的
	Params      []string `json:"params"`  // 用于查询
	Indexes     []string `json:"indexes"` // 索引数组

	AutoIndexes map[string]*AutoIndex `json:"AutoIndexes"` // 根据字段,自动建立的索引数组
}

func (*Table) FillDefault

func (o *Table) FillDefault()

填充默认值

func (*Table) GetModelFileName

func (o *Table) GetModelFileName(tablename string) string

func (*Table) GetModelPath

func (o *Table) GetModelPath() string

func (*Table) GetPrimaryIntKeys

func (e *Table) GetPrimaryIntKeys() (keys []string)

func (*Table) GetPrimaryStringKeys

func (e *Table) GetPrimaryStringKeys() (keys []string)

func (*Table) HasDateTimeField

func (e *Table) HasDateTimeField() bool

type TypeCfg

type TypeCfg struct {
	Type     string // 在yaml文件中的字段名
	GoType   string `yaml:"goType,omitempty"`   // 生成go代码时的type名
	GoImport string `yaml:"goImport,omitempty"` // 生成go代码时的import路径
}

数据类型

type TypeDict

type TypeDict map[string]*TypeCfg

类型字典

Jump to

Keyboard shortcuts

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