lbry.go/bytes.go

125 lines
2.4 KiB
Go
Raw Normal View History

2016-09-06 18:56:31 +02:00
package null
import (
2016-11-10 08:47:49 +01:00
"bytes"
2016-09-06 18:56:31 +02:00
"database/sql/driver"
2016-11-10 08:47:49 +01:00
"encoding/json"
2016-09-06 18:56:31 +02:00
2016-11-12 02:45:55 +01:00
"gopkg.in/nullbio/null.v6/convert"
2016-09-06 18:56:31 +02:00
)
2016-11-10 08:47:49 +01:00
// NullBytes is a global byte slice of JSON null
var NullBytes = []byte("null")
2016-09-06 18:56:31 +02:00
// Bytes is a nullable []byte.
type Bytes struct {
2016-11-10 08:47:49 +01:00
Bytes []byte
Valid bool
2016-09-06 18:56:31 +02:00
}
// NewBytes creates a new Bytes
func NewBytes(b []byte, valid bool) Bytes {
return Bytes{
2016-11-10 08:47:49 +01:00
Bytes: b,
Valid: valid,
2016-09-06 18:56:31 +02:00
}
}
2016-09-07 14:24:10 +02:00
// BytesFrom creates a new Bytes that will be invalid if nil.
2016-09-06 18:56:31 +02:00
func BytesFrom(b []byte) Bytes {
2016-09-07 14:24:10 +02:00
return NewBytes(b, b != nil)
2016-09-06 18:56:31 +02:00
}
2016-09-07 14:24:10 +02:00
// BytesFromPtr creates a new Bytes that will be invalid if nil.
2016-09-06 18:56:31 +02:00
func BytesFromPtr(b *[]byte) Bytes {
2016-09-07 14:24:10 +02:00
if b == nil {
2016-09-06 18:56:31 +02:00
return NewBytes(nil, false)
}
n := NewBytes(*b, true)
return n
}
// UnmarshalJSON implements json.Unmarshaler.
func (b *Bytes) UnmarshalJSON(data []byte) error {
2016-11-10 08:47:49 +01:00
if bytes.Equal(data, NullBytes) {
b.Valid = false
b.Bytes = nil
return nil
2016-09-06 18:56:31 +02:00
}
2016-11-10 08:47:49 +01:00
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
2016-11-10 08:47:49 +01:00
b.Bytes = []byte(s)
b.Valid = true
2016-09-06 18:56:31 +02:00
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Bytes) UnmarshalText(text []byte) error {
if text == nil || len(text) == 0 {
b.Bytes = nil
2016-09-06 18:56:31 +02:00
b.Valid = false
} else {
b.Bytes = append(b.Bytes[0:0], text...)
b.Valid = true
}
return nil
}
// MarshalJSON implements json.Marshaler.
func (b Bytes) MarshalJSON() ([]byte, error) {
if len(b.Bytes) == 0 || b.Bytes == nil {
return NullBytes, nil
2016-09-06 18:56:31 +02:00
}
return b.Bytes, nil
}
// MarshalText implements encoding.TextMarshaler.
func (b Bytes) MarshalText() ([]byte, error) {
if !b.Valid {
return nil, nil
}
return b.Bytes, nil
}
// SetValid changes this Bytes's value and also sets it to be non-null.
func (b *Bytes) SetValid(n []byte) {
b.Bytes = n
b.Valid = true
}
// Ptr returns a pointer to this Bytes's value, or a nil pointer if this Bytes is null.
func (b Bytes) Ptr() *[]byte {
if !b.Valid {
return nil
}
return &b.Bytes
}
// IsZero returns true for null or zero Bytes's, for future omitempty support (Go 1.4?)
func (b Bytes) IsZero() bool {
return !b.Valid
}
// Scan implements the Scanner interface.
2016-11-10 08:47:49 +01:00
func (b *Bytes) Scan(value interface{}) error {
2016-09-06 18:56:31 +02:00
if value == nil {
2016-11-10 08:47:49 +01:00
b.Bytes, b.Valid = []byte{}, false
2016-09-06 18:56:31 +02:00
return nil
}
2016-11-10 08:47:49 +01:00
b.Valid = true
return convert.ConvertAssign(&b.Bytes, value)
2016-09-06 18:56:31 +02:00
}
// Value implements the driver Valuer interface.
2016-11-10 08:47:49 +01:00
func (b Bytes) Value() (driver.Value, error) {
if !b.Valid {
2016-09-06 18:56:31 +02:00
return nil, nil
}
2016-11-10 08:47:49 +01:00
return b.Bytes, nil
2016-09-06 18:56:31 +02:00
}