2014-09-01 06:46:40 +02:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
2016-11-10 16:00:05 +01:00
|
|
|
"bytes"
|
|
|
|
"database/sql/driver"
|
2014-09-01 06:46:40 +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"
|
2014-09-01 06:46:40 +02:00
|
|
|
)
|
|
|
|
|
2016-05-17 14:02:36 +02:00
|
|
|
// Float64 is a nullable float64.
|
|
|
|
type Float64 struct {
|
2016-11-10 16:00:05 +01:00
|
|
|
Float64 float64
|
|
|
|
Valid bool
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 14:02:36 +02:00
|
|
|
// NewFloat64 creates a new Float64
|
|
|
|
func NewFloat64(f float64, valid bool) Float64 {
|
|
|
|
return Float64{
|
2016-11-10 16:00:05 +01:00
|
|
|
Float64: f,
|
|
|
|
Valid: valid,
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 14:02:36 +02:00
|
|
|
// Float64From creates a new Float64 that will always be valid.
|
|
|
|
func Float64From(f float64) Float64 {
|
|
|
|
return NewFloat64(f, true)
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 14:02:36 +02:00
|
|
|
// Float64FromPtr creates a new Float64 that be null if f is nil.
|
|
|
|
func Float64FromPtr(f *float64) Float64 {
|
2014-09-01 06:46:40 +02:00
|
|
|
if f == nil {
|
2016-05-17 14:02:36 +02:00
|
|
|
return NewFloat64(0, false)
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
2016-05-17 14:02:36 +02:00
|
|
|
return NewFloat64(*f, true)
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2016-05-17 14:02:36 +02:00
|
|
|
func (f *Float64) UnmarshalJSON(data []byte) error {
|
2016-11-10 16:00:05 +01:00
|
|
|
if bytes.Equal(data, NullBytes) {
|
|
|
|
f.Float64 = 0
|
2014-09-01 06:46:40 +02:00
|
|
|
f.Valid = false
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-10 16:00:05 +01:00
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &f.Float64); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Valid = true
|
|
|
|
return nil
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
2016-05-17 14:02:36 +02:00
|
|
|
func (f *Float64) 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
|
|
|
f.Valid = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
f.Float64, err = strconv.ParseFloat(string(text), 64)
|
2014-09-01 20:22:17 +02:00
|
|
|
f.Valid = err == nil
|
2014-09-01 06:46:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
2016-05-17 14:02:36 +02:00
|
|
|
func (f Float64) MarshalJSON() ([]byte, error) {
|
2014-09-01 06:46:40 +02:00
|
|
|
if !f.Valid {
|
2016-11-10 16:40:50 +01:00
|
|
|
return NullBytes, nil
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
2014-09-01 20:22:17 +02:00
|
|
|
return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText implements encoding.TextMarshaler.
|
2016-05-17 14:02:36 +02:00
|
|
|
func (f Float64) MarshalText() ([]byte, error) {
|
2014-09-01 06:46:40 +02:00
|
|
|
if !f.Valid {
|
2014-09-01 20:22:17 +02:00
|
|
|
return []byte{}, nil
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
2014-09-01 20:22:17 +02:00
|
|
|
return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 14:02:36 +02:00
|
|
|
// SetValid changes this Float64's value and also sets it to be non-null.
|
|
|
|
func (f *Float64) SetValid(n float64) {
|
2014-09-01 20:22:17 +02:00
|
|
|
f.Float64 = n
|
2014-09-01 07:25:57 +02:00
|
|
|
f.Valid = true
|
|
|
|
}
|
|
|
|
|
2016-05-17 14:02:36 +02:00
|
|
|
// Ptr returns a pointer to this Float64's value, or a nil pointer if this Float64 is null.
|
|
|
|
func (f Float64) Ptr() *float64 {
|
2014-09-01 06:46:40 +02:00
|
|
|
if !f.Valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &f.Float64
|
|
|
|
}
|
|
|
|
|
2017-07-06 20:14:43 +02:00
|
|
|
// IsNull returns true for invalid Float64s, for future omitempty support (Go 1.4?)
|
|
|
|
func (f Float64) IsNull() bool {
|
2014-09-01 20:22:17 +02:00
|
|
|
return !f.Valid
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
2016-11-10 16:00:05 +01:00
|
|
|
|
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
func (f *Float64) Scan(value interface{}) error {
|
|
|
|
if value == nil {
|
|
|
|
f.Float64, f.Valid = 0, false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
f.Valid = true
|
|
|
|
return convert.ConvertAssign(&f.Float64, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
func (f Float64) Value() (driver.Value, error) {
|
|
|
|
if !f.Valid {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return f.Float64, nil
|
|
|
|
}
|