Make null.Time.Scan more robust by returning an error on the wrong type

This commit is contained in:
Jed Borovik 2015-09-10 10:56:15 -04:00
parent b9afe213ba
commit 8112aae462

11
time.go
View file

@ -17,8 +17,15 @@ type Time struct {
// Scan implements the Scanner interface.
func (t *Time) Scan(value interface{}) error {
t.Time, t.Valid = value.(time.Time)
return nil
var err error
switch x := value.(type) {
case time.Time:
t.Time = x
default:
err = fmt.Errorf("null: cannot scan type %T into null.Time: %v", value, value)
}
t.Valid = err == nil
return err
}
// Value implements the driver Valuer interface.