2016-11-10 16:40:50 +01:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Byte is an nullable int.
|
|
|
|
type Byte struct {
|
|
|
|
Byte byte
|
|
|
|
Valid bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewByte creates a new Byte
|
|
|
|
func NewByte(b byte, valid bool) Byte {
|
|
|
|
return Byte{
|
|
|
|
Byte: b,
|
|
|
|
Valid: valid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByteFrom creates a new Byte that will always be valid.
|
|
|
|
func ByteFrom(b byte) Byte {
|
|
|
|
return NewByte(b, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByteFromPtr creates a new Byte that be null if i is nil.
|
|
|
|
func ByteFromPtr(b *byte) Byte {
|
|
|
|
if b == nil {
|
|
|
|
return NewByte(0, false)
|
|
|
|
}
|
|
|
|
return NewByte(*b, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (b *Byte) UnmarshalJSON(data []byte) error {
|
|
|
|
if len(data) == 0 || bytes.Equal(data, NullBytes) {
|
|
|
|
b.Valid = false
|
|
|
|
b.Byte = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var x string
|
|
|
|
if err := json.Unmarshal(data, &x); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(x) > 1 {
|
|
|
|
return errors.New("json: cannot convert to byte, text len is greater than one")
|
|
|
|
}
|
|
|
|
|
2016-11-11 08:15:01 +01:00
|
|
|
b.Byte = x[0]
|
2016-11-10 16:40:50 +01:00
|
|
|
b.Valid = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
func (b *Byte) UnmarshalText(text []byte) error {
|
2016-11-11 08:15:01 +01:00
|
|
|
if text == nil || len(text) == 0 {
|
2016-11-10 16:40:50 +01:00
|
|
|
b.Valid = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(text) > 1 {
|
|
|
|
return errors.New("text: cannot convert to byte, text len is greater than one")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Valid = true
|
|
|
|
b.Byte = text[0]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (b Byte) MarshalJSON() ([]byte, error) {
|
|
|
|
if !b.Valid {
|
|
|
|
return NullBytes, nil
|
|
|
|
}
|
2016-11-11 08:15:01 +01:00
|
|
|
return []byte{'"', b.Byte, '"'}, nil
|
2016-11-10 16:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
|
|
|
func (b Byte) MarshalText() ([]byte, error) {
|
|
|
|
if !b.Valid {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
return []byte{b.Byte}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValid changes this Byte's value and also sets it to be non-null.
|
|
|
|
func (b *Byte) SetValid(n byte) {
|
|
|
|
b.Byte = n
|
|
|
|
b.Valid = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ptr returns a pointer to this Byte's value, or a nil pointer if this Byte is null.
|
|
|
|
func (b Byte) Ptr() *byte {
|
|
|
|
if !b.Valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &b.Byte
|
|
|
|
}
|
|
|
|
|
2017-07-06 20:14:43 +02:00
|
|
|
// IsNull returns true for invalid Bytes, for future omitempty support (Go 1.4?)
|
|
|
|
func (b Byte) IsNull() bool {
|
2016-11-10 16:40:50 +01:00
|
|
|
return !b.Valid
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (b *Byte) Scan(value interface{}) error {
|
|
|
|
if value == nil {
|
|
|
|
b.Byte, b.Valid = 0, false
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-12 04:40:11 +01:00
|
|
|
|
|
|
|
val := value.(string)
|
|
|
|
if len(val) == 0 {
|
|
|
|
b.Valid = false
|
|
|
|
b.Byte = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-10 16:40:50 +01:00
|
|
|
b.Valid = true
|
2016-11-12 04:40:11 +01:00
|
|
|
b.Byte = byte(val[0])
|
|
|
|
return nil
|
2016-11-10 16:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (b Byte) Value() (driver.Value, error) {
|
|
|
|
if !b.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return []byte{b.Byte}, nil
|
|
|
|
}
|