lbry.go/extras/null/float64.go

119 lines
2.4 KiB
Go
Raw Permalink Normal View History

2014-09-01 06:46:40 +02:00
package null
import (
"bytes"
"database/sql/driver"
2014-09-01 06:46:40 +02:00
"encoding/json"
"strconv"
"gopkg.in/nullbio/null.v6/convert"
2014-09-01 06:46:40 +02:00
)
// Float64 is a nullable float64.
type Float64 struct {
Float64 float64
Valid bool
2014-09-01 06:46:40 +02:00
}
// NewFloat64 creates a new Float64
func NewFloat64(f float64, valid bool) Float64 {
return Float64{
Float64: f,
Valid: valid,
2014-09-01 06:46:40 +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
}
// 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 {
return NewFloat64(0, false)
2014-09-01 06:46:40 +02:00
}
return NewFloat64(*f, true)
2014-09-01 06:46:40 +02:00
}
// UnmarshalJSON implements json.Unmarshaler.
func (f *Float64) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, NullBytes) {
f.Float64 = 0
2014-09-01 06:46:40 +02:00
f.Valid = false
return nil
}
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.
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)
f.Valid = err == nil
2014-09-01 06:46:40 +02:00
return err
}
// MarshalJSON implements json.Marshaler.
func (f Float64) MarshalJSON() ([]byte, error) {
2014-09-01 06:46:40 +02:00
if !f.Valid {
return NullBytes, nil
2014-09-01 06:46:40 +02:00
}
return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil
2014-09-01 06:46:40 +02:00
}
// MarshalText implements encoding.TextMarshaler.
func (f Float64) MarshalText() ([]byte, error) {
2014-09-01 06:46:40 +02:00
if !f.Valid {
return []byte{}, nil
2014-09-01 06:46:40 +02:00
}
return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil
2014-09-01 06:46:40 +02:00
}
// SetValid changes this Float64's value and also sets it to be non-null.
func (f *Float64) SetValid(n float64) {
f.Float64 = n
f.Valid = true
}
// 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
}
// IsNull returns true for invalid Float64s, for future omitempty support (Go 1.4?)
func (f Float64) IsNull() bool {
return !f.Valid
2014-09-01 06:46:40 +02: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
}