lbry.go/int.go

125 lines
2.3 KiB
Go
Raw Normal View History

2014-08-30 16:53:25 +02:00
package null
import (
"bytes"
"database/sql/driver"
2014-08-30 16:53:25 +02:00
"encoding/json"
"strconv"
2016-09-07 13:33:21 +02:00
"gopkg.in/nullbio/null.v5/convert"
2014-08-30 16:53:25 +02:00
)
// Int is an nullable int.
2014-08-30 16:53:25 +02:00
type Int struct {
Int int
Valid bool
2014-08-30 16:53:25 +02:00
}
// NewInt creates a new Int
func NewInt(i int, valid bool) Int {
2014-08-30 16:53:25 +02:00
return Int{
Int: i,
Valid: valid,
2014-08-30 16:53:25 +02:00
}
}
// IntFrom creates a new Int that will always be valid.
func IntFrom(i int) Int {
return NewInt(i, true)
2014-08-31 10:28:34 +02:00
}
2014-09-02 02:24:56 +02:00
// IntFromPtr creates a new Int that be null if i is nil.
func IntFromPtr(i *int) Int {
2014-08-31 10:28:34 +02:00
if i == nil {
return NewInt(0, false)
}
return NewInt(*i, true)
2014-08-31 10:28:34 +02:00
}
2014-08-30 16:53:25 +02:00
// UnmarshalJSON implements json.Unmarshaler.
func (i *Int) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, NullBytes) {
2014-08-30 16:53:25 +02:00
i.Valid = false
i.Int = 0
2014-08-30 16:53:25 +02:00
return nil
}
var x int64
if err := json.Unmarshal(data, &x); err != nil {
return err
}
i.Int = int(x)
i.Valid = true
return nil
2014-08-30 16:53:25 +02:00
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (i *Int) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
i.Valid = false
return nil
}
var err error
res, err := strconv.ParseInt(string(text), 10, 0)
i.Valid = err == nil
if i.Valid {
i.Int = int(res)
}
2014-08-30 16:53:25 +02:00
return err
}
// MarshalJSON implements json.Marshaler.
func (i Int) MarshalJSON() ([]byte, error) {
if !i.Valid {
return []byte("null"), nil
2014-08-30 16:53:25 +02:00
}
return []byte(strconv.FormatInt(int64(i.Int), 10)), nil
2014-08-30 16:53:25 +02:00
}
// MarshalText implements encoding.TextMarshaler.
func (i Int) MarshalText() ([]byte, error) {
if !i.Valid {
return []byte{}, nil
2014-08-30 16:53:25 +02:00
}
return []byte(strconv.FormatInt(int64(i.Int), 10)), nil
2014-08-30 16:53:25 +02:00
}
// SetValid changes this Int's value and also sets it to be non-null.
func (i *Int) SetValid(n int) {
i.Int = n
i.Valid = true
}
2014-08-31 10:28:34 +02:00
// Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.
func (i Int) Ptr() *int {
2014-08-30 16:53:25 +02:00
if !i.Valid {
return nil
}
return &i.Int
2014-08-30 16:53:25 +02:00
}
// IsZero returns true for invalid Ints, for future omitempty support (Go 1.4?)
2014-08-30 16:53:25 +02:00
func (i Int) IsZero() bool {
return !i.Valid
2014-08-30 16:53:25 +02:00
}
// Scan implements the Scanner interface.
func (i *Int) Scan(value interface{}) error {
if value == nil {
i.Int, i.Valid = 0, false
return nil
}
i.Valid = true
return convert.ConvertAssign(&i.Int, value)
}
// Value implements the driver Valuer interface.
func (i Int) Value() (driver.Value, error) {
if !i.Valid {
return nil, nil
}
return int64(i.Int), nil
}