bool.go 865 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package models
  2. import (
  3. "database/sql"
  4. "database/sql/driver"
  5. "github.com/rotisserie/eris"
  6. )
  7. func NewBool(b bool) sql.NullBool {
  8. return sql.NullBool{Bool: b, Valid: true}
  9. }
  10. // BitBool is an implementation of a bool for the MySQL type BIT(1).
  11. // This type allows you to avoid wasting an entire byte for MySQL's boolean type TINYINT.
  12. type BitBool bool
  13. // Value implements the driver.Valuer interface,
  14. // and turns the BitBool into a bitfield (BIT(1)) for MySQL storage.
  15. func (b BitBool) Value() (driver.Value, error) {
  16. if b {
  17. return []byte{1}, nil
  18. } else {
  19. return []byte{0}, nil
  20. }
  21. }
  22. // Scan implements the sql.Scanner interface,
  23. // and turns the bitfield incoming from MySQL into a BitBool
  24. func (b *BitBool) Scan(src interface{}) error {
  25. v, ok := src.([]byte)
  26. if !ok {
  27. return eris.New("bad []byte type assertion")
  28. }
  29. *b = v[0] == 1
  30. return nil
  31. }