2013-11-14 17:26:02 +04:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2014-09-30 16:02:51 +04:00
|
|
|
"errors"
|
2013-11-15 07:36:27 +04:00
|
|
|
"reflect"
|
2013-11-14 17:26:02 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Field struct {
|
2015-02-15 18:01:09 +03:00
|
|
|
*StructField
|
2015-02-16 07:04:46 +03:00
|
|
|
IsBlank bool
|
|
|
|
Field reflect.Value
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
2014-09-02 15:03:01 +04:00
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
func (field *Field) Set(value interface{}) error {
|
2014-09-30 16:02:51 +04:00
|
|
|
if !field.Field.IsValid() {
|
|
|
|
return errors.New("field value not valid")
|
2014-09-02 15:03:01 +04:00
|
|
|
}
|
2014-09-30 16:02:51 +04:00
|
|
|
|
|
|
|
if !field.Field.CanAddr() {
|
2015-02-23 04:40:39 +03:00
|
|
|
return errors.New("unaddressable value")
|
2014-09-30 16:02:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if rvalue, ok := value.(reflect.Value); ok {
|
|
|
|
value = rvalue.Interface()
|
2014-09-02 15:03:01 +04:00
|
|
|
}
|
2014-09-30 16:02:51 +04:00
|
|
|
|
|
|
|
if scanner, ok := field.Field.Addr().Interface().(sql.Scanner); ok {
|
2015-02-17 17:55:14 +03:00
|
|
|
if v, ok := value.(reflect.Value); ok {
|
2015-03-16 03:22:31 +03:00
|
|
|
if err := scanner.Scan(v.Interface()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-17 17:55:14 +03:00
|
|
|
} else {
|
2015-03-16 03:22:31 +03:00
|
|
|
if err := scanner.Scan(value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-17 17:55:14 +03:00
|
|
|
}
|
2014-09-30 16:02:51 +04:00
|
|
|
} else {
|
2015-02-17 17:55:14 +03:00
|
|
|
reflectValue, ok := value.(reflect.Value)
|
|
|
|
if !ok {
|
|
|
|
reflectValue = reflect.ValueOf(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflectValue.Type().ConvertibleTo(field.Field.Type()) {
|
|
|
|
field.Field.Set(reflectValue.Convert(field.Field.Type()))
|
|
|
|
} else {
|
|
|
|
return errors.New("could not convert argument")
|
|
|
|
}
|
2014-09-30 16:02:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
field.IsBlank = isBlank(field.Field)
|
2015-02-17 17:55:14 +03:00
|
|
|
return nil
|
2014-09-02 15:03:01 +04:00
|
|
|
}
|
2015-02-15 18:01:09 +03:00
|
|
|
|
2015-02-16 12:47:07 +03:00
|
|
|
// Fields get value's fields
|
|
|
|
func (scope *Scope) Fields() map[string]*Field {
|
2015-02-17 12:40:21 +03:00
|
|
|
if scope.fields == nil {
|
|
|
|
fields := map[string]*Field{}
|
|
|
|
structFields := scope.GetStructFields()
|
2015-02-16 12:47:07 +03:00
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
indirectValue := scope.IndirectValue()
|
|
|
|
isStruct := indirectValue.Kind() == reflect.Struct
|
2015-02-17 12:40:21 +03:00
|
|
|
for _, structField := range structFields {
|
2015-02-17 17:55:14 +03:00
|
|
|
if isStruct {
|
|
|
|
fields[structField.DBName] = getField(indirectValue, structField)
|
|
|
|
} else {
|
|
|
|
fields[structField.DBName] = &Field{StructField: structField, IsBlank: true}
|
|
|
|
}
|
2015-02-17 12:40:21 +03:00
|
|
|
}
|
2015-02-15 18:01:09 +03:00
|
|
|
|
2015-02-17 12:40:21 +03:00
|
|
|
scope.fields = fields
|
|
|
|
}
|
|
|
|
return scope.fields
|
2015-02-15 18:01:09 +03:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:55:14 +03:00
|
|
|
func getField(indirectValue reflect.Value, structField *StructField) *Field {
|
|
|
|
field := &Field{StructField: structField}
|
|
|
|
for _, name := range structField.Names {
|
|
|
|
indirectValue = reflect.Indirect(indirectValue).FieldByName(name)
|
2015-02-17 09:30:37 +03:00
|
|
|
}
|
2015-02-17 17:55:14 +03:00
|
|
|
field.Field = indirectValue
|
2015-02-17 12:40:21 +03:00
|
|
|
field.IsBlank = isBlank(indirectValue)
|
2015-02-17 17:55:14 +03:00
|
|
|
return field
|
2015-02-15 18:01:09 +03:00
|
|
|
}
|