2016-05-17 14:02:36 +02:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
2016-11-10 16:00:05 +01:00
|
|
|
"bytes"
|
|
|
|
"database/sql/driver"
|
2016-05-17 14:02:36 +02:00
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
2016-11-10 16:00:05 +01:00
|
|
|
|
2016-11-15 16:23:47 +01:00
|
|
|
"gopkg.in/nullbio/null.v6/convert"
|
2016-05-17 14:02:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Int64 is an nullable int64.
|
|
|
|
type Int64 struct {
|
2016-11-10 16:00:05 +01:00
|
|
|
Int64 int64
|
|
|
|
Valid bool
|
2016-05-17 14:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInt64 creates a new Int64
|
|
|
|
func NewInt64(i int64, valid bool) Int64 {
|
|
|
|
return Int64{
|
2016-11-10 16:00:05 +01:00
|
|
|
Int64: i,
|
|
|
|
Valid: valid,
|
2016-05-17 14:02:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64From creates a new Int64 that will always be valid.
|
|
|
|
func Int64From(i int64) Int64 {
|
|
|
|
return NewInt64(i, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int64FromPtr creates a new Int64 that be null if i is nil.
|
|
|
|
func Int64FromPtr(i *int64) Int64 {
|
|
|
|
if i == nil {
|
|
|
|
return NewInt64(0, false)
|
|
|
|
}
|
|
|
|
return NewInt64(*i, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (i *Int64) UnmarshalJSON(data []byte) error {
|
2016-11-10 16:00:05 +01:00
|
|
|
if bytes.Equal(data, NullBytes) {
|
2016-05-17 14:02:36 +02:00
|
|
|
i.Valid = false
|
2016-11-10 16:00:05 +01:00
|
|
|
i.Int64 = 0
|
2016-05-17 14:02:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-10 16:00:05 +01:00
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &i.Int64); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Valid = true
|
|
|
|
return nil
|
2016-05-17 14:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
func (i *Int64) UnmarshalText(text []byte) error {
|
2016-11-11 08:15:01 +01:00
|
|
|
if text == nil || len(text) == 0 {
|
2016-05-17 14:02:36 +02:00
|
|
|
i.Valid = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
i.Int64, err = strconv.ParseInt(string(text), 10, 64)
|
|
|
|
i.Valid = err == nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (i Int64) MarshalJSON() ([]byte, error) {
|
|
|
|
if !i.Valid {
|
2016-11-10 16:40:50 +01:00
|
|
|
return NullBytes, nil
|
2016-05-17 14:02:36 +02:00
|
|
|
}
|
|
|
|
return []byte(strconv.FormatInt(i.Int64, 10)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
|
|
|
func (i Int64) MarshalText() ([]byte, error) {
|
|
|
|
if !i.Valid {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
return []byte(strconv.FormatInt(i.Int64, 10)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValid changes this Int64's value and also sets it to be non-null.
|
|
|
|
func (i *Int64) SetValid(n int64) {
|
|
|
|
i.Int64 = n
|
|
|
|
i.Valid = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ptr returns a pointer to this Int64's value, or a nil pointer if this Int64 is null.
|
|
|
|
func (i Int64) Ptr() *int64 {
|
|
|
|
if !i.Valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &i.Int64
|
|
|
|
}
|
|
|
|
|
2017-07-06 20:14:43 +02:00
|
|
|
// IsNull returns true for invalid Int64's, for future omitempty support (Go 1.4?)
|
|
|
|
func (i Int64) IsNull() bool {
|
2016-05-17 14:02:36 +02:00
|
|
|
return !i.Valid
|
|
|
|
}
|
2016-11-10 16:00:05 +01:00
|
|
|
|
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (i *Int64) Scan(value interface{}) error {
|
|
|
|
if value == nil {
|
|
|
|
i.Int64, i.Valid = 0, false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
i.Valid = true
|
|
|
|
return convert.ConvertAssign(&i.Int64, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (i Int64) Value() (driver.Value, error) {
|
|
|
|
if !i.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return i.Int64, nil
|
|
|
|
}
|