From 6df282a35ba2637f8a5e78555c97dcd28caa6818 Mon Sep 17 00:00:00 2001 From: qqxhb <30866940+qqxhb@users.noreply.github.com> Date: Fri, 7 Apr 2023 16:00:08 +0800 Subject: [PATCH] feat: field tag (#784) * feat: field tag * feat: rm new field * feat: test * feat: sort tag * feat: support comment tag and default tag for primary key * feat: comment tag test --- config.go | 6 - examples/cmd/from_object/prepare.go | 3 +- field/association.go | 22 +++- field/tag.go | 129 ++++++++++++++++++++++ field_options.go | 59 +++++----- generator.go | 1 - helper/object.go | 4 +- internal/generate/export.go | 27 +++-- internal/generate/generate.go | 6 +- internal/model/base.go | 24 ++-- internal/model/config.go | 1 - internal/model/tbl_column.go | 42 +++---- internal/template/model.go | 6 - tests/.expect/dal_1/model/users.gen.go | 13 +-- tests/.expect/dal_2/model/users.gen.go | 13 +-- tests/.expect/dal_3/model/users.gen.go | 13 +-- tests/.expect/dal_4/model/users.gen.go | 13 +-- tests/.expect/dal_test/model/users.gen.go | 9 +- 18 files changed, 246 insertions(+), 145 deletions(-) create mode 100644 field/tag.go diff --git a/config.go b/config.go index 78151324..f2161b8f 100644 --- a/config.go +++ b/config.go @@ -56,7 +56,6 @@ type Config struct { dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string) fieldJSONTagNS func(columnName string) (tagContent string) - fieldNewTagNS func(columnName string) (tagContent string) modelOpts []ModelOpt } @@ -104,11 +103,6 @@ func (cfg *Config) WithJSONTagNameStrategy(ns func(columnName string) (tagConten cfg.fieldJSONTagNS = ns } -// WithNewTagNameStrategy specify new tag naming strategy -func (cfg *Config) WithNewTagNameStrategy(ns func(columnName string) (tagContent string)) { - cfg.fieldNewTagNS = ns -} - // WithImportPkgPath specify import package path func (cfg *Config) WithImportPkgPath(paths ...string) { for i, path := range paths { diff --git a/examples/cmd/from_object/prepare.go b/examples/cmd/from_object/prepare.go index c347f2ba..30a56b49 100644 --- a/examples/cmd/from_object/prepare.go +++ b/examples/cmd/from_object/prepare.go @@ -3,6 +3,7 @@ package main import ( "strings" + "gorm.io/gen/field" "gorm.io/gen/helper" ) @@ -57,7 +58,7 @@ func (f *DemoField) GORMTag() string { return f.gormTag } func (f *DemoField) JSONTag() string { return f.jsonTag } // Tag return new tag -func (f *DemoField) Tag() string { return f.tag } +func (f *DemoField) Tag() field.Tag { return field.NewTag() } // Comment return comment func (f *DemoField) Comment() string { return f.comment } diff --git a/field/association.go b/field/association.go index 28089090..384d0491 100644 --- a/field/association.go +++ b/field/association.go @@ -219,9 +219,9 @@ type RelateConfig struct { RelateSlicePointer bool JSONTag string - GORMTag string - NewTag string - OverwriteTag string + GORMTag GormTag + Tag Tag + OverwriteTag Tag } // RelateFieldPrefix return generated relation field's type @@ -237,3 +237,19 @@ func (c *RelateConfig) RelateFieldPrefix(relationshipType RelationshipType) stri return defaultRelationshipPrefix[relationshipType] } } +func (c *RelateConfig) GetTag(fieldName string) Tag { + if c == nil { + return NewTag() + } + if c.OverwriteTag != nil { + return c.OverwriteTag + } + if c.Tag == nil { + c.Tag = NewTag() + } + if c.JSONTag == "" { + c.JSONTag = ns.ColumnName("", fieldName) + } + c.Tag.Set(TagKeyJson, c.JSONTag) + return c.Tag +} diff --git a/field/tag.go b/field/tag.go new file mode 100644 index 00000000..7330af07 --- /dev/null +++ b/field/tag.go @@ -0,0 +1,129 @@ +package field + +import ( + "sort" + "strings" +) + +const ( + TagKeyGorm = "gorm" + TagKeyJson = "json" + + //gorm tag + TagKeyGormColumn = "column" + TagKeyGormType = "type" + TagKeyGormPrimaryKey = "primaryKey" + TagKeyGormAutoIncrement = "autoIncrement" + TagKeyGormNotNull = "not null" + TagKeyGormUniqueIndex = "uniqueIndex" + TagKeyGormIndex = "index" + TagKeyGormDefault = "default" + TagKeyGormComment = "comment" +) + +var ( + tagKeyPriorities = map[string]int16{ + TagKeyGorm: 100, + TagKeyJson: 99, + + TagKeyGormColumn: 10, + TagKeyGormType: 9, + TagKeyGormPrimaryKey: 8, + TagKeyGormAutoIncrement: 7, + TagKeyGormNotNull: 6, + TagKeyGormUniqueIndex: 5, + TagKeyGormIndex: 4, + TagKeyGormDefault: 3, + TagKeyGormComment: 0, + } +) + +type TagBuilder interface { + Build() string +} + +type Tag map[string]string + +func NewTag() Tag { + return Tag{} +} + +func (tag Tag) Set(key, value string) { + tag[key] = value +} + +func (tag Tag) Remove(key string) { + delete(tag, key) +} + +func (tag Tag) Build() string { + if tag == nil || len(tag) == 0 { + return "" + } + + tags := make([]string, 0, len(tag)) + keys := tagKeySort(tag) + for _, k := range keys { + v := tag[k] + if k == "" || v == "" { + continue + } + tags = append(tags, k+":\""+v+"\"") + } + return strings.Join(tags, " ") +} + +type GormTag Tag + +func NewGormTag() GormTag { + return GormTag{} +} + +func (tag GormTag) Set(key, value string) { + tag[key] = value +} + +func (tag GormTag) Remove(key string) { + delete(tag, key) +} + +func (tag GormTag) Build() string { + if tag == nil || len(tag) == 0 { + return "" + } + tags := make([]string, 0, len(tag)) + keys := tagKeySort(Tag(tag)) + for _, k := range keys { + v := tag[k] + if k == "" && v == "" { + continue + } + tv := make([]string, 0, 2) + if k != "" { + tv = append(tv, k) + } + if v != "" { + tv = append(tv, v) + } + tags = append(tags, strings.Join(tv, ":")) + } + + return strings.Join(tags, ";") +} + +func tagKeySort(tag Tag) []string { + keys := make([]string, 0, len(tag)) + if len(tag) == 0 { + return keys + } + for k, _ := range tag { + keys = append(keys, k) + } + sort.Slice(keys, func(i, j int) bool { + if tagKeyPriorities[keys[i]] == tagKeyPriorities[keys[j]] { + return keys[i] <= keys[j] + } + return tagKeyPriorities[keys[i]] > tagKeyPriorities[keys[j]] + }) + return keys +} diff --git a/field_options.go b/field_options.go index d2134649..8db9566f 100644 --- a/field_options.go +++ b/field_options.go @@ -1,7 +1,6 @@ package gen import ( - "fmt" "reflect" "regexp" "strings" @@ -19,12 +18,12 @@ var ns = schema.NamingStrategy{} var ( // FieldNew add new field (any type your want) - FieldNew = func(fieldName, fieldType, fieldTag string) model.CreateFieldOpt { + FieldNew = func(fieldName, fieldType string, fieldTag field.Tag) model.CreateFieldOpt { return func(*model.Field) *model.Field { return &model.Field{ - Name: fieldName, - Type: fieldType, - OverwriteTag: fieldTag, + Name: fieldName, + Type: fieldType, + Tag: fieldTag, } } } @@ -112,10 +111,10 @@ var ( } } // FieldTag specify GORM tag and JSON tag - FieldTag = func(columnName string, gormTag, jsonTag string) model.ModifyFieldOpt { + FieldTag = func(columnName string, tagFunc func(tag field.Tag) field.Tag) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { - m.GORMTag, m.JSONTag = gormTag, jsonTag + m.Tag = tagFunc(m.Tag) } return m } @@ -124,7 +123,7 @@ var ( FieldJSONTag = func(columnName string, jsonTag string) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { - m.JSONTag = jsonTag + m.Tag.Set(field.TagKeyJson, jsonTag) } return m } @@ -133,25 +132,28 @@ var ( FieldJSONTagWithNS = func(schemaName func(columnName string) (tagContent string)) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if schemaName != nil { - m.JSONTag = schemaName(m.ColumnName) + m.Tag.Set(field.TagKeyJson, schemaName(m.ColumnName)) + } return m } } // FieldGORMTag specify GORM tag - FieldGORMTag = func(columnName string, gormTag string) model.ModifyFieldOpt { + FieldGORMTag = func(columnName string, gormTag func(tag field.GormTag) field.GormTag) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { - m.GORMTag = gormTag + m.GORMTag = gormTag(m.GORMTag) } return m } } // FieldNewTag add new tag - FieldNewTag = func(columnName string, newTag string) model.ModifyFieldOpt { + FieldNewTag = func(columnName string, newTag field.Tag) model.ModifyFieldOpt { return func(m *model.Field) *model.Field { if m.ColumnName == columnName { - m.NewTag += " " + newTag + for k, v := range newTag { + m.Tag.Set(k, v) + } } return m } @@ -162,7 +164,7 @@ var ( if schemaName == nil { schemaName = func(name string) string { return name } } - m.NewTag = fmt.Sprintf(`%s %s:"%s"`, m.NewTag, tagName, schemaName(m.ColumnName)) + m.Tag.Set(tagName, schemaName(m.ColumnName)) return m } } @@ -199,18 +201,13 @@ var ( if config == nil { config = &field.RelateConfig{} } - if config.JSONTag == "" { - config.JSONTag = ns.ColumnName("", fieldName) - } + return func(*model.Field) *model.Field { return &model.Field{ - Name: fieldName, - Type: config.RelateFieldPrefix(relationship) + table.StructInfo.Type, - JSONTag: config.JSONTag, - GORMTag: config.GORMTag, - NewTag: config.NewTag, - OverwriteTag: config.OverwriteTag, - + Name: fieldName, + Type: config.RelateFieldPrefix(relationship) + table.StructInfo.Type, + Tag: config.GetTag(fieldName), + GORMTag: config.GORMTag, Relation: field.NewRelationWithType( relationship, fieldName, table.StructInfo.Package+"."+table.StructInfo.Type, table.Relations()...), @@ -228,19 +225,13 @@ var ( if config == nil { config = &field.RelateConfig{} } - if config.JSONTag == "" { - config.JSONTag = ns.ColumnName("", fieldName) - } return func(*model.Field) *model.Field { return &model.Field{ - Name: fieldName, - Type: config.RelateFieldPrefix(relationship) + fieldType, - JSONTag: config.JSONTag, - GORMTag: config.GORMTag, - NewTag: config.NewTag, - OverwriteTag: config.OverwriteTag, - + Name: fieldName, + Type: config.RelateFieldPrefix(relationship) + fieldType, + GORMTag: config.GORMTag, + Tag: config.GetTag(fieldName), Relation: field.NewRelationWithModel(relationship, fieldName, fieldType, relModel), } } diff --git a/generator.go b/generator.go index 8c022773..352f71ec 100644 --- a/generator.go +++ b/generator.go @@ -186,7 +186,6 @@ func (g *Generator) genModelConfig(tableName string, modelName string, modelOpts FieldWithTypeTag: g.FieldWithTypeTag, FieldJSONTagNS: g.fieldJSONTagNS, - FieldNewTagNS: g.fieldNewTagNS, }, } } diff --git a/helper/object.go b/helper/object.go index 328f6834..9108d2d0 100644 --- a/helper/object.go +++ b/helper/object.go @@ -3,6 +3,8 @@ package helper import ( "errors" "fmt" + + "gorm.io/gen/field" ) // Object an object interface @@ -34,7 +36,7 @@ type Field interface { // JSONTag return json tag JSONTag() string // Tag return field tag - Tag() string + Tag() field.Tag // Comment return comment Comment() string diff --git a/internal/generate/export.go b/internal/generate/export.go index 6d2aca16..3ee62099 100644 --- a/internal/generate/export.go +++ b/internal/generate/export.go @@ -84,16 +84,25 @@ func GetQueryStructMetaFromObject(obj helper.Object, conf *model.Config) (*Query } fields := make([]*model.Field, 0, 16) - for _, field := range obj.Fields() { + for _, fl := range obj.Fields() { + tag := fl.Tag() + if tag == nil { + tag = field.NewTag() + } + if gt := fl.GORMTag(); gt != "" { + tag.Set(field.TagKeyGorm, gt) + } + if jt := fl.GORMTag(); jt != "" { + tag.Set(field.TagKeyJson, jt) + } + fields = append(fields, &model.Field{ - Name: field.Name(), - Type: field.Type(), - ColumnName: field.ColumnName(), - GORMTag: field.GORMTag(), - JSONTag: field.JSONTag(), - NewTag: field.Tag(), - ColumnComment: field.Comment(), - MultilineComment: strings.Contains(field.Comment(), "\n"), + Name: fl.Name(), + Type: fl.Type(), + ColumnName: fl.ColumnName(), + Tag: tag, + ColumnComment: fl.Comment(), + MultilineComment: strings.Contains(fl.Comment(), "\n"), }) } diff --git a/internal/generate/generate.go b/internal/generate/generate.go index 7fb3d780..b2b23df3 100644 --- a/internal/generate/generate.go +++ b/internal/generate/generate.go @@ -19,15 +19,15 @@ import ( func getFields(db *gorm.DB, conf *model.Config, columns []*model.Column) (fields []*model.Field) { for _, col := range columns { col.SetDataTypeMap(conf.DataTypeMap) - col.WithNS(conf.FieldJSONTagNS, conf.FieldNewTagNS) + col.WithNS(conf.FieldJSONTagNS) m := col.ToField(conf.FieldNullable, conf.FieldCoverable, conf.FieldSignable) if filterField(m, conf.FilterOpts) == nil { continue } - if t, ok := col.ColumnType.ColumnType(); ok && !conf.FieldWithTypeTag { // remove type tag if FieldWithTypeTag == false - m.GORMTag = strings.ReplaceAll(m.GORMTag, ";type:"+t, "") + if _, ok := col.ColumnType.ColumnType(); ok && !conf.FieldWithTypeTag { // remove type tag if FieldWithTypeTag == false + m.GORMTag.Remove("type") } m = modifyField(m, conf.ModifyOpts) diff --git a/internal/model/base.go b/internal/model/base.go index 5daeec72..e5cdcfd3 100644 --- a/internal/model/base.go +++ b/internal/model/base.go @@ -2,7 +2,6 @@ package model import ( "bytes" - "fmt" "strings" "gorm.io/gen/field" @@ -170,31 +169,22 @@ type Field struct { ColumnName string ColumnComment string MultilineComment bool - JSONTag string - GORMTag string - NewTag string - OverwriteTag string + Tag field.Tag + GORMTag field.GormTag CustomGenType string Relation *field.Relation } // Tags ... func (m *Field) Tags() string { - if m.OverwriteTag != "" { - return strings.TrimSpace(m.OverwriteTag) + if _, ok := m.Tag[field.TagKeyGorm]; ok { + return m.Tag.Build() } - var tags strings.Builder - if gormTag := strings.TrimSpace(m.GORMTag); gormTag != "" { - tags.WriteString(fmt.Sprintf(`gorm:"%s" `, gormTag)) + if gormTag := strings.TrimSpace(m.GORMTag.Build()); gormTag != "" { + m.Tag.Set(field.TagKeyGorm, gormTag) } - if jsonTag := strings.TrimSpace(m.JSONTag); jsonTag != "" { - tags.WriteString(fmt.Sprintf(`json:"%s" `, jsonTag)) - } - if newTag := strings.TrimSpace(m.NewTag); newTag != "" { - tags.WriteString(newTag) - } - return strings.TrimSpace(tags.String()) + return m.Tag.Build() } // IsRelation ... diff --git a/internal/model/config.go b/internal/model/config.go index 65bcf3d6..c1ce6aa8 100644 --- a/internal/model/config.go +++ b/internal/model/config.go @@ -42,7 +42,6 @@ type FieldConfig struct { FieldWithTypeTag bool // generate with gorm column type tag FieldJSONTagNS func(columnName string) string - FieldNewTagNS func(columnName string) string ModifyOpts []FieldOption FilterOpts []FieldOption diff --git a/internal/model/tbl_column.go b/internal/model/tbl_column.go index a13a3ada..cee590d3 100644 --- a/internal/model/tbl_column.go +++ b/internal/model/tbl_column.go @@ -1,11 +1,11 @@ package model import ( - "bytes" "fmt" "reflect" "strings" + "gorm.io/gen/field" "gorm.io/gorm" ) @@ -17,7 +17,6 @@ type Column struct { UseScanType bool `gorm:"-"` dataTypeMap map[string]func(columnType gorm.ColumnType) (dataType string) `gorm:"-"` jsonTagNS func(columnName string) string `gorm:"-"` - newTagNS func(columnName string) string `gorm:"-"` } // SetDataTypeMap set data type map @@ -37,14 +36,11 @@ func (c *Column) GetDataType() (fieldtype string) { } // WithNS with name strategy -func (c *Column) WithNS(jsonTagNS, newTagNS func(columnName string) string) { - c.jsonTagNS, c.newTagNS = jsonTagNS, newTagNS +func (c *Column) WithNS(jsonTagNS func(columnName string) string) { + c.jsonTagNS = jsonTagNS if c.jsonTagNS == nil { c.jsonTagNS = func(n string) string { return n } } - if c.newTagNS == nil { - c.newTagNS = func(string) string { return "" } - } } // ToField convert to field @@ -75,8 +71,7 @@ func (c *Column) ToField(nullable, coverable, signable bool) *Field { ColumnName: c.Name(), MultilineComment: c.multilineComment(), GORMTag: c.buildGormTag(), - JSONTag: c.jsonTagNS(c.Name()), - NewTag: c.newTagNS(c.Name()), + Tag: map[string]string{field.TagKeyJson: c.jsonTagNS(c.Name())}, ColumnComment: comment, } } @@ -86,19 +81,20 @@ func (c *Column) multilineComment() bool { return ok && strings.Contains(cm, "\n") } -func (c *Column) buildGormTag() string { - var buf bytes.Buffer - buf.WriteString(fmt.Sprintf("column:%s;type:%s", c.Name(), c.columnType())) +func (c *Column) buildGormTag() field.GormTag { + tag := field.NewGormTag() + tag.Set(field.TagKeyGormColumn, c.Name()) + tag.Set(field.TagKeyGormType, c.columnType()) isPriKey, ok := c.PrimaryKey() isValidPriKey := ok && isPriKey if isValidPriKey { - buf.WriteString(";primaryKey") + tag.Set(field.TagKeyGormPrimaryKey, "") if at, ok := c.AutoIncrement(); ok { - buf.WriteString(fmt.Sprintf(";autoIncrement:%t", at)) + tag.Set(field.TagKeyGormAutoIncrement, fmt.Sprintf("%t", at)) } } else if n, ok := c.Nullable(); ok && !n { - buf.WriteString(";not null") + tag.Set(field.TagKeyGormNotNull, "") } for _, idx := range c.Indexes { @@ -109,16 +105,22 @@ func (c *Column) buildGormTag() string { continue } if uniq, _ := idx.Unique(); uniq { - buf.WriteString(fmt.Sprintf(";uniqueIndex:%s,priority:%d", idx.Name(), idx.Priority)) + tag.Set(field.TagKeyGormUniqueIndex, fmt.Sprintf("%s,priority:%d", idx.Name(), idx.Priority)) } else { - buf.WriteString(fmt.Sprintf(";index:%s,priority:%d", idx.Name(), idx.Priority)) + tag.Set(field.TagKeyGormIndex, fmt.Sprintf("%s,priority:%d", idx.Name(), idx.Priority)) } } - if dtValue := c.defaultTagValue(); !isValidPriKey && c.needDefaultTag(dtValue) { // cannot set default tag for primary key - buf.WriteString(fmt.Sprintf(`;default:%s`, dtValue)) + if dtValue := c.defaultTagValue(); c.needDefaultTag(dtValue) { // cannot set default tag for primary key + tag.Set(field.TagKeyGormDefault, dtValue) + } + if comment, ok := c.Comment(); ok && comment != "" { + if c.multilineComment() { + comment = strings.ReplaceAll(comment, "\n", "\\n") + } + tag.Set(field.TagKeyGormComment, comment) } - return buf.String() + return tag } // needDefaultTag check if default tag needed diff --git a/internal/template/model.go b/internal/template/model.go index 21e9ae0b..133c7a8b 100644 --- a/internal/template/model.go +++ b/internal/template/model.go @@ -19,13 +19,7 @@ import ( // {{.ModelStructName}} {{.StructComment}} type {{.ModelStructName}} struct { {{range .Fields}} - {{if .MultilineComment -}} - /* -{{.ColumnComment}} - */ - {{end -}} {{.Name}} {{.Type}} ` + "`{{.Tags}}` " + - "{{if not .MultilineComment}}{{if .ColumnComment}}// {{.ColumnComment}}{{end}}{{end}}" + `{{end}} } diff --git a/tests/.expect/dal_1/model/users.gen.go b/tests/.expect/dal_1/model/users.gen.go index 1db026b2..dc034144 100644 --- a/tests/.expect/dal_1/model/users.gen.go +++ b/tests/.expect/dal_1/model/users.gen.go @@ -14,17 +14,12 @@ const TableNameUser = "users" type User struct { ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - Name string `gorm:"column:name" json:"name"` // oneline + Name string `gorm:"column:name;comment:oneline" json:"name"` Address string `gorm:"column:address" json:"address"` RegisterTime time.Time `gorm:"column:register_time" json:"register_time"` - /* - multiline - line1 - line2 - */ - Alive bool `gorm:"column:alive" json:"alive"` - CompanyID int64 `gorm:"column:company_id;default:666" json:"company_id"` - PrivateURL string `gorm:"column:private_url;default:https://a.b.c" json:"private_url"` + Alive bool `gorm:"column:alive;comment:multiline\nline1\nline2" json:"alive"` + CompanyID int64 `gorm:"column:company_id;default:666" json:"company_id"` + PrivateURL string `gorm:"column:private_url;default:https://a.b.c" json:"private_url"` } // TableName User's table name diff --git a/tests/.expect/dal_2/model/users.gen.go b/tests/.expect/dal_2/model/users.gen.go index b9df6533..0affe0c5 100644 --- a/tests/.expect/dal_2/model/users.gen.go +++ b/tests/.expect/dal_2/model/users.gen.go @@ -14,17 +14,12 @@ const TableNameUser = "users" type User struct { ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"-"` CreatedAt *time.Time `gorm:"column:created_at" json:"-"` - Name *string `gorm:"column:name;index:idx_name,priority:1" json:"-"` // oneline + Name *string `gorm:"column:name;index:idx_name,priority:1;comment:oneline" json:"-"` Address *string `gorm:"column:address" json:"-"` RegisterTime *time.Time `gorm:"column:register_time" json:"-"` - /* - multiline - line1 - line2 - */ - Alive *bool `gorm:"column:alive" json:"-"` - CompanyID *int64 `gorm:"column:company_id;default:666" json:"-"` - PrivateURL *string `gorm:"column:private_url;default:https://a.b.c" json:"-"` + Alive *bool `gorm:"column:alive;comment:multiline\nline1\nline2" json:"-"` + CompanyID *int64 `gorm:"column:company_id;default:666" json:"-"` + PrivateURL *string `gorm:"column:private_url;default:https://a.b.c" json:"-"` } // TableName User's table name diff --git a/tests/.expect/dal_3/model/users.gen.go b/tests/.expect/dal_3/model/users.gen.go index b9df6533..0affe0c5 100644 --- a/tests/.expect/dal_3/model/users.gen.go +++ b/tests/.expect/dal_3/model/users.gen.go @@ -14,17 +14,12 @@ const TableNameUser = "users" type User struct { ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"-"` CreatedAt *time.Time `gorm:"column:created_at" json:"-"` - Name *string `gorm:"column:name;index:idx_name,priority:1" json:"-"` // oneline + Name *string `gorm:"column:name;index:idx_name,priority:1;comment:oneline" json:"-"` Address *string `gorm:"column:address" json:"-"` RegisterTime *time.Time `gorm:"column:register_time" json:"-"` - /* - multiline - line1 - line2 - */ - Alive *bool `gorm:"column:alive" json:"-"` - CompanyID *int64 `gorm:"column:company_id;default:666" json:"-"` - PrivateURL *string `gorm:"column:private_url;default:https://a.b.c" json:"-"` + Alive *bool `gorm:"column:alive;comment:multiline\nline1\nline2" json:"-"` + CompanyID *int64 `gorm:"column:company_id;default:666" json:"-"` + PrivateURL *string `gorm:"column:private_url;default:https://a.b.c" json:"-"` } // TableName User's table name diff --git a/tests/.expect/dal_4/model/users.gen.go b/tests/.expect/dal_4/model/users.gen.go index b9df6533..0affe0c5 100644 --- a/tests/.expect/dal_4/model/users.gen.go +++ b/tests/.expect/dal_4/model/users.gen.go @@ -14,17 +14,12 @@ const TableNameUser = "users" type User struct { ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"-"` CreatedAt *time.Time `gorm:"column:created_at" json:"-"` - Name *string `gorm:"column:name;index:idx_name,priority:1" json:"-"` // oneline + Name *string `gorm:"column:name;index:idx_name,priority:1;comment:oneline" json:"-"` Address *string `gorm:"column:address" json:"-"` RegisterTime *time.Time `gorm:"column:register_time" json:"-"` - /* - multiline - line1 - line2 - */ - Alive *bool `gorm:"column:alive" json:"-"` - CompanyID *int64 `gorm:"column:company_id;default:666" json:"-"` - PrivateURL *string `gorm:"column:private_url;default:https://a.b.c" json:"-"` + Alive *bool `gorm:"column:alive;comment:multiline\nline1\nline2" json:"-"` + CompanyID *int64 `gorm:"column:company_id;default:666" json:"-"` + PrivateURL *string `gorm:"column:private_url;default:https://a.b.c" json:"-"` } // TableName User's table name diff --git a/tests/.expect/dal_test/model/users.gen.go b/tests/.expect/dal_test/model/users.gen.go index 1db026b2..1de375ad 100644 --- a/tests/.expect/dal_test/model/users.gen.go +++ b/tests/.expect/dal_test/model/users.gen.go @@ -14,15 +14,10 @@ const TableNameUser = "users" type User struct { ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` - Name string `gorm:"column:name" json:"name"` // oneline + Name string `gorm:"column:name;comment:oneline" json:"name"` Address string `gorm:"column:address" json:"address"` RegisterTime time.Time `gorm:"column:register_time" json:"register_time"` - /* - multiline - line1 - line2 - */ - Alive bool `gorm:"column:alive" json:"alive"` + Alive bool `gorm:"column:alive;comment:multiline\nline1\nline2" json:"alive"` CompanyID int64 `gorm:"column:company_id;default:666" json:"company_id"` PrivateURL string `gorm:"column:private_url;default:https://a.b.c" json:"private_url"` }