From 0706dd0747ac5a699f7474140dc1ae0b8eae27da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=B0=8F=E5=86=9B?= <346944475@qq.com> Date: Tue, 23 Jun 2020 12:10:08 +0800 Subject: [PATCH] add 'DEFAULT NULL' to point MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加默认值为null 转换成指针类型 --- README.md | 1 + README_zh_cn.md | 1 + config.yml | 1 + data/cmd/cmd.go | 10 ---------- data/config/MyIni.go | 20 +++++++++++--------- data/view/cnf/def.go | 4 ++-- data/view/model/common.go | 23 ++++++++++++++++++++--- data/view/model/model.go | 4 ++-- data/view/model/model_test.go | 2 +- 9 files changed, 39 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 74794b4..e7a2b9c 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ is_url_tag : true # Whether to mark web or not is_foreign_key : true # Whether to mark foreign key or not is_gui : false # Whether to operate on gui is_table_name : false # Whether to out GetTableName function +is_null_to_point : false # database is 'DEFAULT NULL' then set element type as point mysql_info : host : "127.0.0.1" diff --git a/README_zh_cn.md b/README_zh_cn.md index a444587..a48e05f 100644 --- a/README_zh_cn.md +++ b/README_zh_cn.md @@ -51,6 +51,7 @@ is_url_tag : true # 是否打web标记 is_foreign_key : true # 是否导出外键关联 is_gui : false # 是否ui模式显示 is_table_name : false # 是否直接生成表名函数 +is_null_to_point : false # 数据库默认 'DEFAULT NULL' 时设置结构为指针类型 mysql_info: host : 127.0.0.1 port : 3306 diff --git a/config.yml b/config.yml index 9d3bd0c..4297f9c 100644 --- a/config.yml +++ b/config.yml @@ -13,6 +13,7 @@ is_web_tag_pk_hidden: true # web标记是否隐藏主键 is_foreign_key : true # 是否导出外键关联 is_gui : false # 是否ui模式显示 is_table_name : false # 是否直接生成表名函数 +is_null_to_point : false # 数据库默认 'DEFAULT NULL' 时设置结构为指针类型 mysql_info: host : 127.0.0.1 port : 3306 diff --git a/data/cmd/cmd.go b/data/cmd/cmd.go index 621bcd7..f744eb8 100644 --- a/data/cmd/cmd.go +++ b/data/cmd/cmd.go @@ -2,7 +2,6 @@ package cmd import ( "os" - "strings" "github.com/xxjwxc/public/mylog" @@ -21,7 +20,6 @@ var foreignKey bool var funcKey bool var ui bool var urlTag string -var outFileName string var rootCmd = &cobra.Command{ Use: "main", @@ -73,8 +71,6 @@ func init() { rootCmd.PersistentFlags().StringVarP(&urlTag, "url", "l", "", "url标签(json,url)") rootCmd.MarkFlagRequired("url tag") - rootCmd.Flags().StringVar(&outFileName, "outfilename", "", "输出文件名,默认以数据库名称命名") - rootCmd.Flags().IntVar(&mysqlInfo.Port, "port", 3306, "端口号") } @@ -114,12 +110,6 @@ func MergeMysqlDbInfo() { if len(urlTag) > 0 { config.SetURLTag(urlTag) } - if len(outFileName) > 0 { - if !strings.HasSuffix(outFileName, ".go") { - outFileName += ".go" - } - config.SetOutFileName(outFileName) - } config.SetMysqlDbInfo(&tmp) diff --git a/data/config/MyIni.go b/data/config/MyIni.go index 9f87e09..2da009f 100644 --- a/data/config/MyIni.go +++ b/data/config/MyIni.go @@ -23,7 +23,7 @@ type Config struct { IsOutFunc bool `yaml:"is_out_func"` IsGUI bool `yaml:"is_gui"` // IsTableName bool `yaml:"is_table_name"` - OutFileName string `yaml:"-"` + IsNullToPoint bool `yaml:"is_null_to_point"` // null to porint } // MysqlDbInfo mysql database information. mysql 数据库信息 @@ -150,14 +150,6 @@ func SetIsTableName(b bool) { _map.IsTableName = b } -func SetOutFileName(f string) { - _map.OutFileName = f -} - -func GetOutFileName() string { - return _map.OutFileName -} - // GetURLTag get url tag. func GetURLTag() string { if _map.URLTag != "json" && _map.URLTag != "url" { @@ -203,3 +195,13 @@ func GetDBTag() string { func SetDBTag(s string) { _map.DbTag = s } + +// SetIsNullToPoint if with null to porint in struct +func SetIsNullToPoint(b bool) { + _map.IsNullToPoint = b +} + +// GetIsNullToPoint get if with null to porint in sturct +func GetIsNullToPoint() bool { + return _map.IsNullToPoint +} diff --git a/data/view/cnf/def.go b/data/view/cnf/def.go index af498a8..c3582d2 100644 --- a/data/view/cnf/def.go +++ b/data/view/cnf/def.go @@ -23,8 +23,8 @@ var TypeMysqlDicMp = map[string]string{ "bit(1)": "[]uint8", "tinyint": "int8", "tinyint unsigned": "uint8", - "tinyint(1)": "bool", - "tinyint(1) unsigned": "bool", + "tinyint(1)": "bool", // tinyint(1) 默认设置成bool + "tinyint(1) unsigned": "bool", // tinyint(1) 默认设置成bool "json": "string", "text": "string", "timestamp": "time.Time", diff --git a/data/view/model/common.go b/data/view/model/common.go index e92f849..ac2a275 100644 --- a/data/view/model/common.go +++ b/data/view/model/common.go @@ -55,22 +55,39 @@ func FilterKeywords(src string) string { } // getTypeName Type acquisition filtering.类型获取过滤 -func getTypeName(name string) string { +func getTypeName(name string, isNull bool) string { // Precise matching first.先精确匹配 if v, ok := cnf.TypeMysqlDicMp[name]; ok { - return v + return fixNullToPorint(v, isNull) } // Fuzzy Regular Matching.模糊正则匹配 for k, v := range cnf.TypeMysqlMatchMp { if ok, _ := regexp.MatchString(k, name); ok { - return v + return fixNullToPorint(v, isNull) } } panic(fmt.Sprintf("type (%v) not match in any way.maybe need to add on (https://github.com/xxjwxc/gormt/blob/master/data/view/cnf/def.go)", name)) } +// 过滤null point 类型 +func fixNullToPorint(name string, isNull bool) string { + if isNull && config.GetIsNullToPoint() { + if strings.HasPrefix(name, "uint") { + return "*" + name + } + if strings.HasPrefix(name, "int") { + return "*" + name + } + if strings.HasPrefix(name, "float") { + return "*" + name + } + } + + return name +} + func getUninStr(left, middle, right string) string { re := left if len(right) > 0 { diff --git a/data/view/model/model.go b/data/view/model/model.go index 2ae9a11..6b477c1 100644 --- a/data/view/model/model.go +++ b/data/view/model/model.go @@ -81,7 +81,7 @@ func (m *_Model) genTableElement(cols []ColumnsInfo) (el []genstruct.GenElement) } else { tmp.SetName(getCamelName(v.Name)) tmp.SetNotes(v.Notes) - tmp.SetType(getTypeName(v.Type)) + tmp.SetType(getTypeName(v.Type, v.IsNull)) for _, v1 := range v.Index { switch v1.Key { // case ColumnsKeyDefault: @@ -243,7 +243,7 @@ func (m *_Model) generateFunc() (genOut []GenOutInfo) { pkg.AddImport(`"time"`) buildFList(&primary, ColumnsKeyPrimary, "", "int64", "id") } else { - typeName := getTypeName(el.Type) + typeName := getTypeName(el.Type, el.IsNull) isMulti := (len(el.Index) == 0) for _, v1 := range el.Index { if v1.Multi { diff --git a/data/view/model/model_test.go b/data/view/model/model_test.go index a95cd3b..6e3c9a6 100644 --- a/data/view/model/model_test.go +++ b/data/view/model/model_test.go @@ -9,7 +9,7 @@ import ( ) func TestTypeName(t *testing.T) { - fmt.Println(getTypeName("tinyint")) + fmt.Println(getTypeName("tinyint", true)) } func TestTools(t *testing.T) {