Debian Codegraph @

zero/float.go

raw symbols SCIP
1 package zero
2
3 import (
4 "database/sql"
5 "encoding/json"
6 "fmt"
7 "math"
8 "reflect"
9 "strconv"
10 )
11
12 // Float is a nullable float64. Zero input will be considered null.
13 // JSON marshals to zero if null.
14 // Considered null to SQL if zero.
15 type Float struct {
16 sql.NullFloat64
17 }
18
19 // NewFloat creates a new Float
20 func NewFloat(f float64, valid bool) Float {
21 return Float{
22 NullFloat64: sql.NullFloat64{
23 Float64: f,
24 Valid: valid,
25 },
26 }
27 }
28
29 // FloatFrom creates a new Float that will be null if zero.
30 func FloatFrom(f float64) Float {
31 return NewFloat(f, f != 0)
32 }
33
34 // FloatFromPtr creates a new Float that be null if f is nil.
35 func FloatFromPtr(f *float64) Float {
36 if f == nil {
37 return NewFloat(0, false)
38 }
39 return NewFloat(*f, true)
40 }
41
42 // ValueOrZero returns the inner value if valid, otherwise zero.
43 func (f Float) ValueOrZero() float64 {
44 if !f.Valid {
45 return 0
46 }
47 return f.Float64
48 }
49
50 // UnmarshalJSON implements json.Unmarshaler.
51 // It supports number and null input.
52 // 0 will be considered a null Float.
53 // It also supports unmarshalling a sql.NullFloat64.
54 func (f *Float) UnmarshalJSON(data []byte) error {
55 var err error
56 var v interface{}
57 if err = json.Unmarshal(data, &v); err != nil {
58 return err
59 }
60 switch x := v.(type) {
61 case float64:
62 f.Float64 = x
63 case string:
64 str := string(x)
65 if len(str) == 0 {
66 f.Valid = false
67 return nil
68 }
69 f.Float64, err = strconv.ParseFloat(str, 64)
70 case map[string]interface{}:
71 err = json.Unmarshal(data, &f.NullFloat64)
72 case nil:
73 f.Valid = false
74 return nil
75 default:
76 err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Float", reflect.TypeOf(v).Name())
77 }
78 f.Valid = (err == nil) && (f.Float64 != 0)
79 return err
80 }
81
82 // UnmarshalText implements encoding.TextUnmarshaler.
83 // It will unmarshal to a null Float if the input is a blank, zero, or not a float.
84 // It will return an error if the input is not a float, blank, or "null".
85 func (f *Float) UnmarshalText(text []byte) error {
86 str := string(text)
87 if str == "" || str == "null" {
88 f.Valid = false
89 return nil
90 }
91 var err error
92 f.Float64, err = strconv.ParseFloat(string(text), 64)
93 f.Valid = (err == nil) && (f.Float64 != 0)
94 return err
95 }
96
97 // MarshalJSON implements json.Marshaler.
98 // It will encode null if this Float is null.
99 func (f Float) MarshalJSON() ([]byte, error) {
100 n := f.Float64
101 if !f.Valid {
102 n = 0
103 }
104 if math.IsInf(f.Float64, 0) || math.IsNaN(f.Float64) {
105 return nil, &json.UnsupportedValueError{
106 Value: reflect.ValueOf(f.Float64),
107 Str: strconv.FormatFloat(f.Float64, 'g', -1, 64),
108 }
109 }
110 return []byte(strconv.FormatFloat(n, 'f', -1, 64)), nil
111 }
112
113 // MarshalText implements encoding.TextMarshaler.
114 // It will encode a zero if this Float is null.
115 func (f Float) MarshalText() ([]byte, error) {
116 n := f.Float64
117 if !f.Valid {
118 n = 0
119 }
120 return []byte(strconv.FormatFloat(n, 'f', -1, 64)), nil
121 }
122
123 // SetValid changes this Float's value and also sets it to be non-null.
124 func (f *Float) SetValid(v float64) {
125 f.Float64 = v
126 f.Valid = true
127 }
128
129 // Ptr returns a poFloater to this Float's value, or a nil poFloater if this Float is null.
130 func (f Float) Ptr() *float64 {
131 if !f.Valid {
132 return nil
133 }
134 return &f.Float64
135 }
136
137 // IsZero returns true for null or zero Floats, for future omitempty support (Go 1.4?)
138 func (f Float) IsZero() bool {
139 return !f.Valid || f.Float64 == 0
140 }
141
142 // Equal returns true if both floats have the same value or are both either null or zero.
143 // Warning: calculations using floating point numbers can result in different ways
144 // the numbers are stored in memory. Therefore, this function is not suitable to
145 // compare the result of a calculation. Use this method only to check if the value
146 // has changed in comparison to some previous value.
147 func (f Float) Equal(other Float) bool {
148 return f.ValueOrZero() == other.ValueOrZero()
149 }