diff --git a/readme.md b/readme.md index 016889bb121d..5f6c211cd389 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,8 @@ ## change +### 修改 +支持自定义time类型 JsonTime +如果time类型字段有默认值,生成gorm字段时标识为只读字段 ### 支持`gorm`风格的`model`生成 - ddl diff --git a/tools/goctl/model/sql/gen/field.go b/tools/goctl/model/sql/gen/field.go index 70a10ca01911..89215d462935 100644 --- a/tools/goctl/model/sql/gen/field.go +++ b/tools/goctl/model/sql/gen/field.go @@ -53,7 +53,7 @@ func genField(table Table, field *parser.Field) (string, error) { } func genGormField(table Table, field *parser.Field) (string, error) { - tag, err := genGormTag(table, field.NameOriginal) + tag, err := genGormTag(table, field) if err != nil { return "", err } @@ -87,7 +87,7 @@ func genGormFields(table Table, fields []*parser.Field) (string, error) { if err != nil { return "", err } - + result = strings.ReplaceAll(result, "time.Time", "JsonTime") list = append(list, result) } diff --git a/tools/goctl/model/sql/gen/gen_gorm.go b/tools/goctl/model/sql/gen/gen_gorm.go index ddedf0833261..8620bbd280d1 100644 --- a/tools/goctl/model/sql/gen/gen_gorm.go +++ b/tools/goctl/model/sql/gen/gen_gorm.go @@ -3,6 +3,9 @@ package gen import ( "bytes" "fmt" + "os" + "path/filepath" + "github.com/zeromicro/go-zero/tools/goctl/config" "github.com/zeromicro/go-zero/tools/goctl/model/sql/model" "github.com/zeromicro/go-zero/tools/goctl/model/sql/parser" @@ -11,8 +14,6 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/util/format" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" "github.com/zeromicro/go-zero/tools/goctl/util/stringx" - "os" - "path/filepath" ) type GormGenerator struct { @@ -136,7 +137,7 @@ func (g *GormGenerator) createFile(modelList map[string]*codeTuple) error { name := util.SafeString(modelFilename) + "_gen.go" filename := filepath.Join(dirAbs, name) - err = os.WriteFile(filename, []byte(codes.modelCode), os.ModePerm) + err = os.WriteFile(filename, []byte(codes.modelCode), 0644) if err != nil { return err } @@ -147,7 +148,7 @@ func (g *GormGenerator) createFile(modelList map[string]*codeTuple) error { g.Warning("%s already exists, ignored.", name) continue } - err = os.WriteFile(filename, []byte(codes.modelCustomCode), os.ModePerm) + err = os.WriteFile(filename, []byte(codes.modelCustomCode), 0644) if err != nil { return err } diff --git a/tools/goctl/model/sql/gen/imports.go b/tools/goctl/model/sql/gen/imports.go index b1f8910c33cc..4d91377d7a97 100644 --- a/tools/goctl/model/sql/gen/imports.go +++ b/tools/goctl/model/sql/gen/imports.go @@ -49,7 +49,7 @@ func gormGenImports(timeImport bool) (string, error) { } buffer, err := util.With("import").Parse(text).Execute(map[string]any{ - "time": timeImport, + "time": false, //use JsonTime not need import time.Time more details see gorm-err.tpl }) if err != nil { return "", err diff --git a/tools/goctl/model/sql/gen/tag.go b/tools/goctl/model/sql/gen/tag.go index b0dda4bf704c..0db91ff7ef77 100644 --- a/tools/goctl/model/sql/gen/tag.go +++ b/tools/goctl/model/sql/gen/tag.go @@ -1,6 +1,7 @@ package gen import ( + "github.com/zeromicro/go-zero/tools/goctl/model/sql/parser" "github.com/zeromicro/go-zero/tools/goctl/model/sql/template" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" @@ -27,7 +28,8 @@ func genTag(table Table, in string) (string, error) { return output.String(), nil } -func genGormTag(table Table, in string) (string, error) { +func genGormTag(table Table, field *parser.Field) (string, error) { + in := field.NameOriginal if in == "" { return in, nil } @@ -37,8 +39,9 @@ func genGormTag(table Table, in string) (string, error) { } output, err := util.With("tag").Parse(text).Execute(map[string]any{ - "field": in, - "data": table, + "field": in, + "isTimeAndHasDefaultValue": field.IsTime && field.HasDefaultValue, + "data": table, }) if err != nil { return "", err diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go index e25ca567eb46..466f83413045 100644 --- a/tools/goctl/model/sql/parser/parser.go +++ b/tools/goctl/model/sql/parser/parser.go @@ -45,6 +45,8 @@ type ( SeqInIndex int OrdinalPosition int ContainsPQ bool + HasDefaultValue bool + IsTime bool } // KeyType types alias of int @@ -246,7 +248,8 @@ func convertColumns(columns []*parser.Column, primaryColumn string, strict bool, field.Name = stringx.From(column.Name) field.DataType = dataType field.Comment = util.TrimNewLine(comment) - + field.HasDefaultValue = column.Constraint.HasDefaultValue + field.IsTime = field.DataType == timeImport if field.Name.Source() == primaryColumn { primaryKey = Primary{ Field: field, @@ -380,6 +383,8 @@ func getTableFields(table *model.Table, strict bool, args ...bool) (map[string]* SeqInIndex: columnSeqInIndex, OrdinalPosition: each.OrdinalPosition, ContainsPQ: containsPQ, + IsTime: dt == timeImport, + HasDefaultValue: each.ColumnDefault != nil, } fieldM[each.Name] = field } diff --git a/tools/goctl/model/sql/template/tpl/gorm-err.tpl b/tools/goctl/model/sql/template/tpl/gorm-err.tpl index 354c3b14a100..3230b65f73c8 100644 --- a/tools/goctl/model/sql/template/tpl/gorm-err.tpl +++ b/tools/goctl/model/sql/template/tpl/gorm-err.tpl @@ -2,6 +2,9 @@ package {{.pkg}} import ( "errors" + "fmt" + "time" + "gorm.io/gorm" ) @@ -10,4 +13,11 @@ func IgnoreRecordNotFound(err error) error { err = nil } return err -} \ No newline at end of file +} + +type JsonTime time.Time + +func (t JsonTime) MarshalJSON() ([]byte, error) { + var stamp = fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02 15:04:05")) + return []byte(stamp), nil +} diff --git a/tools/goctl/model/sql/template/tpl/gorm-tag.tpl b/tools/goctl/model/sql/template/tpl/gorm-tag.tpl index 31701e255865..5ab2148df7fe 100644 --- a/tools/goctl/model/sql/template/tpl/gorm-tag.tpl +++ b/tools/goctl/model/sql/template/tpl/gorm-tag.tpl @@ -1 +1 @@ -`gorm:"column:{{.field}}"` \ No newline at end of file +{{if .isTimeAndHasDefaultValue}}`gorm:"column:{{.field}};->" json:"{{.field}}"`{{else}}`gorm:"column:{{.field}}" json:"{{.field}}"`{{end}} \ No newline at end of file