fixing
This commit is contained in:
parent
7e2b6eba61
commit
b803039c31
270
model_struct.go
270
model_struct.go
@ -60,7 +60,7 @@ type Relationship struct {
|
|||||||
ForeignDBName string
|
ForeignDBName string
|
||||||
AssociationForeignFieldName string
|
AssociationForeignFieldName string
|
||||||
AssociationForeignDBName string
|
AssociationForeignDBName string
|
||||||
JoinTable string
|
JoinTableHandler JoinTableHandlerInterface
|
||||||
AssociationFieldName string
|
AssociationFieldName string
|
||||||
PivotKey string
|
PivotKey string
|
||||||
PivotType string
|
PivotType string
|
||||||
@ -149,146 +149,152 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, field := range fields {
|
defer func() {
|
||||||
if !field.IsIgnored {
|
for _, field := range fields {
|
||||||
fieldStruct := field.Struct
|
if !field.IsIgnored {
|
||||||
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
|
fieldStruct := field.Struct
|
||||||
if indirectType.Kind() == reflect.Ptr {
|
fieldType, indirectType := fieldStruct.Type, fieldStruct.Type
|
||||||
indirectType = indirectType.Elem()
|
if indirectType.Kind() == reflect.Ptr {
|
||||||
}
|
indirectType = indirectType.Elem()
|
||||||
|
|
||||||
if _, isScanner := reflect.New(fieldType).Interface().(sql.Scanner); isScanner {
|
|
||||||
field.IsScanner, field.IsNormal = true, true
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, isTime := reflect.New(indirectType).Interface().(*time.Time); isTime {
|
|
||||||
field.IsNormal = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if !field.IsNormal {
|
|
||||||
gormSettings := parseTagSetting(field.Tag.Get("gorm"))
|
|
||||||
toScope := scope.New(reflect.New(fieldStruct.Type).Interface())
|
|
||||||
|
|
||||||
getForeignField := func(column string, fields []*StructField) *StructField {
|
|
||||||
for _, field := range fields {
|
|
||||||
if field.Name == column || field.DBName == ToDBName(column) {
|
|
||||||
return field
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var relationship = &Relationship{}
|
if _, isScanner := reflect.New(fieldType).Interface().(sql.Scanner); isScanner {
|
||||||
|
field.IsScanner, field.IsNormal = true, true
|
||||||
foreignKey := gormSettings["FOREIGNKEY"]
|
|
||||||
if polymorphic := gormSettings["POLYMORPHIC"]; polymorphic != "" {
|
|
||||||
if polymorphicField := getForeignField(polymorphic+"Id", toScope.GetStructFields()); polymorphicField != nil {
|
|
||||||
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
|
|
||||||
relationship.ForeignFieldName = polymorphicField.Name
|
|
||||||
relationship.ForeignDBName = polymorphicField.DBName
|
|
||||||
relationship.PolymorphicType = polymorphicType.Name
|
|
||||||
relationship.PolymorphicDBName = polymorphicType.DBName
|
|
||||||
polymorphicType.IsForeignKey = true
|
|
||||||
polymorphicField.IsForeignKey = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch indirectType.Kind() {
|
if _, isTime := reflect.New(indirectType).Interface().(*time.Time); isTime {
|
||||||
case reflect.Slice:
|
|
||||||
elemType := indirectType.Elem()
|
|
||||||
if elemType.Kind() == reflect.Ptr {
|
|
||||||
elemType = elemType.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
if elemType.Kind() == reflect.Struct {
|
|
||||||
if foreignKey == "" {
|
|
||||||
foreignKey = scopeType.Name() + "Id"
|
|
||||||
}
|
|
||||||
|
|
||||||
if many2many := gormSettings["MANY2MANY"]; many2many != "" {
|
|
||||||
relationship.Kind = "many_to_many"
|
|
||||||
relationship.JoinTable = many2many
|
|
||||||
relationship.PivotKey = gormSettings["MANY2MANY"]
|
|
||||||
relationship.PivotType = elemType.Name() + "Id"
|
|
||||||
relationship.AssociationFieldName = elemType.Name()
|
|
||||||
|
|
||||||
associationForeignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]
|
|
||||||
if associationForeignKey == "" {
|
|
||||||
associationForeignKey = elemType.Name() + "Id"
|
|
||||||
}
|
|
||||||
|
|
||||||
relationship.ForeignFieldName = foreignKey
|
|
||||||
relationship.ForeignDBName = ToDBName(foreignKey)
|
|
||||||
relationship.AssociationForeignFieldName = associationForeignKey
|
|
||||||
relationship.AssociationForeignDBName = ToDBName(associationForeignKey)
|
|
||||||
field.Relationship = relationship
|
|
||||||
} else {
|
|
||||||
relationship.Kind = "has_many"
|
|
||||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
|
||||||
relationship.ForeignFieldName = foreignField.Name
|
|
||||||
relationship.ForeignDBName = foreignField.DBName
|
|
||||||
foreignField.IsForeignKey = true
|
|
||||||
field.Relationship = relationship
|
|
||||||
} else if relationship.ForeignFieldName != "" {
|
|
||||||
field.Relationship = relationship
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
field.IsNormal = true
|
|
||||||
}
|
|
||||||
case reflect.Struct:
|
|
||||||
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
|
||||||
for _, toField := range toScope.GetStructFields() {
|
|
||||||
toField = toField.clone()
|
|
||||||
toField.Names = append([]string{fieldStruct.Name}, toField.Names...)
|
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, toField)
|
|
||||||
if toField.IsPrimaryKey {
|
|
||||||
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, toField)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
belongsToForeignKey := foreignKey
|
|
||||||
if belongsToForeignKey == "" {
|
|
||||||
belongsToForeignKey = field.Name + "Id"
|
|
||||||
}
|
|
||||||
|
|
||||||
if foreignField := getForeignField(belongsToForeignKey, fields); foreignField != nil {
|
|
||||||
relationship.Kind = "belongs_to"
|
|
||||||
relationship.ForeignFieldName = foreignField.Name
|
|
||||||
relationship.ForeignDBName = foreignField.DBName
|
|
||||||
foreignField.IsForeignKey = true
|
|
||||||
field.Relationship = relationship
|
|
||||||
} else {
|
|
||||||
if foreignKey == "" {
|
|
||||||
foreignKey = modelStruct.ModelType.Name() + "Id"
|
|
||||||
}
|
|
||||||
relationship.Kind = "has_one"
|
|
||||||
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
|
||||||
relationship.ForeignFieldName = foreignField.Name
|
|
||||||
relationship.ForeignDBName = foreignField.DBName
|
|
||||||
foreignField.IsForeignKey = true
|
|
||||||
field.Relationship = relationship
|
|
||||||
} else if relationship.ForeignFieldName != "" {
|
|
||||||
field.Relationship = relationship
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
field.IsNormal = true
|
field.IsNormal = true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if field.IsNormal {
|
if !field.IsNormal {
|
||||||
if len(modelStruct.PrimaryFields) == 0 && field.DBName == "id" {
|
gormSettings := parseTagSetting(field.Tag.Get("gorm"))
|
||||||
field.IsPrimaryKey = true
|
toScope := scope.New(reflect.New(fieldStruct.Type).Interface())
|
||||||
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
|
|
||||||
|
getForeignField := func(column string, fields []*StructField) *StructField {
|
||||||
|
for _, field := range fields {
|
||||||
|
if field.Name == column || field.DBName == ToDBName(column) {
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var relationship = &Relationship{}
|
||||||
|
|
||||||
|
foreignKey := gormSettings["FOREIGNKEY"]
|
||||||
|
if polymorphic := gormSettings["POLYMORPHIC"]; polymorphic != "" {
|
||||||
|
if polymorphicField := getForeignField(polymorphic+"Id", toScope.GetStructFields()); polymorphicField != nil {
|
||||||
|
if polymorphicType := getForeignField(polymorphic+"Type", toScope.GetStructFields()); polymorphicType != nil {
|
||||||
|
relationship.ForeignFieldName = polymorphicField.Name
|
||||||
|
relationship.ForeignDBName = polymorphicField.DBName
|
||||||
|
relationship.PolymorphicType = polymorphicType.Name
|
||||||
|
relationship.PolymorphicDBName = polymorphicType.DBName
|
||||||
|
polymorphicType.IsForeignKey = true
|
||||||
|
polymorphicField.IsForeignKey = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch indirectType.Kind() {
|
||||||
|
case reflect.Slice:
|
||||||
|
elemType := indirectType.Elem()
|
||||||
|
if elemType.Kind() == reflect.Ptr {
|
||||||
|
elemType = elemType.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
if elemType.Kind() == reflect.Struct {
|
||||||
|
if foreignKey == "" {
|
||||||
|
foreignKey = scopeType.Name() + "Id"
|
||||||
|
}
|
||||||
|
|
||||||
|
if many2many := gormSettings["MANY2MANY"]; many2many != "" {
|
||||||
|
relationship.Kind = "many_to_many"
|
||||||
|
|
||||||
|
associationForeignKey := gormSettings["ASSOCIATIONFOREIGNKEY"]
|
||||||
|
if associationForeignKey == "" {
|
||||||
|
associationForeignKey = elemType.Name() + "Id"
|
||||||
|
}
|
||||||
|
|
||||||
|
relationship.ForeignFieldName = foreignKey
|
||||||
|
relationship.ForeignDBName = ToDBName(foreignKey)
|
||||||
|
relationship.AssociationForeignFieldName = associationForeignKey
|
||||||
|
relationship.AssociationForeignDBName = ToDBName(associationForeignKey)
|
||||||
|
|
||||||
|
joinTableHandler := JoinTableHandler{}
|
||||||
|
joinTableHandler.Setup(relationship, many2many, scopeType, elemType)
|
||||||
|
relationship.JoinTableHandler = &joinTableHandler
|
||||||
|
field.Relationship = relationship
|
||||||
|
|
||||||
|
relationship.PivotKey = gormSettings["MANY2MANY"]
|
||||||
|
relationship.PivotType = elemType.Name() + "Id"
|
||||||
|
relationship.AssociationFieldName = elemType.Name()
|
||||||
|
} else {
|
||||||
|
relationship.Kind = "has_many"
|
||||||
|
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
|
relationship.ForeignFieldName = foreignField.Name
|
||||||
|
relationship.ForeignDBName = foreignField.DBName
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
field.Relationship = relationship
|
||||||
|
} else if relationship.ForeignFieldName != "" {
|
||||||
|
field.Relationship = relationship
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
field.IsNormal = true
|
||||||
|
}
|
||||||
|
case reflect.Struct:
|
||||||
|
if _, ok := gormSettings["EMBEDDED"]; ok || fieldStruct.Anonymous {
|
||||||
|
for _, toField := range toScope.GetStructFields() {
|
||||||
|
toField = toField.clone()
|
||||||
|
toField.Names = append([]string{fieldStruct.Name}, toField.Names...)
|
||||||
|
modelStruct.StructFields = append(modelStruct.StructFields, toField)
|
||||||
|
if toField.IsPrimaryKey {
|
||||||
|
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, toField)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
belongsToForeignKey := foreignKey
|
||||||
|
if belongsToForeignKey == "" {
|
||||||
|
belongsToForeignKey = field.Name + "Id"
|
||||||
|
}
|
||||||
|
|
||||||
|
if foreignField := getForeignField(belongsToForeignKey, fields); foreignField != nil {
|
||||||
|
relationship.Kind = "belongs_to"
|
||||||
|
relationship.ForeignFieldName = foreignField.Name
|
||||||
|
relationship.ForeignDBName = foreignField.DBName
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
field.Relationship = relationship
|
||||||
|
} else {
|
||||||
|
if foreignKey == "" {
|
||||||
|
foreignKey = modelStruct.ModelType.Name() + "Id"
|
||||||
|
}
|
||||||
|
relationship.Kind = "has_one"
|
||||||
|
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
|
||||||
|
relationship.ForeignFieldName = foreignField.Name
|
||||||
|
relationship.ForeignDBName = foreignField.DBName
|
||||||
|
foreignField.IsForeignKey = true
|
||||||
|
field.Relationship = relationship
|
||||||
|
} else if relationship.ForeignFieldName != "" {
|
||||||
|
field.Relationship = relationship
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
field.IsNormal = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if field.IsNormal {
|
||||||
|
if len(modelStruct.PrimaryFields) == 0 && field.DBName == "id" {
|
||||||
|
field.IsPrimaryKey = true
|
||||||
|
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
||||||
}
|
}
|
||||||
modelStruct.StructFields = append(modelStruct.StructFields, field)
|
}()
|
||||||
}
|
|
||||||
|
|
||||||
modelStructs[scopeType] = &modelStruct
|
modelStructs[scopeType] = &modelStruct
|
||||||
|
|
||||||
@ -314,7 +320,7 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
|
|||||||
|
|
||||||
additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"]
|
additionalType := sqlSettings["NOT NULL"] + " " + sqlSettings["UNIQUE"]
|
||||||
if value, ok := sqlSettings["DEFAULT"]; ok {
|
if value, ok := sqlSettings["DEFAULT"]; ok {
|
||||||
additionalType = additionalType + "DEFAULT " + value
|
additionalType = additionalType + " DEFAULT " + value
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.IsScanner {
|
if field.IsScanner {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user