Skip to content

Commit

Permalink
fix: fix ens
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed May 11, 2024
1 parent fd027f1 commit b0708f5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
50 changes: 28 additions & 22 deletions codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion driver/mysql/def_atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
})
}
Expand Down
8 changes: 4 additions & 4 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion model_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
)
Expand Down

0 comments on commit b0708f5

Please sign in to comment.