Refactor Association

This commit is contained in:
Jinzhu 2015-02-17 19:17:21 +08:00
parent 2a8c7973cb
commit 00d1187c4c
1 changed files with 14 additions and 34 deletions

View File

@ -30,27 +30,12 @@ func (association *Association) Find(value interface{}) *Association {
func (association *Association) Append(values ...interface{}) *Association { func (association *Association) Append(values ...interface{}) *Association {
scope := association.Scope scope := association.Scope
field := association.Field field := association.Field
fieldType := field.Field.Type()
for _, value := range values { for _, value := range values {
reflectvalue := reflect.ValueOf(value) reflectvalue := reflect.Indirect(reflect.ValueOf(value))
if reflectvalue.Kind() == reflect.Ptr { if reflectvalue.Kind() == reflect.Struct {
if reflectvalue.Elem().Kind() == reflect.Struct {
if fieldType.Elem().Kind() == reflect.Ptr {
field.Set(reflect.Append(field.Field, reflectvalue)) field.Set(reflect.Append(field.Field, reflectvalue))
} else if fieldType.Elem().Kind() == reflect.Struct { } else if reflectvalue.Kind() == reflect.Slice {
field.Set(reflect.Append(field.Field, reflectvalue.Elem()))
}
} else if reflectvalue.Elem().Kind() == reflect.Slice {
if fieldType.Elem().Kind() == reflect.Ptr {
field.Set(reflect.AppendSlice(field.Field, reflectvalue))
} else if fieldType.Elem().Kind() == reflect.Struct {
field.Set(reflect.AppendSlice(field.Field, reflectvalue.Elem()))
}
}
} else if reflectvalue.Kind() == reflect.Struct && fieldType.Elem().Kind() == reflect.Struct {
field.Set(reflect.Append(field.Field, reflectvalue))
} else if reflectvalue.Kind() == reflect.Slice && fieldType.Elem() == reflectvalue.Type().Elem() {
field.Set(reflect.AppendSlice(field.Field, reflectvalue)) field.Set(reflect.AppendSlice(field.Field, reflectvalue))
} else { } else {
association.setErr(errors.New("invalid association type")) association.setErr(errors.New("invalid association type"))
@ -65,23 +50,18 @@ func (association *Association) getPrimaryKeys(values ...interface{}) []interfac
scope := association.Scope scope := association.Scope
for _, value := range values { for _, value := range values {
reflectValue := reflect.ValueOf(value) reflectValue := reflect.Indirect(reflect.ValueOf(value))
if reflectValue.Kind() == reflect.Ptr {
reflectValue = reflectValue.Elem()
}
if reflectValue.Kind() == reflect.Slice { if reflectValue.Kind() == reflect.Slice {
for i := 0; i < reflectValue.Len(); i++ { for i := 0; i < reflectValue.Len(); i++ {
newScope := scope.New(reflectValue.Index(i).Interface()) primaryField := scope.New(reflectValue.Index(i).Interface()).PrimaryKeyField()
primaryKey := newScope.PrimaryKeyValue() if !primaryField.IsBlank {
if !reflect.DeepEqual(reflect.ValueOf(primaryKey), reflect.Zero(reflect.ValueOf(primaryKey).Type())) { primaryKeys = append(primaryKeys, primaryField.Field.Interface())
primaryKeys = append(primaryKeys, primaryKey)
} }
} }
} else if reflectValue.Kind() == reflect.Struct { } else if reflectValue.Kind() == reflect.Struct {
newScope := scope.New(value) primaryField := scope.New(value).PrimaryKeyField()
primaryKey := newScope.PrimaryKeyValue() if !primaryField.IsBlank {
if !reflect.DeepEqual(reflect.ValueOf(primaryKey), reflect.Zero(reflect.ValueOf(primaryKey).Type())) { primaryKeys = append(primaryKeys, primaryField.Field.Interface())
primaryKeys = append(primaryKeys, primaryKey)
} }
} }
} }
@ -121,16 +101,16 @@ func (association *Association) Replace(values ...interface{}) *Association {
newPrimaryKeys := association.getPrimaryKeys(field.Interface()) newPrimaryKeys := association.getPrimaryKeys(field.Interface())
var addedPrimaryKeys = []interface{}{} var addedPrimaryKeys = []interface{}{}
for _, new := range newPrimaryKeys { for _, newKey := range newPrimaryKeys {
hasEqual := false hasEqual := false
for _, old := range oldPrimaryKeys { for _, oldKey := range oldPrimaryKeys {
if reflect.DeepEqual(new, old) { if reflect.DeepEqual(newKey, oldKey) {
hasEqual = true hasEqual = true
break break
} }
} }
if !hasEqual { if !hasEqual {
addedPrimaryKeys = append(addedPrimaryKeys, new) addedPrimaryKeys = append(addedPrimaryKeys, newKey)
} }
} }
for _, primaryKey := range association.getPrimaryKeys(values...) { for _, primaryKey := range association.getPrimaryKeys(values...) {