Beispiel #1
0
func TestSetNullable(t *testing.T) {
	var ns String
	expect.False(t, ns.Valid)
	ns.Set("hello world")
	expect.True(t, ns.Valid)

	var ni Int
	expect.False(t, ni.Valid)
	ni.Set(1)
	expect.True(t, ni.Valid)

	var nt Time
	expect.False(t, nt.Valid)
	nt.Set(time.Now())
	expect.True(t, nt.Valid)

	var nd Date
	expect.False(t, nd.Valid)
	nd.Set(date.From(time.Now()))
	expect.True(t, nd.Valid)

	var nb Bool
	expect.False(t, nb.Valid)
	nb.Set(true)
	expect.True(t, nb.Valid)

	var nu UUID
	expect.False(t, nu.Valid)
	nu.Set(uuid.UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8})
	expect.True(t, nu.Valid)
}
Beispiel #2
0
// Implement sql.Scanner interface
func (n *Date) Scan(src interface{}) error {
	n.Valid = false
	if src == nil {
		return nil
	}

	var srcTime Time
	switch t := src.(type) {
	case string:
		var err error
		srcTime.Time, err = time.Parse("2006-01-02", t)
		if err != nil {
			return err
		}
	case []byte:
		var err error
		srcTime.Time, err = time.Parse("2006-01-02", string(t))
		if err != nil {
			return err
		}
	case time.Time:
		srcTime.Time = t
	default:
		return errors.New("null: scan value was not a Time, []byte, string, or nil")
	}

	n.Valid = true
	n.Date = date.From(srcTime.Time)
	return nil
}