Skip to content

Commit

Permalink
add factory file
Browse files Browse the repository at this point in the history
  • Loading branch information
airplayx committed Oct 10, 2023
1 parent d7f165f commit d8b2118
Show file tree
Hide file tree
Showing 793 changed files with 83 additions and 16 deletions.
2 changes: 1 addition & 1 deletion configs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var (
//JSON the default conf
JSON = []byte(`{"const":{"theme":"light","scale":"1.0","language":"en"},"language":{"black":"黑色","light":"白色","default":"默认","others":"其它","effective after restart":"重启后生效","base":"基本","screen":"屏幕","language":"语言","rules":"规则","mapping":"映射","special":"特殊","sql to struct":"sql 转 struct","json to struct":"json 转 struct","edit link":"编辑连接","add link":"添加连接","bad link parameters":"错误的连接参数","Please input %s to delete the current group record":"请输入 %s 确认删除当前组记录","action":"操作","save ok":"保存成功","confirm":"确定","cancel":"取消","Please confirm to delete the current: %s ?":"确认删除当前 %s 连接记录吗?","no":"否","yes":"是","info":"信息","connection test":"测试连接","testing":"测试中","connection successful":"连接成功","the database can not be empty":"数据库名称不能为空","duplicate links":"重复的连接记录","engine":"引擎","host":"主机地址","port":"端口","user":"用户名","password":"密码","database":"数据库","table":"表结构","build only":"仅生成","build and include omitempty":"生成并包含omitempty","carriage return":"回车换行","auto save files":"保存文件","output folder":"输出文件夹","tags":"标签","exclusion table":"排除表","always execute":"始终执行","type conversion":"类型转换","the fields starting with is_ are automatically converted to bool":"is_开头字段自动转为bool","specified fields transformation":"指定字段转换","bool":"布尔值"},"sql2struct":{"target_dir":"./models","auto_save":false,"tags":["gorm","json"],"json_omitempty":false,"exclude_tables":[],"try_complete":true,"tinyint2bool":true,"sourceMap":[],"reflect":"{\"tinyint\":\"int8\",\"smallint\":\"int16\",\"int\":\"int32\",\"bigint\":\"int64\",\"float\":\"float64\",\"double\":\"float64\",\"decimal\":\"float64\",\"char\":\"string\",\"varchar\":\"string\",\"text\":\"string\",\"mediumtext\":\"string\",\"longtext\":\"string\",\"time\":\"time.Time\",\"date\":\"time.Time\",\"datetime\":\"time.Time\",\"timestamp\":\"time.Time\",\"enum\":\"string\",\"set\":\"string\",\"blob\":\"string\"}","special":"{\"id\":\"uint\"}"}}`)
JSON = []byte(`{"const":{"theme":"light","scale":"1.0","language":"en"},"language":{"black":"黑色","light":"白色","default":"默认","others":"其它","effective after restart":"重启后生效","base":"基本","screen":"屏幕","language":"语言","rules":"规则","mapping":"映射","special":"特殊","sql to struct":"sql 转 struct","json to struct":"json 转 struct","edit link":"编辑连接","add link":"添加连接","bad link parameters":"错误的连接参数","Please input %s to delete the current group record":"请输入 %s 确认删除当前组记录","action":"操作","save ok":"保存成功","confirm":"确定","cancel":"取消","Please confirm to delete the current: %s ?":"确认删除当前 %s 连接记录吗?","no":"否","yes":"是","info":"信息","connection test":"测试连接","testing":"测试中","connection successful":"连接成功","the database can not be empty":"数据库名称不能为空","duplicate links":"重复的连接记录","engine":"引擎","host":"主机地址","port":"端口","user":"用户名","password":"密码","database":"数据库","table":"表结构","build only":"仅生成","build and include omitempty":"生成并包含omitempty","carriage return":"回车换行","auto save files":"保存文件","save factory func":"生成工厂函数","output folder":"输出文件夹","tags":"标签","exclusion table":"排除表","always execute":"始终执行","type conversion":"类型转换","the fields starting with is_ are automatically converted to bool":"is_开头字段自动转为bool","specified fields transformation":"指定字段转换","bool":"布尔值"},"sql2struct":{"target_dir":"./models","auto_save":false,"tags":["gorm","json"],"json_omitempty":false,"exclude_tables":[],"try_complete":true,"tinyint2bool":true,"sourceMap":[],"reflect":"{\"tinyint\":\"int8\",\"smallint\":\"int16\",\"int\":\"int32\",\"bigint\":\"int64\",\"float\":\"float64\",\"double\":\"float64\",\"decimal\":\"float64\",\"char\":\"string\",\"varchar\":\"string\",\"text\":\"string\",\"mediumtext\":\"string\",\"longtext\":\"string\",\"time\":\"time.Time\",\"date\":\"time.Time\",\"datetime\":\"time.Time\",\"timestamp\":\"time.Time\",\"enum\":\"string\",\"set\":\"string\",\"blob\":\"string\"}","special":"{\"id\":\"uint\"}"}}`)
//CustomFile the default conf file
CustomFile = "./config.json"
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module gormat

go 1.13

replace fyne.io/fyne => ./vendor/fyne.io/fyne
replace fyne.io/fyne => ./vendor_/fyne.io/fyne

require (
fyne.io/fyne v1.3.2
Expand Down
12 changes: 12 additions & 0 deletions internal/app/sql2struct/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func Option(win fyne.Window, options *sql2struct.SQL2Struct) fyne.Widget {
autoSave.SetSelected(configs.Text("no"))
}

saveFactoryFunc := widget.NewRadio([]string{configs.Text("yes"), configs.Text("no")}, func(s string) {

})
saveFactoryFunc.Horizontal = true
if options.SaveFactoryFunc {
saveFactoryFunc.SetSelected(configs.Text("yes"))
} else {
saveFactoryFunc.SetSelected(configs.Text("no"))
}

gorm := widget.NewCheck("gorm", func(bool) {})
gorm.SetChecked(collection.Collect(options.Tags).Contains("gorm"))

Expand Down Expand Up @@ -67,6 +77,7 @@ func Option(win fyne.Window, options *sql2struct.SQL2Struct) fyne.Widget {
OnSubmit: func() {
options.TargetDir = targetDir.Text
options.AutoSave = autoSave.Selected == configs.Text("yes")
options.SaveFactoryFunc = saveFactoryFunc.Selected == configs.Text("yes")
options.Tags = []string{}
if gorm.Checked {
options.Tags = append(options.Tags, "gorm")
Expand All @@ -92,6 +103,7 @@ func Option(win fyne.Window, options *sql2struct.SQL2Struct) fyne.Widget {
},
Items: []*widget.FormItem{
{Text: configs.Text("auto save files"), Widget: autoSave},
{Text: configs.Text("save factory func"), Widget: saveFactoryFunc},
{Text: configs.Text("output folder"), Widget: targetDir},
{Text: configs.Text("tags"), Widget: gorm},
//{Text: "", Widget: beegoOrm},
Expand Down
19 changes: 10 additions & 9 deletions pkg/sql2struct/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (

//SQL2Struct ...
type SQL2Struct struct {
AutoSave bool `json:"auto_save"`
JSONOmitempty bool `json:"json_omitempty"`
Reflect string `json:"reflect"`
SourceMap []SourceMap `json:"sourceMap"`
Special string `json:"special"`
Tags []string `json:"tags"`
TargetDir string `json:"target_dir"`
Tinyint2bool bool `json:"tinyint2bool"`
AutoSave bool `json:"auto_save"`
SaveFactoryFunc bool `json:"save_factory_func"`
JSONOmitempty bool `json:"json_omitempty"`
Reflect string `json:"reflect"`
SourceMap []SourceMap `json:"sourceMap"`
Special string `json:"special"`
Tags []string `json:"tags"`
TargetDir string `json:"target_dir"`
Tinyint2bool bool `json:"tinyint2bool"`
}

//SourceMap ...
Expand All @@ -38,7 +39,7 @@ type SourceMap struct {
//Configs ...
func Configs() (s2s *SQL2Struct) {
data, _, _, _ := jsonparser.Get(configs.JSON, "sql2struct")
if err := json.Unmarshal([]byte(data), &s2s); err != nil {
if err := json.Unmarshal(data, &s2s); err != nil {
log.Println(err.Error())
}
return
Expand Down
61 changes: 58 additions & 3 deletions pkg/sql2struct/gen_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,22 @@ func (genTool *GenTool) genFile(table *schemas.Table) (by []byte, err error) {
}
//struct
str += fmt.Sprintln("type", v.StructName, "struct {")
for _, v := range v.Fields {
str += fmt.Sprintln(v.FieldName, v.Type, v.Tag, v.Comment)
for _, f := range v.Fields {
str += fmt.Sprintln(f.FieldName, f.Type, f.Tag, f.Comment)
}
str += fmt.Sprintln("}")

//func
str += fmt.Sprintln("func (", v.StructName, ") TableName() string {")
str += fmt.Sprintln("func (f *", v.StructName, ") TableName() string {")
str += fmt.Sprintln(fmt.Sprintf("return `%s` //"+v.Comment, v.TableName))
str += fmt.Sprintln("}")

//setFunc
for _, f := range v.Fields {
str += fmt.Sprintln(fmt.Sprintf("\nfunc (f *%s) Set%s(n %s) {", v.StructName, f.FieldName, f.Type))
str += fmt.Sprintln(fmt.Sprintf("f.%s = n", f.FieldName))
str += fmt.Sprintln("}")
}
//format
by, err = format.Source([]byte(str))
if err != nil {
Expand All @@ -78,11 +85,59 @@ func (genTool *GenTool) genFile(table *schemas.Table) (by []byte, err error) {
return
}
log.Println("gen into file:", file)
if Configs().SaveFactoryFunc {
if err := saveFacFile(genTool.targetDir, genTool.packageName, v); err != nil {
return by, err
}
}
}
}
return
}

func saveFacFile(targetDir, packageName string, v Model) error {
facStr := fmt.Sprintln("package", packageName)
if len(v.Imports) > 0 {
facStr += fmt.Sprintln("import (")
for _, i := range v.Imports {
facStr += fmt.Sprintln(`"` + i + `"`)
}
facStr += fmt.Sprintln(")")
}

//factory
facStr += fmt.Sprintln(fmt.Sprintf("type %sFactory interface {", v.StructName))
for _, f := range v.Fields {
facStr += fmt.Sprintln(fmt.Sprintf("Set%s(%s)", f.FieldName, f.Type))
}
facStr += fmt.Sprintln(fmt.Sprintf("Get%s() %s", v.StructName, v.StructName))
for _, f := range v.Fields {
facStr += fmt.Sprintln(fmt.Sprintf("Get%s() %s", f.FieldName, f.Type))
}
facStr += fmt.Sprintln("}")

//extension
facStr += fmt.Sprintln(fmt.Sprintf("\ntype %sExtension struct {", v.StructName))
facStr += fmt.Sprintln(fmt.Sprintf("*%s", v.StructName))
facStr += fmt.Sprintln("}")
facStr += fmt.Sprintln(fmt.Sprintf("\nfunc (f %sExtension) Get%s() %s {", v.StructName, v.StructName, v.StructName))
facStr += fmt.Sprintln(fmt.Sprintf("return *f.%s", v.StructName))
facStr += fmt.Sprintln("}")
for _, f := range v.Fields {
facStr += fmt.Sprintln(fmt.Sprintf("\nfunc (f %sExtension) Get%s() %s {", v.StructName, f.FieldName, f.Type))
facStr += fmt.Sprintln(fmt.Sprintf("return f.%s", f.FieldName))
facStr += fmt.Sprintln("}")
}
//format
funcData, _ := format.Source([]byte(facStr))
facFile := filepath.Join(targetDir, fmt.Sprintf("%s_factory.go", v.TableName))
if err := ioutil.WriteFile(facFile, funcData, 0644); err != nil {
return err
}
log.Println("gen into file:", facFile)
return nil
}

//Gen ...
func (genTool *GenTool) Gen(table *schemas.Table, dbConf *SourceMap) (result []byte, err error) {
//if err = Init(dbConf); err != nil {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewConfirm(title, message string, callback func(bool), parent fyne.Window)
d.dismiss = &widget.Button{Text: "No", /*Icon: theme.CancelIcon(),*/
OnTapped: d.Hide,
}
confirm := &widget.Button{Text: "Yes", /*Icon: theme.ConfirmIcon(),*/ Style: widget.PrimaryButton,
confirm := &widget.Button{Text: "Yes" /*Icon: theme.ConfirmIcon(),*/, Style: widget.PrimaryButton,
OnTapped: func() {
d.hideWithResponse(true)
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d8b2118

Please sign in to comment.