Loggest thy stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
733 B

  1. package sqltypes
  2. import (
  3. "database/sql/driver"
  4. "errors"
  5. "git.aiterp.net/stufflog3/stufflog3-api/internal/models"
  6. "time"
  7. )
  8. type NullDate struct {
  9. Date models.Date
  10. Valid bool
  11. }
  12. func (n *NullDate) Scan(value interface{}) error {
  13. if value == nil {
  14. n.Valid = false
  15. return nil
  16. }
  17. switch value := value.(type) {
  18. case string:
  19. date, err := models.ParseDate(value)
  20. if err != nil {
  21. return err
  22. }
  23. n.Date = date
  24. n.Valid = true
  25. case time.Time:
  26. n.Date = models.Date{value.Year(), int(value.Month()), value.Day()}
  27. n.Valid = true
  28. default:
  29. return errors.New("invalid type")
  30. }
  31. return nil
  32. }
  33. func (n NullDate) Value() (driver.Value, error) {
  34. if !n.Valid {
  35. return nil, nil
  36. }
  37. return n.Date.String(), nil
  38. }