-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathint.go
97 lines (73 loc) · 2.53 KB
/
int.go
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
86
87
88
89
90
91
92
93
94
95
96
97
package null
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"golang.org/x/exp/constraints"
)
// Int is an int that will write as null when it is zero both to databases and JSON
// null values when unmarshalled or scanned from a DB will result in a zero value.
type Int int
// NullInt is our constant for an Int value that will be written as null
const NullInt = Int(0)
// Scan implements the Scanner interface
func (i *Int) Scan(value any) error { return ScanInt(value, i) }
// Value implements the Valuer interface
func (i Int) Value() (driver.Value, error) { return IntValue(i) }
// UnmarshalJSON implements the Unmarshaller interface
func (i *Int) UnmarshalJSON(b []byte) error { return UnmarshalInt(b, i) }
// MarshalJSON implements the Marshaller interface
func (i Int) MarshalJSON() ([]byte, error) { return MarshalInt(i) }
// Int64 is an int64 that will write as null when it is zero both to databases and JSON
// null values when unmarshalled or scanned from a DB will result in a zero value.
type Int64 int64
// NullInt64 is our constant for an Int64 value that will be written as null
const NullInt64 = Int64(0)
// Scan implements the Scanner interface
func (i *Int64) Scan(value any) error { return ScanInt(value, i) }
// Value implements the Valuer interface
func (i Int64) Value() (driver.Value, error) { return IntValue(i) }
// UnmarshalJSON implements the Unmarshaller interface
func (i *Int64) UnmarshalJSON(b []byte) error { return UnmarshalInt(b, i) }
// MarshalJSON implements the Marshaller interface
func (i Int64) MarshalJSON() ([]byte, error) { return MarshalInt(i) }
// ScanInt scans a nullable INT into an int type, using zero for NULL.
func ScanInt[T constraints.Signed](value any, i *T) error {
ni := sql.NullInt64{}
if err := ni.Scan(value); err != nil {
return err
}
if !ni.Valid {
*i = T(0)
return nil
}
*i = T(ni.Int64)
return nil
}
// IntValue converts an int type value to NULL if it is zero.
func IntValue[T constraints.Signed](i T) (driver.Value, error) {
if i == 0 {
return nil, nil
}
return int64(i), nil
}
// UnmarshalInt unmarshals an int type from JSON, using zero for null.
func UnmarshalInt[T constraints.Signed](b []byte, i *T) error {
var val *int64
if err := json.Unmarshal(b, &val); err != nil {
return err
}
if val == nil {
*i = 0
return nil
}
*i = T(*val)
return nil
}
// MarshalJSON marshals an int type to JSON, using null for zero.
func MarshalInt[T constraints.Signed](i T) ([]byte, error) {
if i == 0 {
return json.Marshal(nil)
}
return json.Marshal(int64(i))
}