Make null.Time.Scan more robust by returning an error on the wrong type
This commit is contained in:
parent
b9afe213ba
commit
8112aae462
1 changed files with 9 additions and 2 deletions
11
time.go
11
time.go
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue