diff --git a/go.mod b/go.mod index 9c9eb46..6feb1bd 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/tiger1103/gfast-cache v1.0.0 github.com/tiger1103/gfast-token v1.0.3 github.com/xuri/excelize/v2 v2.7.1 + golang.org/x/tools v0.6.0 ) require ( @@ -78,12 +79,12 @@ require ( golang.org/x/crypto v0.12.0 // indirect golang.org/x/image v0.5.0 // indirect golang.org/x/lint v0.0.0-20190930215403-16217165b5de // indirect + golang.org/x/mod v0.8.0 // indirect golang.org/x/net v0.14.0 // indirect golang.org/x/sync v0.1.0 // indirect golang.org/x/sys v0.11.0 // indirect golang.org/x/term v0.11.0 // indirect golang.org/x/text v0.12.0 // indirect - golang.org/x/tools v0.6.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/internal/app/system/logic/toolsGenTable/tools_gen_table.go b/internal/app/system/logic/toolsGenTable/tools_gen_table.go index 1386546..07e6d02 100644 --- a/internal/app/system/logic/toolsGenTable/tools_gen_table.go +++ b/internal/app/system/logic/toolsGenTable/tools_gen_table.go @@ -32,6 +32,7 @@ import ( "github.com/tiger1103/gfast/v3/internal/app/system/model/entity" "github.com/tiger1103/gfast/v3/internal/app/system/service" "github.com/tiger1103/gfast/v3/library/liberr" + "golang.org/x/tools/imports" "io" "os" "reflect" @@ -1154,6 +1155,7 @@ func (s *sToolsGenTable) createFile(fileName, data string, cover bool) (err erro f.WriteString(data) } f.Close() + err = s.goFmt(fileName) } return } @@ -1315,3 +1317,29 @@ func (s *sToolsGenTable) SyncTable(ctx context.Context, tableId int64) (err erro }) return } + +// goFmt formats the source file and adds or removes import statements as necessary. +func (s *sToolsGenTable) goFmt(path string)(err error) { + replaceFunc := func(path, content string) string { + res, err := imports.Process(path, []byte(content), nil) + if err != nil { + g.Log().Printf(context.Background(),`error format "%s" go files: %v`, path, err) + return content + } + return string(res) + } + if gfile.IsFile(path) { + // File format. + if gfile.ExtName(path) != "go" { + return + } + err = gfile.ReplaceFileFunc(replaceFunc, path) + } else { + // Folder format. + err = gfile.ReplaceDirFunc(replaceFunc, path, "*.go", true) + } + if err != nil { + err = fmt.Errorf(`error format "%s" go files: %v`, path, err) + } + return +} \ No newline at end of file diff --git a/internal/consts/version.go b/internal/consts/version.go index 8d89334..78132de 100644 --- a/internal/consts/version.go +++ b/internal/consts/version.go @@ -8,5 +8,5 @@ package consts const ( - Version = "3.2.6" + Version = "3.2.7" ) diff --git a/resource/template/vm/go/controller.template b/resource/template/vm/go/controller.template index 8021bb4..e9a0d36 100644 --- a/resource/template/vm/go/controller.template +++ b/resource/template/vm/go/controller.template @@ -21,7 +21,8 @@ package controller {{$hasGconv:=false}} {{$hasSystemApi:=false}} {{$hasCommonService:=false}} -{{range $index, $column := .table.DetailColumns}} +{{if .table.ExcelPort}} +{{range $index, $column := .table.Columns}} {{if ne $column.DictType ""}} {{$hasGmap = true}} {{$hasGconv = true}} @@ -36,6 +37,7 @@ package controller {{end}} {{end}} {{end}} +{{end}} import ( "context" "{{.goModName}}/api/v1/{{.table.ModuleName}}" @@ -101,7 +103,7 @@ func (c *{{$structName}}Controller) Export(ctx context.Context, req *{{.table.Mo listData []*model.{{.table.ClassName}}InfoRes //表头 {{$tableHeader := ""}} - {{range $index, $column := .table.DetailColumns}} + {{range $index, $column := .table.Columns}} {{if and (ne $column.HtmlType "richtext") (ne $column.HtmlType "imagefile") (ne $column.HtmlType "images") (ne $column.HtmlType "file") (ne $column.HtmlType "files")}} {{$tableHeader = concat $tableHeader `"` $column.ColumnComment `"` "," }} {{end}} @@ -149,7 +151,7 @@ func (c *{{$structName}}Controller) Export(ctx context.Context, req *{{.table.Mo } for _,v:=range listData{ var ( - {{range $index, $column := .table.DetailColumns}} + {{range $index, $column := .table.Columns}} {{if and (ne $column.HtmlType "richtext") (ne $column.HtmlType "imagefile") (ne $column.HtmlType "images") (ne $column.HtmlType "file") (ne $column.HtmlType "files")}} {{if or (ne $column.DictType "") (ne $column.LinkTableName "")}} {{if eq $column.HtmlType "checkbox" "" "selects" "treeSelects"}} @@ -173,7 +175,7 @@ func (c *{{$structName}}Controller) Export(ctx context.Context, req *{{.table.Mo {{end}} {{end}} ) - {{range $index, $column := .table.DetailColumns}} + {{range $index, $column := .table.Columns}} {{if and (ne $column.HtmlType "richtext") (ne $column.HtmlType "imagefile") (ne $column.HtmlType "images") (ne $column.HtmlType "file") (ne $column.HtmlType "files")}} {{if ne $column.DictType ""}} {{if eq $column.HtmlType "checkbox" "" "selects" "treeSelects"}} @@ -213,7 +215,7 @@ func (c *{{$structName}}Controller) Export(ctx context.Context, req *{{.table.Mo {{end}} {{end}} dt := []interface{}{ - {{range $index, $column := .table.DetailColumns}} + {{range $index, $column := .table.Columns}} {{if and (ne $column.HtmlType "richtext") (ne $column.HtmlType "imagefile") (ne $column.HtmlType "images") (ne $column.HtmlType "file") (ne $column.HtmlType "files")}} {{if ne $column.DictType ""}} {{if eq $column.HtmlType "checkbox" "" "selects" "treeSelects"}}