Skip to content

Commit

Permalink
Merge pull request #1 from polaris-team/master
Browse files Browse the repository at this point in the history
Add Json Tag camel case configuration.
  • Loading branch information
fizzday authored Aug 2, 2019
2 parents c92ef17 + 362eac9 commit 2f8bd4b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
31 changes: 22 additions & 9 deletions table2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"database/sql"
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
"os"
"os/exec"
"strings"
_ "github.com/go-sql-driver/mysql"
)

//map for converting mysql type to golang types
Expand All @@ -17,13 +17,13 @@ var typeForMysqlToGo = map[string]string{
"tinyint": "int",
"smallint": "int",
"mediumint": "int",
"bigint": "int",
"bigint": "int64",
"int unsigned": "int",
"integer unsigned": "int",
"tinyint unsigned": "int",
"smallint unsigned": "int",
"mediumint unsigned": "int",
"bigint unsigned": "int",
"bigint unsigned": "int64",
"bit": "int",
"bool": "bool",
"enum": "string",
Expand Down Expand Up @@ -64,8 +64,10 @@ type Table2Struct struct {
}

type T2tConfig struct {
StructNameToHump bool // 结构体名称是否转为驼峰式,默认为false
RmTagIfUcFirsted bool // 如果字段首字母本来就是大写, 就不添加tag, 默认false添加, true不添加
TagToLower bool // tag的字段名字是否转换为小写, 如果本身有大写字母的话, 默认false不转
JsonTagToHump bool // json tag是否转为驼峰,默认为false,不转换
UcFirstOnly bool // 字段首字母大写的同时, 是否要把其他字母转换为小写,默认false不转换
SeperatFile bool // 每个struct放入单独的文件,默认false,放入同一个文件
}
Expand Down Expand Up @@ -158,6 +160,10 @@ func (t *Table2Struct) Run() error {
tableRealName = tableRealName[len(t.prefix):]
}
tableName := tableRealName
structName := tableName
if t.config.StructNameToHump {
structName = t.camelCase(structName)
}

switch len(tableName) {
case 0:
Expand All @@ -168,7 +174,7 @@ func (t *Table2Struct) Run() error {
tableName = strings.ToUpper(tableName[0:1]) + tableName[1:]
}
depth := 1
structContent += "type " + tableName + " struct {\n"
structContent += "type " + structName + " struct {\n"
for _, v := range item {
//structContent += tab(depth) + v.ColumnName + " " + v.Type + " " + v.Json + "\n"
// 字段注释
Expand All @@ -184,7 +190,7 @@ func (t *Table2Struct) Run() error {
// 添加 method 获取真实表名
if t.realNameMethod != "" {
structContent += fmt.Sprintf("func (*%s) %s() string {\n",
tableName, t.realNameMethod)
structName, t.realNameMethod)
structContent += fmt.Sprintf("%sreturn \"%s\"\n",
tab(depth), tableRealName)
structContent += "}\n\n"
Expand Down Expand Up @@ -249,7 +255,7 @@ func (t *Table2Struct) getColumns(table ...string) (tableColumns map[string][]co
WHERE table_schema = DATABASE()`
// 是否指定了具体的table
if t.table != "" {
sqlStr += fmt.Sprintf(" AND TABLE_NAME = '%s'", t.prefix + t.table)
sqlStr += fmt.Sprintf(" AND TABLE_NAME = '%s'", t.prefix+t.table)
}
// sql排序
sqlStr += " order by TABLE_NAME asc, ORDINAL_POSITION asc"
Expand All @@ -276,6 +282,7 @@ func (t *Table2Struct) getColumns(table ...string) (tableColumns map[string][]co
col.ColumnComment = col.ColumnComment
col.ColumnName = t.camelCase(col.ColumnName)
col.Type = typeForMysqlToGo[col.Type]
jsonTag := col.Tag
// 字段首字母本身大写, 是否需要删除tag
if t.config.RmTagIfUcFirsted &&
col.ColumnName[0:1] == strings.ToUpper(col.ColumnName[0:1]) {
Expand All @@ -284,18 +291,24 @@ func (t *Table2Struct) getColumns(table ...string) (tableColumns map[string][]co
// 是否需要将tag转换成小写
if t.config.TagToLower {
col.Tag = strings.ToLower(col.Tag)
jsonTag = col.Tag
}

if t.config.JsonTagToHump {
jsonTag = t.camelCase(jsonTag)
}

//if col.Nullable == "YES" {
// col.Json = fmt.Sprintf("`json:\"%s,omitempty\"`", col.Json)
//} else {
//}
}
if t.tagKey=="" {
t.tagKey="orm"
if t.tagKey == "" {
t.tagKey = "orm"
}
if t.enableJsonTag {
//col.Json = fmt.Sprintf("`json:\"%s\" %s:\"%s\"`", col.Json, t.config.TagKey, col.Json)
col.Tag = fmt.Sprintf("`%s:\"%s\" json:\"%s\"`", t.tagKey, col.Tag, col.Tag)
col.Tag = fmt.Sprintf("`%s:\"%s\" json:\"%s\"`", t.tagKey, col.Tag, jsonTag)
} else {
col.Tag = fmt.Sprintf("`%s:\"%s\"`", t.tagKey, col.Tag)
}
Expand Down

0 comments on commit 2f8bd4b

Please sign in to comment.