Refactor model struct
This commit is contained in:
parent
5c478b46e1
commit
9e5c64d611
187
model_struct.go
187
model_struct.go
@ -156,7 +156,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set fields
|
// Get all fields
|
||||||
|
fields := []*StructField{}
|
||||||
for i := 0; i < scopeType.NumField(); i++ {
|
for i := 0; i < scopeType.NumField(); i++ {
|
||||||
if fieldStruct := scopeType.Field(i); ast.IsExported(fieldStruct.Name) {
|
if fieldStruct := scopeType.Field(i); ast.IsExported(fieldStruct.Name) {
|
||||||
field := &StructField{
|
field := &StructField{
|
||||||
@ -185,20 +186,29 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
} else {
|
} else {
|
||||||
field.DBName = ToDBName(fieldStruct.Name)
|
field.DBName = ToDBName(fieldStruct.Name)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
fields = append(fields, field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
|
for _, field := range fields {
|
||||||
if indirectType.Kind() == reflect.Ptr {
|
if !field.IsIgnored {
|
||||||
indirectType = indirectType.Elem()
|
fieldStruct := field.Struct
|
||||||
}
|
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
|
||||||
|
if indirectType.Kind() == reflect.Ptr {
|
||||||
|
indirectType = indirectType.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
if _, isScanner := reflect.New(fieldType).Interface().(sql.Scanner); isScanner {
|
if _, isScanner := reflect.New(fieldType).Interface().(sql.Scanner); isScanner {
|
||||||
field.IsScanner, field.IsNormal = true, true
|
field.IsScanner, field.IsNormal = true, true
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, isTime := reflect.New(indirectType).Interface().(*time.Time); isTime {
|
if _, isTime := reflect.New(indirectType).Interface().(*time.Time); isTime {
|
||||||
field.IsNormal = true
|
field.IsNormal = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !field.IsNormal {
|
||||||
|
gormSettings := parseTagSetting(field.Tag.Get("gorm"))
|
||||||
many2many := gormSettings["MANY2MANY"]
|
many2many := gormSettings["MANY2MANY"]
|
||||||
foreignKey := gormSettings["FOREIGNKEY"]
|
foreignKey := gormSettings["FOREIGNKEY"]
|
||||||
foreignType := gormSettings["FOREIGNTYPE"]
|
foreignType := gormSettings["FOREIGNTYPE"]
|
||||||
@ -208,98 +218,93 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
foreignType = polymorphic + "Type"
|
foreignType = polymorphic + "Type"
|
||||||
}
|
}
|
||||||
|
|
||||||
if !field.IsNormal {
|
switch indirectType.Kind() {
|
||||||
switch indirectType.Kind() {
|
case reflect.Slice:
|
||||||
case reflect.Slice:
|
typ := indirectType.Elem()
|
||||||
typ := indirectType.Elem()
|
if typ.Kind() == reflect.Ptr {
|
||||||
if typ.Kind() == reflect.Ptr {
|
typ = typ.Elem()
|
||||||
typ = typ.Elem()
|
}
|
||||||
|
|
||||||
|
if typ.Kind() == reflect.Struct {
|
||||||
|
kind := "has_many"
|
||||||
|
|
||||||
|
if foreignKey == "" {
|
||||||
|
foreignKey = scopeType.Name() + "Id"
|
||||||
}
|
}
|
||||||
|
|
||||||
if typ.Kind() == reflect.Struct {
|
if associationForeignKey == "" {
|
||||||
kind := "has_many"
|
associationForeignKey = typ.Name() + "Id"
|
||||||
|
|
||||||
if foreignKey == "" {
|
|
||||||
foreignKey = scopeType.Name() + "Id"
|
|
||||||
}
|
|
||||||
|
|
||||||
if associationForeignKey == "" {
|
|
||||||
associationForeignKey = typ.Name() + "Id"
|
|
||||||
}
|
|
||||||
|
|
||||||
if many2many != "" {
|
|
||||||
kind = "many_to_many"
|
|
||||||
} else if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
|
||||||
foreignKey = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
field.Relationship = &Relationship{
|
|
||||||
JoinTable: many2many,
|
|
||||||
ForeignType: foreignType,
|
|
||||||
ForeignFieldName: foreignKey,
|
|
||||||
AssociationForeignFieldName: associationForeignKey,
|
|
||||||
ForeignDBName: ToDBName(foreignKey),
|
|
||||||
AssociationForeignDBName: ToDBName(associationForeignKey),
|
|
||||||
Kind: kind,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
field.IsNormal = true
|
|
||||||
}
|
|
||||||
case reflect.Struct:
|
|
||||||
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
|
||||||
for _, field := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
|
|
||||||
field = field.clone()
|
|
||||||
field.Names = append([]string{fieldStruct.Name}, field.Names...)
|
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
|
||||||
}
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
var belongsToForeignKey, hasOneForeignKey, kind string
|
|
||||||
|
|
||||||
if foreignKey == "" {
|
|
||||||
belongsToForeignKey = field.Name + "Id"
|
|
||||||
hasOneForeignKey = scopeType.Name() + "Id"
|
|
||||||
} else {
|
|
||||||
belongsToForeignKey = foreignKey
|
|
||||||
hasOneForeignKey = foreignKey
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := scopeType.FieldByName(belongsToForeignKey); ok {
|
|
||||||
kind = "belongs_to"
|
|
||||||
foreignKey = belongsToForeignKey
|
|
||||||
} else {
|
|
||||||
foreignKey = hasOneForeignKey
|
|
||||||
kind = "has_one"
|
|
||||||
}
|
|
||||||
|
|
||||||
field.Relationship = &Relationship{
|
|
||||||
ForeignFieldName: foreignKey,
|
|
||||||
ForeignDBName: ToDBName(foreignKey),
|
|
||||||
ForeignType: foreignType,
|
|
||||||
Kind: kind,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
if many2many != "" {
|
||||||
|
kind = "many_to_many"
|
||||||
|
} else if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
||||||
|
foreignKey = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
field.Relationship = &Relationship{
|
||||||
|
JoinTable: many2many,
|
||||||
|
ForeignType: foreignType,
|
||||||
|
ForeignFieldName: foreignKey,
|
||||||
|
AssociationForeignFieldName: associationForeignKey,
|
||||||
|
ForeignDBName: ToDBName(foreignKey),
|
||||||
|
AssociationForeignDBName: ToDBName(associationForeignKey),
|
||||||
|
Kind: kind,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
field.IsNormal = true
|
field.IsNormal = true
|
||||||
}
|
}
|
||||||
|
case reflect.Struct:
|
||||||
|
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
||||||
|
for _, f := range scope.New(reflect.New(indirectType).Interface()).GetStructFields() {
|
||||||
|
f = f.clone()
|
||||||
|
f.Names = append([]string{fieldStruct.Name}, f.Names...)
|
||||||
|
modelStruct.StructFields = append(modelStruct.StructFields, f)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
var belongsToForeignKey, hasOneForeignKey, kind string
|
||||||
|
|
||||||
|
if foreignKey == "" {
|
||||||
|
belongsToForeignKey = field.Name + "Id"
|
||||||
|
hasOneForeignKey = scopeType.Name() + "Id"
|
||||||
|
} else {
|
||||||
|
belongsToForeignKey = foreignKey
|
||||||
|
hasOneForeignKey = foreignKey
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := scopeType.FieldByName(belongsToForeignKey); ok {
|
||||||
|
kind = "belongs_to"
|
||||||
|
foreignKey = belongsToForeignKey
|
||||||
|
} else {
|
||||||
|
foreignKey = hasOneForeignKey
|
||||||
|
kind = "has_one"
|
||||||
|
}
|
||||||
|
|
||||||
|
field.Relationship = &Relationship{
|
||||||
|
ForeignFieldName: foreignKey,
|
||||||
|
ForeignDBName: ToDBName(foreignKey),
|
||||||
|
ForeignType: foreignType,
|
||||||
|
Kind: kind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
field.IsNormal = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, field := range modelStruct.StructFields {
|
if field.IsNormal {
|
||||||
if field.IsNormal {
|
if modelStruct.PrimaryKeyField == nil && field.DBName == "id" {
|
||||||
if modelStruct.PrimaryKeyField == nil && field.DBName == "id" {
|
field.IsPrimaryKey = true
|
||||||
field.IsPrimaryKey = true
|
modelStruct.PrimaryKeyField = field
|
||||||
modelStruct.PrimaryKeyField = field
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if scope.db != nil {
|
if scope.db != nil {
|
||||||
scope.generateSqlTag(field)
|
scope.generateSqlTag(field)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||||
}
|
}
|
||||||
|
|
||||||
if scope.db != nil {
|
if scope.db != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user