gorm/field.go
jnfeinstein 8b451f0084 Add support for polymorphic relationships using the POLYMORPHIC setting.
This commit adds support for two settings:

FOREIGNTYPE - A field that is used to store the type of the owner.

POLYMORPHIC - A shortcut to set FOREIGNKEY and FOREIGNTYPE to the same
value suffixed by "Id" and "Type" respectively.

The type is stored as the table name, which I thought might be useful
for other queries.

The biggest gotcha of this commit is that I flipped the definition of
has_one and belongs_to. gorm is very flexible such that it didn't
really care if it was a has_one or belongs_to, and can pretty much
determine it at runtime. For the sake of the error, I had to define
one of them as belongs_to, and I chose the one with the fields as
the belongs_to, like ActiveRecord. The error could probably be
genericized to "gorm cannot determine type", but I think it's nicer
to tell people DONT DO PATTERN XYZ CAUSE IT WONT WORK. Functionally,
it doesn't matter.
2014-11-25 21:35:47 -08:00

66 lines
1.4 KiB
Go

package gorm
import (
"database/sql"
"errors"
"reflect"
"time"
)
type relationship struct {
JoinTable string
ForeignKey string
ForeignType string
AssociationForeignKey string
Kind string
}
type Field struct {
Name string
DBName string
Field reflect.Value
Tag reflect.StructTag
Relationship *relationship
IsNormal bool
IsBlank bool
IsIgnored bool
IsPrimaryKey bool
DefaultValue interface{}
}
func (field *Field) IsScanner() bool {
_, isScanner := reflect.New(field.Field.Type()).Interface().(sql.Scanner)
return isScanner
}
func (field *Field) IsTime() bool {
_, isTime := field.Field.Interface().(time.Time)
return isTime
}
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("field value not addressable")
}
if rvalue, ok := value.(reflect.Value); ok {
value = rvalue.Interface()
}
if scanner, ok := field.Field.Addr().Interface().(sql.Scanner); ok {
scanner.Scan(value)
} else if reflect.TypeOf(value).ConvertibleTo(field.Field.Type()) {
field.Field.Set(reflect.ValueOf(value).Convert(field.Field.Type()))
} else {
return errors.New("could not convert argument")
}
field.IsBlank = isBlank(field.Field)
return
}