Debian Codegraph @

zero/float.go

raw symbols SCIP
1 package zero
2
3 import (
4 "database/sql"
5 "encoding/json"
6 "fmt"
7 "reflect"
8 "strconv"
9 )
10
11 // Float is a nullable float64. Zero input will be considered null.
12 // JSON marshals to zero if null.
13 // Considered null to SQL if zero.
14 type Float struct {
15 sql.NullFloat64
16 }
17
18 // NewFloat creates a new Float
19 func NewFloat(f float64, valid bool) Float {
20 return Float{
21 NullFloat64: sql.NullFloat64{
22 Float64: f,
23 Valid: valid,
24 },
25 }
26 }
27
28 // FloatFrom creates a new Float that will be null if zero.
29 func FloatFrom(f float64) Float {
30 return NewFloat(f, f != 0)
31 }
32
33 // FloatFromPtr creates a new Float that be null if f is nil.
34 func FloatFromPtr(f *float64) Float {
35 if f == nil {
36 return NewFloat(0, false)
37 }
38 return NewFloat(*f, true)
39 }
40
41 // UnmarshalJSON implements json.Unmarshaler.
42 // It supports number and null input.
43 // 0 will be considered a null Float.
44 // It also supports unmarshalling a sql.NullFloat64.
45 func (f *Float) UnmarshalJSON(data []byte) error {
46 var err error
47 var v interface{}
48 if err = json.Unmarshal(data, &v); err != nil {
49 return err
50 }
51 switch x := v.(type) {
52 case float64:
53 f.Float64 = x
54 case map[string]interface{}:
55 err = json.Unmarshal(data, &f.NullFloat64)
56 case nil:
57 f.Valid = false
58 return nil
59 default:
60 err = fmt.Errorf("json: cannot unmarshal %v into Go value of type zero.Float", reflect.TypeOf(v).Name())
61 }
62 f.Valid = (err == nil) && (f.Float64 != 0)
63 return err
64 }
65
66 // UnmarshalText implements encoding.TextUnmarshaler.
67 // It will unmarshal to a null Float if the input is a blank, zero, or not a float.
68 // It will return an error if the input is not a float, blank, or "null".
69 func (f *Float) UnmarshalText(text []byte) error {
70 str := string(text)
71 if str == "" || str == "null" {
72 f.Valid = false
73 return nil
74 }
75 var err error
76 f.Float64, err = strconv.ParseFloat(string(text), 64)
77 f.Valid = (err == nil) && (f.Float64 != 0)
78 return err
79 }
80
81 // MarshalJSON implements json.Marshaler.
82 // It will encode null if this Float is null.
83 func (f Float) MarshalJSON() ([]byte, error) {
84 n := f.Float64
85 if !f.Valid {
86 n = 0
87 }
88 return []byte(strconv.FormatFloat(n, 'f', -1, 64)), nil
89 }
90
91 // MarshalText implements encoding.TextMarshaler.
92 // It will encode a zero if this Float is null.
93 func (f Float) MarshalText() ([]byte, error) {
94 n := f.Float64
95 if !f.Valid {
96 n = 0
97 }
98 return []byte(strconv.FormatFloat(n, 'f', -1, 64)), nil
99 }
100
101 // SetValid changes this Float's value and also sets it to be non-null.
102 func (f *Float) SetValid(v float64) {
103 f.Float64 = v
104 f.Valid = true
105 }
106
107 // Ptr returns a poFloater to this Float's value, or a nil poFloater if this Float is null.
108 func (f Float) Ptr() *float64 {
109 if !f.Valid {
110 return nil
111 }
112 return &f.Float64
113 }
114
115 // IsZero returns true for null or zero Floats, for future omitempty support (Go 1.4?)
116 func (f Float) IsZero() bool {
117 return !f.Valid || f.Float64 == 0
118 }