From b0708f53f24816a6643abba01cb7fadb35060434 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Sat, 11 May 2024 13:50:26 +0800 Subject: [PATCH] fix: fix ens --- codegen.go | 50 ++++++++++++++++++++++----------------- driver/mysql/def_atlas.go | 2 +- field.go | 8 +++---- go_type.go | 4 ++-- model_parse.go | 2 +- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/codegen.go b/codegen.go index 1285c8b..8bb0f53 100644 --- a/codegen.go +++ b/codegen.go @@ -8,6 +8,7 @@ import ( "github.com/things-go/ens/matcher" "github.com/things-go/ens/utils" "golang.org/x/tools/imports" + "gorm.io/gorm" "gorm.io/plugin/soft_delete" ) @@ -114,7 +115,7 @@ func (g *CodeGen) genModelStructField(field *FieldDescriptor) string { b := strings.Builder{} b.Grow(256) ident := field.Type.Ident - if field.Optional && !field.Type.Nullable { + if field.GoPointer && !field.Type.NoPointer { ident = "*" + field.Type.Ident } // field @@ -176,49 +177,54 @@ func TransferEntityField(et *EntityDescriptor, opt *Option) *EntityDescriptor { } // 根规则转义一些数据 -func TransferField(f *FieldDescriptor, opt *Option) *FieldDescriptor { - field := *f - if field.ColumnName == "deleted_at" && field.Type.IsInteger() { - field.Optional = false - field.GoType(soft_delete.DeletedAt(0)) +func TransferField(oldField *FieldDescriptor, opt *Option) *FieldDescriptor { + newField := *oldField + if newField.ColumnName == "deleted_at" { + if newField.Type.IsInteger() { + newField.GoPointer = false + newField.GoType(soft_delete.DeletedAt(0)) + } else if newField.Type.IsInteger() { + newField.GoPointer = false + newField.GoType(gorm.DeletedAt{}) + } } if opt == nil { opt = defaultOption() } if opt.EnableInt { - switch field.Type.Type { + switch newField.Type.Type { case TypeInt8, TypeInt16, TypeInt32: - field.GoType(int(0)) + newField.GoType(int(0)) case TypeUint8, TypeUint16, TypeUint32: - field.GoType(uint(0)) + newField.GoType(uint(0)) } } - if opt.EnableBoolInt && field.Type.IsBool() { - field.GoType(int(0)) + if opt.EnableBoolInt && newField.Type.IsBool() { + newField.GoType(int(0)) } - if field.Nullable && opt.DisableNullToPoint { - gt, ok := sqlNullValueGoType[field.Type.Type] + if newField.Nullable && opt.DisableNullToPoint { + gt, ok := sqlNullValueGoType[newField.Type.Type] if ok { - field.Type = gt.Clone() - field.Optional = false + newField.Type = gt.Clone() + newField.GoPointer = false } } for tag, kind := range opt.Tags { if tag == "json" { - if vv := matcher.JsonTag(field.Comment); vv != "" { - field.Tags = append(field.Tags, fmt.Sprintf(`%s:"%s"`, tag, vv)) + if vv := matcher.JsonTag(newField.Comment); vv != "" { + newField.Tags = append(newField.Tags, fmt.Sprintf(`%s:"%s"`, tag, vv)) continue } } - vv := utils.StyleName(kind, field.ColumnName) + vv := utils.StyleName(kind, newField.ColumnName) if vv == "" { continue } - if tag == "json" && matcher.HasAffixJSONTag(field.Comment) { - field.Tags = append(field.Tags, fmt.Sprintf(`%s:"%s,omitempty,string"`, tag, vv)) + if tag == "json" && matcher.HasAffixJSONTag(newField.Comment) { + newField.Tags = append(newField.Tags, fmt.Sprintf(`%s:"%s,omitempty,string"`, tag, vv)) } else { - field.Tags = append(field.Tags, fmt.Sprintf(`%s:"%s,omitempty"`, tag, vv)) + newField.Tags = append(newField.Tags, fmt.Sprintf(`%s:"%s,omitempty"`, tag, vv)) } } - return &field + return &newField } diff --git a/driver/mysql/def_atlas.go b/driver/mysql/def_atlas.go index 1951794..5f8109a 100644 --- a/driver/mysql/def_atlas.go +++ b/driver/mysql/def_atlas.go @@ -274,7 +274,7 @@ func intoSchema(tb *schema.Table) *ens.EntityDescriptor { Column: NewColumnDef(col), Type: intoGoType(col.Type.Raw), GoName: utils.CamelCase(col.Name), - Optional: col.Type.Null, + GoPointer: col.Type.Null, Tags: []string{intoGormTag(tb, col)}, }) } diff --git a/field.go b/field.go index 4819e89..e67cd64 100644 --- a/field.go +++ b/field.go @@ -6,10 +6,10 @@ type FieldDescriptor struct { Nullable bool // Nullable reports whether the column may be null. Column ColumnDef // for go - Type *GoType // go type information. - GoName string // Go name - Optional bool // nullable struct field. - Tags []string // Tags struct tag + Type *GoType // go type information. + GoName string // Go name + GoPointer bool // go field is pointer. + Tags []string // Tags struct tag } func (field *FieldDescriptor) GoType(typ any) { diff --git a/go_type.go b/go_type.go index 959a5af..9f73f80 100644 --- a/go_type.go +++ b/go_type.go @@ -195,7 +195,7 @@ type GoType struct { Ident string // Type identifier, e.g. int, time.Time, sql.NullInt64. PkgPath string // import path. e.g. "", time, database/sql. PkgQualifier string // a package qualifier. e.g. "", time, sql. - Nullable bool // pointers or slices, means not need point. + NoPointer bool // pointers or slices, means not need pointer. } func NewGoType(t Type, v any) *GoType { @@ -209,7 +209,7 @@ func newGoType(t Type, tt reflect.Type) *GoType { Ident: tt.String(), PkgPath: tv.PkgPath(), PkgQualifier: utils.PkgQualifier(tv.String()), - Nullable: slices.Contains([]reflect.Kind{reflect.Slice, reflect.Ptr, reflect.Map}, tt.Kind()), + NoPointer: slices.Contains([]reflect.Kind{reflect.Slice, reflect.Ptr, reflect.Map}, tt.Kind()), } } diff --git a/model_parse.go b/model_parse.go index 065456f..ed75591 100644 --- a/model_parse.go +++ b/model_parse.go @@ -73,7 +73,7 @@ func structToFielders(vt reflect.Type) []*FieldDescriptor { Column: nil, Type: newGoType(t, fv.Type), GoName: fv.Name, - Optional: false, + GoPointer: false, Tags: []string{}, }, )