Skip to content

Commit

Permalink
feat(generate): default import model (go-gorm#311)
Browse files Browse the repository at this point in the history
* feat(generate): render concurrently

* perf(generate): set GOMAXPROCS

* feat(generate): improve generate speed
  • Loading branch information
tr1v3r authored Dec 22, 2021
1 parent a8c98f3 commit 9b24d1f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
17 changes: 15 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gen
import (
"fmt"
"path/filepath"
"runtime/debug"
"strings"

"gorm.io/gen/internal/check"
Expand Down Expand Up @@ -37,8 +38,9 @@ type Config struct {

Mode GenerateMode // generate mode

queryPkgName string // generated query code's package name
dbNameOpts []model.SchemaNameOpt
queryPkgName string // generated query code's package name
modelImportPath string
dbNameOpts []model.SchemaNameOpt

// name strategy for syncing table from db
tableNameNS func(tableName string) (targetTableName string)
Expand Down Expand Up @@ -89,10 +91,21 @@ func (cfg *Config) WithNewTagNameStrategy(ns func(columnName string) (tagContent
cfg.fieldNewTagNS = ns
}

var moduleFullPath = func() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
return info.Path
}()

// Revise format path and db
func (cfg *Config) Revise() (err error) {
if strings.TrimSpace(cfg.ModelPkgPath) == "" {
cfg.ModelPkgPath = check.DefaultModelPkg
cfg.modelImportPath = filepath.Dir(filepath.Clean(moduleFullPath+"/"+cfg.OutPath)) + "/" + cfg.ModelPkgPath
} else {
cfg.modelImportPath = filepath.Clean(moduleFullPath + "/" + cfg.ModelPkgPath)
}

cfg.OutPath, err = filepath.Abs(cfg.OutPath)
Expand Down
12 changes: 9 additions & 3 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type M map[string]interface{}
// RowsAffected execute affected raws
type RowsAffected int64

var concurrent = runtime.NumCPU()

func init() { runtime.GOMAXPROCS(runtime.NumCPU()) }

// NewGenerator create a new generator
Expand Down Expand Up @@ -248,7 +250,7 @@ func (g *Generator) generateQueryFile() (err error) {
}

errChan := make(chan error)
pool := pools.NewPool(runtime.NumCPU())
pool := pools.NewPool(concurrent)
// generate query code for all struct
for _, info := range g.Data {
pool.Wait()
Expand Down Expand Up @@ -330,9 +332,13 @@ func (g *Generator) generateQueryFile() (err error) {
func (g *Generator) generateSingleQueryFile(data *genInfo) (err error) {
var buf bytes.Buffer

structPkgPath := data.StructInfo.PkgPath
if structPkgPath == "" {
structPkgPath = g.modelImportPath
}
err = render(tmpl.Header, &buf, map[string]string{
"Package": g.queryPkgName,
"StructPkgPath": data.StructInfo.PkgPath,
"StructPkgPath": structPkgPath,
})
if err != nil {
return err
Expand Down Expand Up @@ -401,7 +407,7 @@ func (g *Generator) generateModelFile() error {
}

errChan := make(chan error)
pool := pools.NewPool(runtime.NumCPU())
pool := pools.NewPool(concurrent)
for _, data := range g.modelData {
if data == nil || !data.GenBaseStruct {
continue
Expand Down
1 change: 1 addition & 0 deletions internal/template/base.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion internal/template/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ package template
const Model = NotEditMark + `
package {{.StructInfo.Package}}
import "time"
import (
"encoding/json"
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
)
const TableName{{.StructName}} = "{{.TableName}}"
Expand Down

0 comments on commit 9b24d1f

Please sign in to comment.