func TestTimeOrNull(t *testing.T) { var ( nullTime pq.NullTime value driver.Value err error ) // When the time is nil nullTime = TimeOrNull(nil) // nullTime.Valid should be false assert.False(t, nullTime.Valid) // nullInt.Value() should return nil value, err = nullTime.Value() assert.Nil(t, err) assert.Nil(t, value) // When the time is time.Time instance now := time.Now() nullTime = TimeOrNull(&now) // nullTime.Valid should be true assert.True(t, nullTime.Valid) // nullTime.Value() should return the time.Time value, err = nullTime.Value() assert.Nil(t, err) assert.Equal(t, now, value) }
func setTimer(rows *sql.Rows) (*Timer, error) { var finishedAt pq.NullTime var createdAt pq.NullTime timer := Timer{} err := rows.Scan(&timer.ID, &timer.User, &timer.Name, &createdAt, &finishedAt) if err != nil { return nil, err } val, err := createdAt.Value() if err != nil { return nil, err } if val != nil { timer.CreatedAt = &createdAt.Time } val, err = finishedAt.Value() if err != nil { return nil, err } if val != nil { timer.FinishedAt = &finishedAt.Time } return &timer, nil }