gorm/field.go

85 lines
2.1 KiB
Go
Raw Normal View History

2013-11-14 17:26:02 +04:00
package gorm
import (
"database/sql"
"errors"
"fmt"
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
2016-01-16 06:37:16 +03:00
func (field *Field) Set(value interface{}) (err error) {
if !field.Field.IsValid() {
return errors.New("field value not valid")
2014-09-02 15:03:01 +04:00
}
if !field.Field.CanAddr() {
return errors.New("unaddressable value")
}
2016-01-04 16:49:04 +03:00
reflectValue, ok := value.(reflect.Value)
if !ok {
reflectValue = reflect.ValueOf(value)
2014-09-02 15:03:01 +04:00
}
2016-01-16 06:37:16 +03:00
fieldValue := field.Field
2016-01-04 16:49:04 +03:00
if reflectValue.IsValid() {
2016-01-16 06:37:16 +03:00
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
2015-02-17 17:55:14 +03:00
} else {
2016-01-16 06:37:16 +03:00
if fieldValue.Kind() == reflect.Ptr {
if fieldValue.IsNil() {
fieldValue.Set(reflect.New(field.Struct.Type.Elem()))
}
fieldValue = fieldValue.Elem()
}
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
} else if scanner, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
err = scanner.Scan(reflectValue.Interface())
} else {
err = fmt.Errorf("could not convert argument of field %s from %s to %s", field.Name, reflectValue.Type(), fieldValue.Type())
}
2015-02-17 17:55:14 +03:00
}
2016-01-04 16:49:04 +03:00
} else {
field.Field.Set(reflect.Zero(field.Field.Type()))
}
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 {
2016-01-17 12:46:56 +03:00
var (
fields = map[string]*Field{}
indirectScopeValue = scope.IndirectValue()
isStruct = indirectScopeValue.Kind() == reflect.Struct
)
2015-02-16 12:47:07 +03:00
2016-01-17 12:46:56 +03:00
for _, structField := range scope.GetModelStruct().StructFields {
if field, ok := fields[structField.DBName]; !ok || field.IsIgnored {
if isStruct {
2016-01-17 12:46:56 +03:00
fieldValue := indirectScopeValue
for _, name := range structField.Names {
fieldValue = reflect.Indirect(fieldValue).FieldByName(name)
}
fields[structField.DBName] = &Field{StructField: structField, Field: fieldValue, IsBlank: isBlank(fieldValue)}
} else {
fields[structField.DBName] = &Field{StructField: structField, IsBlank: true}
}
2015-02-17 17:55:14 +03:00
}
2015-02-17 12:40:21 +03:00
}
2015-02-15 18:01:09 +03:00
2016-01-03 06:11:30 +03:00
scope.fields = fields
2015-02-17 12:40:21 +03:00
}
return scope.fields
2015-02-15 18:01:09 +03:00
}