add config with self_define_type_mysql_dic_map out_file_name web_tag_type

This commit is contained in:
pengwenwu
2021-03-19 16:43:55 +08:00
parent 3933212bbc
commit 0153517cda
6 changed files with 64 additions and 3 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ db/oauth_db.go
/model /model
gormt.yml gormt.yml
gormt gormt
/vendor

View File

@@ -21,6 +21,11 @@ db_info:
password : 123456 password : 123456
database : matrix database : matrix
type: 0 # 数据库类型:0:mysql , 1:sqlite , 2:mssql type: 0 # 数据库类型:0:mysql , 1:sqlite , 2:mssql
self_define_type_mysql_dic_map: # 自定义数据类型映射
datetime: time.Time
date: time.Time
out_file_name: "" # 自定义生成文件名
web_tag_type: 0 # json tag类型 0: 下划线 1: 小驼峰
# sqlite # sqlite
# db_info: # db_info:

View File

@@ -24,6 +24,9 @@ type Config struct {
IsTableName bool `yaml:"is_table_name"` IsTableName bool `yaml:"is_table_name"`
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
TablePrefix string `yaml:"table_prefix"` // 表前缀 TablePrefix string `yaml:"table_prefix"` // 表前缀
SelfDefineTypeMysqlDicMap map[string]string `yaml:"self_define_type_mysql_dic_map"`
OutFileName string `yaml:"out_file_name"`
WebTagType int `yaml:"web_tag_type"`
} }
// DBInfo mysql database information. mysql 数据库信息 // DBInfo mysql database information. mysql 数据库信息
@@ -221,3 +224,33 @@ func SetTablePrefix(t string) {
func GetTablePrefix() string { func GetTablePrefix() string {
return _map.TablePrefix return _map.TablePrefix
} }
// SetSelfDefineTypeMysqlDicMap 设置自定义字段映射
func SetSelfDefineTypeMysqlDicMap(data map[string]string) {
_map.SelfDefineTypeMysqlDicMap = data
}
// GetSelfDefineTypeMysqlDicMap 获取自定义字段映射
func GetSelfDefineTypeMysqlDicMap() map[string]string {
return _map.SelfDefineTypeMysqlDicMap
}
// SetOutFileName 设置输出文件名
func SetOutFileName(s string) {
_map.OutFileName = s
}
// GetOutFileName 获取输出文件名
func GetOutFileName() string {
return _map.OutFileName
}
// SetWebTagType 设置json tag类型
func SetWebTagType(i int) {
_map.WebTagType = i
}
// GetWebTagType 获取json tag类型
func GetWebTagType() int {
return _map.WebTagType
}

View File

@@ -44,6 +44,9 @@ var _map = Config{
IsOutFunc: true, IsOutFunc: true,
IsGUI: false, IsGUI: false,
TablePrefix: "", TablePrefix: "",
SelfDefineTypeMysqlDicMap: make(map[string]string),
OutFileName: "",
WebTagType: 0,
} }
var configPath string var configPath string

View File

@@ -67,6 +67,12 @@ func FilterKeywords(src string) string {
// getTypeName Type acquisition filtering.类型获取过滤 // getTypeName Type acquisition filtering.类型获取过滤
func getTypeName(name string, isNull bool) string { func getTypeName(name string, isNull bool) string {
// 优先匹配自定义类型
selfDefineTypeMqlDicMap := config.GetSelfDefineTypeMysqlDicMap()
if v, ok := selfDefineTypeMqlDicMap[name]; ok {
return fixNullToPorint(v, isNull)
}
// Precise matching first.先精确匹配 // Precise matching first.先精确匹配
if v, ok := cnf.TypeMysqlDicMp[name]; ok { if v, ok := cnf.TypeMysqlDicMp[name]; ok {
return fixNullToPorint(v, isNull) return fixNullToPorint(v, isNull)

View File

@@ -29,7 +29,11 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
// struct // struct
var stt GenOutInfo var stt GenOutInfo
stt.FileCtx = m.generate() stt.FileCtx = m.generate()
stt.FileName = info.DbName + ".go" if name := config.GetOutFileName(); name != "" {
stt.FileName = name + ".go"
} else {
stt.FileName = info.DbName + ".go"
}
out = append(out, stt) out = append(out, stt)
// ------end // ------end
@@ -131,7 +135,11 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
if isPK && config.GetIsWebTagPkHidden() { if isPK && config.GetIsWebTagPkHidden() {
tmp.AddTag(_tagJSON, "-") tmp.AddTag(_tagJSON, "-")
} else { } else {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.Name))) if config.GetWebTagType() == 0 {
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.Name))
} else {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.Name)))
}
} }
} }
@@ -174,7 +182,12 @@ func (m *_Model) genForeignKey(col ColumnsInfo) (fklist []genstruct.GenElement)
// json tag // json tag
if config.GetIsWEBTag() { if config.GetIsWEBTag() {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.TableName))+"List") if config.GetWebTagType() == 0 {
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.TableName)+"_list")
} else {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.TableName))+"List")
}
} }
fklist = append(fklist, tmp) fklist = append(fklist, tmp)