Merge pull request #129 from crabmanY/master
修改simple配置为true不输出gorm标签,将is_table_name中的column生成单独抽成配置,以免配置的耦合,增加根据表名生成多个model的配置(一个表一个model)
This commit is contained in:
@@ -52,6 +52,8 @@ is_web_tag: false
|
||||
is_web_tag_pk_hidden: false
|
||||
table_prefix: "" #table prefix
|
||||
table_names: "" # Specified table generation, multiple tables with , separated
|
||||
is_column_name: true # Whether to generate column names
|
||||
is_out_file_by_table_name: false # Whether to generate multiple models based on table names
|
||||
db_info :
|
||||
host : "127.0.0.1"
|
||||
port : 3306
|
||||
|
||||
@@ -56,6 +56,8 @@ is_web_tag: false
|
||||
is_web_tag_pk_hidden: false
|
||||
table_prefix: "" #表前缀
|
||||
table_names: "" #指定表生成,多个表用,隔开
|
||||
is_column_name: true # 是否生成列名
|
||||
is_out_file_by_table_name: false # 是否根据表名生成多个model
|
||||
db_info:
|
||||
host : 127.0.0.1
|
||||
port : 3306
|
||||
|
||||
@@ -29,6 +29,8 @@ type Config struct {
|
||||
OutFileName string `yaml:"out_file_name"`
|
||||
WebTagType int `yaml:"web_tag_type"` // 默认小驼峰
|
||||
TableNames string `yaml:"table_names"` // 表名(多个表名用","隔开)
|
||||
IsColumnName bool `yaml:"is_column_name"` //是否输出列名
|
||||
IsOutFileByTableName bool `yaml:"is_out_file_by_table_name"` //是否根据表名生成文件(多个表名生成多个文件)
|
||||
}
|
||||
|
||||
// DBInfo mysql database information. mysql 数据库信息
|
||||
@@ -293,3 +295,23 @@ func GetOriginTableNames() string {
|
||||
func SetTableNames(tableNames string) {
|
||||
_map.TableNames = tableNames
|
||||
}
|
||||
|
||||
//GetIsColumnName get gen columnName config . 获取生成列名的config
|
||||
func GetIsColumnName() bool {
|
||||
return _map.IsColumnName
|
||||
}
|
||||
|
||||
//SetIsColumnName set gen ColumnName config. 设置生成列名的config
|
||||
func SetIsColumnName(isColumnName bool) {
|
||||
_map.IsColumnName = isColumnName
|
||||
}
|
||||
|
||||
//GetIsOutFileByTableName get gen columnName config . 设置是否根据表名生成文件
|
||||
func GetIsOutFileByTableName() bool {
|
||||
return _map.IsOutFileByTableName
|
||||
}
|
||||
|
||||
//SetIsOutFileByTableName set gen ColumnName config. 设置是否根据表名生成文件
|
||||
func SetIsOutFileByTableName(isOutFileByTableName bool) {
|
||||
_map.IsColumnName = isOutFileByTableName
|
||||
}
|
||||
|
||||
@@ -47,6 +47,8 @@ var _map = Config{
|
||||
SelfTypeDef: make(map[string]string),
|
||||
WebTagType: 0,
|
||||
TableNames: "",
|
||||
IsColumnName: true,
|
||||
IsOutFileByTableName: false,
|
||||
}
|
||||
|
||||
var configPath string
|
||||
|
||||
@@ -282,7 +282,9 @@ func (p *GenPackage) Generate() string {
|
||||
for _, v1 := range v.GenerateTableName() {
|
||||
pa.Add(v1)
|
||||
}
|
||||
}
|
||||
|
||||
if config.GetIsColumnName() {
|
||||
for _, v2 := range v.GenerateColumnName() { // add column list
|
||||
pa.Add(v2)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,10 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
|
||||
}
|
||||
|
||||
// struct
|
||||
if config.GetIsOutFileByTableName() {
|
||||
outByTable := m.GenerateByTableName()
|
||||
out = append(out, outByTable...)
|
||||
} else {
|
||||
var stt GenOutInfo
|
||||
stt.FileCtx = m.generate()
|
||||
stt.FileName = info.DbName + ".go"
|
||||
@@ -34,8 +38,9 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
|
||||
if name := config.GetOutFileName(); len(name) > 0 {
|
||||
stt.FileName = name + ".go"
|
||||
}
|
||||
|
||||
out = append(out, stt)
|
||||
}
|
||||
|
||||
// ------end
|
||||
|
||||
// gen function
|
||||
@@ -76,6 +81,28 @@ func (m *_Model) GetPackage() genstruct.GenPackage {
|
||||
return *m.pkg
|
||||
}
|
||||
|
||||
// GetPackageByTableName Generate multiple model files based on the table name. 根据表名生成多个model文件
|
||||
func (m *_Model) GenerateByTableName() (out []GenOutInfo) {
|
||||
if m.pkg == nil {
|
||||
for _, tab := range m.info.TabList {
|
||||
var pkg genstruct.GenPackage
|
||||
pkg.SetPackage(m.info.PackageName) //package name
|
||||
var sct genstruct.GenStruct
|
||||
sct.SetStructName(getCamelName(tab.Name)) // Big hump.大驼峰
|
||||
sct.SetNotes(tab.Notes)
|
||||
sct.AddElement(m.genTableElement(tab.Em)...) // build element.构造元素
|
||||
sct.SetCreatTableStr(tab.SQLBuildStr)
|
||||
sct.SetTableName(tab.Name)
|
||||
pkg.AddStruct(sct)
|
||||
var stt GenOutInfo
|
||||
stt.FileCtx = pkg.Generate()
|
||||
stt.FileName = tab.Name + ".go"
|
||||
out = append(out, stt)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (m *_Model) generate() string {
|
||||
m.pkg = nil
|
||||
m.GetPackage()
|
||||
@@ -96,6 +123,8 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
|
||||
tmp.SetName(getCamelName(v.Name))
|
||||
tmp.SetNotes(v.Notes)
|
||||
tmp.SetType(getTypeName(v.Type, v.IsNull))
|
||||
// not simple output. 默认不输出gorm标签
|
||||
if !config.GetSimple() {
|
||||
for _, v1 := range v.Index {
|
||||
switch v1.Key {
|
||||
// case ColumnsKeyDefault:
|
||||
@@ -115,6 +144,7 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(v.Name) > 0 {
|
||||
// not simple output
|
||||
@@ -124,11 +154,11 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
|
||||
if !v.IsNull {
|
||||
tmp.AddTag(_tagGorm, "not null")
|
||||
}
|
||||
}
|
||||
// default tag
|
||||
if len(v.Gormt) > 0 {
|
||||
tmp.AddTag(_tagGorm, v.Gormt)
|
||||
}
|
||||
}
|
||||
|
||||
// json tag
|
||||
if config.GetIsWEBTag() {
|
||||
|
||||
Reference in New Issue
Block a user