-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.go
More file actions
85 lines (73 loc) · 1.91 KB
/
Copy pathfloat.go
File metadata and controls
85 lines (73 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package opt
import (
"encoding/json"
"math"
"reflect"
"strconv"
"github.com/coregx/opt/internal"
)
// Float is a nullable float64 with optimized JSON marshaling.
// It rejects Inf and NaN during JSON serialization.
type Float struct {
Option[float64]
}
// NewFloat creates a Float with the given value and validity.
func NewFloat(f float64, valid bool) Float {
return Float{New(f, valid)}
}
// FloatFrom creates a Float that is always valid.
func FloatFrom(f float64) Float {
return Float{From(f)}
}
// FloatFromPtr creates a Float from a pointer. Nil results in null.
func FloatFromPtr(f *float64) Float {
return Float{FromPtr(f)}
}
// FloatOrNull creates a valid Float if f is non-zero, null otherwise.
func FloatOrNull(f float64) Float {
return NewFloat(f, f != 0)
}
// Equal reports whether two Floats are equal.
func (f Float) Equal(other Float) bool {
return Equal(f.Option, other.Option)
}
// MarshalJSON implements json.Marshaler. Rejects Inf and NaN.
func (f Float) MarshalJSON() ([]byte, error) {
if !f.Valid {
return jsonNull, nil
}
if math.IsInf(f.V, 0) || math.IsNaN(f.V) {
return nil, &json.UnsupportedValueError{
Value: reflect.ValueOf(f.V),
Str: strconv.FormatFloat(f.V, 'g', -1, 64),
}
}
return []byte(strconv.FormatFloat(f.V, 'f', -1, 64)), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (f *Float) UnmarshalJSON(data []byte) error {
n, valid, err := internal.UnmarshalFloatJSON(data)
if err != nil {
return err
}
f.V = n
f.Valid = valid
return nil
}
// MarshalText implements encoding.TextMarshaler.
func (f Float) MarshalText() ([]byte, error) {
if !f.Valid {
return []byte{}, nil
}
return []byte(strconv.FormatFloat(f.V, 'f', -1, 64)), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (f *Float) UnmarshalText(text []byte) error {
n, valid, err := internal.UnmarshalFloatText(text)
if err != nil {
return err
}
f.V = n
f.Valid = valid
return nil
}