mirror of https://github.com/go-gorm/gorm.git
92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package gorm
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
// Field model field definition
|
|
type Field struct {
|
|
*StructField
|
|
IsBlank bool
|
|
Field reflect.Value
|
|
}
|
|
|
|
// Set set a value to the field
|
|
func (field *Field) Set(value interface{}) (err error) {
|
|
if !field.Field.IsValid() {
|
|
return errors.New("field value not valid")
|
|
}
|
|
|
|
if !field.Field.CanAddr() {
|
|
return errors.New("unaddressable value")
|
|
}
|
|
|
|
reflectValue, ok := value.(reflect.Value)
|
|
if !ok {
|
|
reflectValue = reflect.ValueOf(value)
|
|
}
|
|
|
|
fieldValue := field.Field
|
|
if reflectValue.IsValid() {
|
|
if reflectValue.Type().ConvertibleTo(fieldValue.Type()) {
|
|
fieldValue.Set(reflectValue.Convert(fieldValue.Type()))
|
|
} else {
|
|
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())
|
|
}
|
|
}
|
|
} else {
|
|
field.Field.Set(reflect.Zero(field.Field.Type()))
|
|
}
|
|
|
|
field.IsBlank = isBlank(field.Field)
|
|
return nil
|
|
}
|
|
|
|
// Fields get value's fields
|
|
func (scope *Scope) Fields() []*Field {
|
|
var (
|
|
fields []*Field
|
|
indirectScopeValue = scope.IndirectValue()
|
|
isStruct = indirectScopeValue.Kind() == reflect.Struct
|
|
)
|
|
|
|
for _, structField := range scope.GetModelStruct().StructFields {
|
|
if isStruct {
|
|
fieldValue := indirectScopeValue
|
|
for _, name := range structField.Names {
|
|
fieldValue = reflect.Indirect(fieldValue).FieldByName(name)
|
|
}
|
|
fields = append(fields, &Field{StructField: structField, Field: fieldValue, IsBlank: isBlank(fieldValue)})
|
|
} else {
|
|
fields = append(fields, &Field{StructField: structField, IsBlank: true})
|
|
}
|
|
}
|
|
|
|
return fields
|
|
}
|
|
|
|
func (scope *Scope) fieldsMap() map[string]*Field {
|
|
var results = map[string]*Field{}
|
|
for _, field := range scope.Fields() {
|
|
if field.IsNormal {
|
|
results[field.DBName] = field
|
|
}
|
|
}
|
|
return results
|
|
}
|