Skip to content

Commit

Permalink
Merge pull request #125 from xxjwxc/pengwenwu-dev
Browse files Browse the repository at this point in the history
Pengwenwu dev
  • Loading branch information
xxjwxc committed Mar 20, 2021
2 parents 3933212 + 8250592 commit 01388f3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ db/oauth_db.go
/model
gormt.yml
gormt
/vendor
6 changes: 5 additions & 1 deletion README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ db_info:
password : qwer
database : oauth_db
type: 0 # 数据库类型:0:mysql , 1:sqlite , 2:mssql

self_type_define: # 自定义数据类型映射
datetime: time.Time
date: time.Time
out_file_name: "" # 自定义生成文件名
web_tag_type: 0 # json tag类型 0: 小驼峰 1: 下划线

```

Expand Down
5 changes: 5 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ db_info:
password : 123456
database : matrix
type: 0 # 数据库类型:0:mysql , 1:sqlite , 2:mssql
self_type_define: # 自定义数据类型映射
datetime: time.Time
time: time.Time
out_file_name: "" # 自定义生成文件名
web_tag_type: 0 # json tag类型 0: 小驼峰 1: 下划线

# sqlite
# db_info:
Expand Down
63 changes: 48 additions & 15 deletions data/config/MyIni.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ import (
// Config custom config struct
type Config struct {
CfgBase `yaml:"base"`
DBInfo DBInfo `yaml:"db_info"`
OutDir string `yaml:"out_dir"`
URLTag string `yaml:"url_tag"` // url tag
Language string `yaml:"language"` // language
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
Simple bool `yaml:"simple"`
IsWEBTag bool `yaml:"is_web_tag"`
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
IsForeignKey bool `yaml:"is_foreign_key"`
IsOutSQL bool `yaml:"is_out_sql"`
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
TablePrefix string `yaml:"table_prefix"` // 表前缀
DBInfo DBInfo `yaml:"db_info"`
OutDir string `yaml:"out_dir"`
URLTag string `yaml:"url_tag"` // url tag
Language string `yaml:"language"` // language
DbTag string `yaml:"db_tag"` // 数据库标签(gormt,db)
Simple bool `yaml:"simple"`
IsWEBTag bool `yaml:"is_web_tag"`
IsWebTagPkHidden bool `yaml:"is_web_tag_pk_hidden"` // web标记是否隐藏主键
IsForeignKey bool `yaml:"is_foreign_key"`
IsOutSQL bool `yaml:"is_out_sql"`
IsOutFunc bool `yaml:"is_out_func"`
IsGUI bool `yaml:"is_gui"` //
IsTableName bool `yaml:"is_table_name"`
IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint
TablePrefix string `yaml:"table_prefix"` // 表前缀
SelfTypeDef map[string]string `yaml:"self_type_define"`
OutFileName string `yaml:"out_file_name"`
WebTagType int `yaml:"web_tag_type"` // 默认小驼峰
}

// DBInfo mysql database information. mysql 数据库信息
Expand Down Expand Up @@ -221,3 +224,33 @@ func SetTablePrefix(t string) {
func GetTablePrefix() string {
return _map.TablePrefix
}

// SetSelfTypeDefine 设置自定义字段映射
func SetSelfTypeDefine(data map[string]string) {
_map.SelfTypeDef = data
}

// GetSelfTypeDefine 获取自定义字段映射
func GetSelfTypeDefine() map[string]string {
return _map.SelfTypeDef
}

// 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
}
2 changes: 2 additions & 0 deletions data/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var _map = Config{
IsOutFunc: true,
IsGUI: false,
TablePrefix: "",
SelfTypeDef: make(map[string]string),
WebTagType: 0,
}

var configPath string
Expand Down
6 changes: 6 additions & 0 deletions data/view/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func FilterKeywords(src string) string {

// getTypeName Type acquisition filtering.类型获取过滤
func getTypeName(name string, isNull bool) string {
// 优先匹配自定义类型
selfDefineTypeMqlDicMap := config.GetSelfTypeDefine()
if v, ok := selfDefineTypeMqlDicMap[name]; ok {
return fixNullToPorint(v, isNull)
}

// Precise matching first.先精确匹配
if v, ok := cnf.TypeMysqlDicMp[name]; ok {
return fixNullToPorint(v, isNull)
Expand Down
16 changes: 14 additions & 2 deletions data/view/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func Generate(info DBInfo) (out []GenOutInfo, m _Model) {
stt.FileCtx = m.generate()
stt.FileName = info.DbName + ".go"

if name := config.GetOutFileName(); len(name) > 0 {
stt.FileName = name + ".go"
}

out = append(out, stt)
// ------end

Expand Down Expand Up @@ -131,7 +135,11 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement)
if isPK && config.GetIsWebTagPkHidden() {
tmp.AddTag(_tagJSON, "-")
} else {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.Name)))
if config.GetWebTagType() == 0 {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.Name)))
} else {
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.Name))
}
}
}

Expand Down Expand Up @@ -174,7 +182,11 @@ func (m *_Model) genForeignKey(col ColumnsInfo) (fklist []genstruct.GenElement)

// json tag
if config.GetIsWEBTag() {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.TableName))+"List")
if config.GetWebTagType() == 0 {
tmp.AddTag(_tagJSON, mybigcamel.UnSmallMarshal(mybigcamel.Marshal(v.TableName))+"List")
} else {
tmp.AddTag(_tagJSON, mybigcamel.UnMarshal(v.TableName)+"_list")
}
}

fklist = append(fklist, tmp)
Expand Down

0 comments on commit 01388f3

Please sign in to comment.