-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
173 lines (141 loc) · 4.46 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2023 Toru Maesaka
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package nullable
import (
"database/sql"
"database/sql/driver"
"strconv"
)
// Int64 is a type alias against the standard sql.NullInt64 type.
type Int64 sql.NullInt64
// NewInt64 returns an Int64 populated with the given int64.
func NewInt64(value int64) Int64 {
return Int64{Int64: value, Valid: true}
}
// Scan wraps the standard Scan function, which implements the Scanner interface.
func (i *Int64) Scan(value any) error {
ni := sql.NullInt64(*i)
if err := ni.Scan(value); err != nil {
return err
}
*i = Int64(ni)
return nil
}
// Value wraps the standard Value function, which implements the driver Valuer interface.
func (i Int64) Value() (driver.Value, error) {
return sql.NullInt64(i).Value()
}
// Null returns true if the underlying value is NULL.
func (i Int64) Null() bool {
return !i.Valid
}
// Nil is an alias for Null() for those that prefer a more Go-like syntax.
func (i Int64) Nil() bool {
return i.Null()
}
// HexString returns a hexadecimal string representation of the underlying value.
func (i Int64) HexString() string {
if !i.Valid {
return ""
}
return intToHexString(i.Int64)
}
// Int32 is a type alias against the standard sql.NullInt32 type.
type Int32 sql.NullInt32
// NewInt32 returns an Int64 populated with the given int32.
func NewInt32(value int32) Int32 {
return Int32{Int32: value, Valid: true}
}
// Scan wraps the standard Scan function, which implements the Scanner interface.
func (i *Int32) Scan(value any) error {
ni := sql.NullInt32(*i)
if err := ni.Scan(value); err != nil {
return err
}
*i = Int32(ni)
return nil
}
// Value wraps the standard Value function, which implements the driver Valuer interface.
func (i Int32) Value() (driver.Value, error) {
return sql.NullInt32(i).Value()
}
// Null returns true if the underlying value is NULL.
func (i Int32) Null() bool {
return !i.Valid
}
// Nil is an alias for Null() for those that prefer a more Go-like syntax.
func (i Int32) Nil() bool {
return i.Null()
}
// HexString returns a hexadecimal string representation of the underlying value.
func (i Int32) HexString() string {
if !i.Valid {
return ""
}
return intToHexString(i.Int32)
}
// Int16 is a type alias against the standard sql.NullInt16 type.
type Int16 sql.NullInt16
// NewInt16 returns an Int16 populated with the given int16.
func NewInt16(value int16) Int16 {
return Int16{Int16: value, Valid: true}
}
// Scan wraps the standard Scan function, which implements the Scanner interface.
func (i *Int16) Scan(value any) error {
ni := sql.NullInt16(*i)
if err := ni.Scan(value); err != nil {
return err
}
*i = Int16(ni)
return nil
}
// Value wraps the standard Value function, which implements the driver Valuer interface.
func (i Int16) Value() (driver.Value, error) {
return sql.NullInt16(i).Value()
}
// Null returns true if the underlying value is NULL.
func (i Int16) Null() bool {
return !i.Valid
}
// Nil is an alias for Null() for those that prefer a more Go-like syntax.
func (i Int16) Nil() bool {
return i.Null()
}
// HexString returns a hexadecimal string representation of the underlying value.
func (i Int16) HexString() string {
if !i.Valid {
return ""
}
return intToHexString(i.Int16)
}
func intToHexString(value any) string {
var src int64
switch v := value.(type) {
case int64:
src = v
case int32:
src = int64(v)
case int16:
src = int64(v)
default:
return ""
}
return strconv.FormatInt(src, 16)
}