lbry.go/extras/null/bool.go

134 lines
2.4 KiB
Go
Raw Permalink Normal View History

2014-09-01 06:46:40 +02:00
package null
import (
2016-11-10 08:47:49 +01:00
"bytes"
"database/sql/driver"
2014-09-01 06:46:40 +02:00
"encoding/json"
"errors"
2016-11-10 08:47:49 +01:00
"gopkg.in/nullbio/null.v6/convert"
2014-09-01 06:46:40 +02:00
)
2015-01-21 03:33:38 +01:00
// Bool is a nullable bool.
2014-09-01 06:46:40 +02:00
type Bool struct {
2016-11-10 08:47:49 +01:00
Bool bool
Valid bool
2014-09-01 06:46:40 +02:00
}
// NewBool creates a new Bool
func NewBool(b bool, valid bool) Bool {
return Bool{
2016-11-10 08:47:49 +01:00
Bool: b,
Valid: valid,
2014-09-01 06:46:40 +02:00
}
}
// BoolFrom creates a new Bool that will always be valid.
2014-09-01 06:46:40 +02:00
func BoolFrom(b bool) Bool {
return NewBool(b, true)
2014-09-01 06:46:40 +02:00
}
2014-09-02 05:06:29 +02:00
// BoolFromPtr creates a new Bool that will be null if f is nil.
2014-09-01 06:46:40 +02:00
func BoolFromPtr(b *bool) Bool {
if b == nil {
return NewBool(false, false)
}
return NewBool(*b, true)
}
// UnmarshalJSON implements json.Unmarshaler.
func (b *Bool) UnmarshalJSON(data []byte) error {
2016-11-10 08:47:49 +01:00
if bytes.Equal(data, NullBytes) {
b.Bool = false
2014-09-01 06:46:40 +02:00
b.Valid = false
return nil
}
2016-11-10 08:47:49 +01:00
if err := json.Unmarshal(data, &b.Bool); err != nil {
return err
}
b.Valid = true
return nil
2014-09-01 06:46:40 +02:00
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (b *Bool) UnmarshalText(text []byte) error {
2016-11-11 08:15:01 +01:00
if text == nil || len(text) == 0 {
2014-09-01 06:46:40 +02:00
b.Valid = false
return nil
2016-11-11 08:15:01 +01:00
}
str := string(text)
switch str {
2014-09-01 06:46:40 +02:00
case "true":
b.Bool = true
case "false":
b.Bool = false
default:
b.Valid = false
return errors.New("invalid input:" + str)
}
b.Valid = true
2014-09-01 06:46:40 +02:00
return nil
}
// MarshalJSON implements json.Marshaler.
func (b Bool) MarshalJSON() ([]byte, error) {
if !b.Valid {
return NullBytes, nil
}
if !b.Bool {
2014-09-01 06:46:40 +02:00
return []byte("false"), nil
}
return []byte("true"), nil
}
// MarshalText implements encoding.TextMarshaler.
func (b Bool) MarshalText() ([]byte, error) {
if !b.Valid {
return []byte{}, nil
}
if !b.Bool {
2014-09-01 06:46:40 +02:00
return []byte("false"), nil
}
return []byte("true"), nil
}
// SetValid changes this Bool's value and also sets it to be non-null.
func (b *Bool) SetValid(v bool) {
b.Bool = v
b.Valid = true
}
// Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null.
2014-09-01 06:46:40 +02:00
func (b Bool) Ptr() *bool {
if !b.Valid {
return nil
}
return &b.Bool
}
// IsNull returns true for invalid Bools, for future omitempty support (Go 1.4?)
func (b Bool) IsNull() bool {
return !b.Valid
2014-09-01 06:46:40 +02:00
}
2016-11-10 08:47:49 +01:00
// Scan implements the Scanner interface.
func (b *Bool) Scan(value interface{}) error {
if value == nil {
b.Bool, b.Valid = false, false
return nil
}
b.Valid = true
return convert.ConvertAssign(&b.Bool, value)
}
// Value implements the driver Valuer interface.
func (b Bool) Value() (driver.Value, error) {
if !b.Valid {
return nil, nil
}
return b.Bool, nil
}