Refactor GetModelStruct

This commit is contained in:
Jinzhu 2016-01-03 14:04:59 +08:00
parent 6a5a2dbc55
commit 4bc06a21c1
1 changed files with 98 additions and 42 deletions

View File

@ -192,11 +192,19 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
var ( var (
relationship = &Relationship{} relationship = &Relationship{}
toScope = scope.New(reflect.New(field.Struct.Type).Interface()) toScope = scope.New(reflect.New(field.Struct.Type).Interface())
foreignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";") foreignKeys []string
associationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ";") associationForeignKeys []string
elemType = field.Struct.Type elemType = field.Struct.Type
) )
if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
foreignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";")
}
if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
associationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ";")
}
for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr { for elemType.Kind() == reflect.Slice || elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem() elemType = elemType.Elem()
} }
@ -248,21 +256,33 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
// if no foreign keys // if no foreign keys
if len(foreignKeys) == 0 { if len(foreignKeys) == 0 {
for _, field := range scope.PrimaryFields() { // FIXME if len(associationForeignKeys) == 0 {
foreignKeys = append(foreignKeys, reflectType.Name()+field.Name) for _, field := range modelStruct.PrimaryFields {
foreignKeys = append(foreignKeys, reflectType.Name()+field.Name)
associationForeignKeys = append(associationForeignKeys, field.Name)
}
} else {
for _, associationForeignKey := range associationForeignKeys {
if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
foreignKeys = append(foreignKeys, reflectType.Name()+foreignField.Name)
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
}
}
} }
} }
for _, foreignKey := range foreignKeys { for idx, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil { if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
// source foreign keys if associationField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); associationField != nil {
foreignField.IsForeignKey = true // source foreign keys
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.Name) foreignField.IsForeignKey = true
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, field.DBName) relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
// association foreign keys // association foreign keys
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name) relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName) relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
}
} }
} }
@ -277,30 +297,53 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
case reflect.Struct: case reflect.Struct:
defer func(field *StructField) { defer func(field *StructField) {
var ( var (
relationship = &Relationship{} relationship = &Relationship{}
toScope = scope.New(reflect.New(field.Struct.Type).Interface()) toScope = scope.New(reflect.New(field.Struct.Type).Interface())
tagForeignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";") tagForeignKeys []string
foreignKeys = tagForeignKeys tagAssociationForeignKeys []string
) )
if len(tagForeignKeys) == 0 { if foreignKey := field.TagSettings["FOREIGNKEY"]; foreignKey != "" {
for _, primaryField := range scope.PrimaryFields() { tagForeignKeys = strings.Split(field.TagSettings["FOREIGNKEY"], ";")
foreignKeys = append(foreignKeys, modelStruct.ModelType.Name()+primaryField.Name)
}
} }
// if has one if foreignKey := field.TagSettings["ASSOCIATIONFOREIGNKEY"]; foreignKey != "" {
for _, foreignKey := range foreignKeys { tagAssociationForeignKeys = strings.Split(field.TagSettings["ASSOCIATIONFOREIGNKEY"], ";")
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil { }
foreignField.IsForeignKey = true
// source foreign keys
scopeField := getForeignField(foreignKey, modelStruct.StructFields)
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)
// association foreign keys // Has One
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name) {
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName) var foreignKeys = tagForeignKeys
var associationForeignKeys = tagAssociationForeignKeys
if len(foreignKeys) == 0 {
if len(associationForeignKeys) == 0 {
for _, primaryField := range modelStruct.PrimaryFields {
foreignKeys = append(foreignKeys, modelStruct.ModelType.Name()+primaryField.Name)
associationForeignKeys = append(associationForeignKeys, primaryField.Name)
}
} else {
for _, associationForeignKey := range tagAssociationForeignKeys {
if foreignField := getForeignField(associationForeignKey, modelStruct.StructFields); foreignField != nil {
foreignKeys = append(foreignKeys, modelStruct.ModelType.Name()+foreignField.Name)
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
}
}
}
}
for idx, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, toScope.GetStructFields()); foreignField != nil {
if scopeField := getForeignField(associationForeignKeys[idx], modelStruct.StructFields); scopeField != nil {
foreignField.IsForeignKey = true
// source foreign keys
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, scopeField.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, scopeField.DBName)
// association foreign keys
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
}
}
} }
} }
@ -308,23 +351,36 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
relationship.Kind = "has_one" relationship.Kind = "has_one"
field.Relationship = relationship field.Relationship = relationship
} else { } else {
foreignKeys = tagForeignKeys var foreignKeys = tagForeignKeys
var associationForeignKeys = tagAssociationForeignKeys
if len(foreignKeys) == 0 { if len(foreignKeys) == 0 {
for _, f := range toScope.PrimaryFields() { if len(associationForeignKeys) == 0 {
foreignKeys = append(foreignKeys, field.Name+f.Name) for _, f := range toScope.PrimaryFields() {
foreignKeys = append(foreignKeys, field.Name+f.Name)
associationForeignKeys = append(associationForeignKeys, f.Name)
}
} else {
for _, associationForeignKey := range associationForeignKeys {
if foreignField := getForeignField(associationForeignKey, toScope.GetStructFields()); foreignField != nil {
foreignKeys = append(foreignKeys, field.Name+foreignField.Name)
associationForeignKeys = append(associationForeignKeys, foreignField.Name)
}
}
} }
} }
for _, foreignKey := range foreignKeys { for idx, foreignKey := range foreignKeys {
if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil { if foreignField := getForeignField(foreignKey, modelStruct.StructFields); foreignField != nil {
// association foreign keys if associationField := getForeignField(associationForeignKeys[idx], toScope.GetStructFields()); associationField != nil {
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, toScope.PrimaryField().Name) // association foreign keys
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, toScope.PrimaryField().DBName) relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, associationField.Name)
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, associationField.DBName)
// source foreign keys // source foreign keys
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name) relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, foreignField.Name)
relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName) relationship.ForeignDBNames = append(relationship.ForeignDBNames, foreignField.DBName)
foreignField.IsForeignKey = true foreignField.IsForeignKey = true
}
} }
} }