-
Notifications
You must be signed in to change notification settings - Fork 3
/
nulluint16.go
91 lines (77 loc) · 1.6 KB
/
nulluint16.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
package nulltype
import (
"bytes"
"database/sql/driver"
"strconv"
"unsafe"
jsoniter "github.com/json-iterator/go"
)
/* SQL and JSon null.Uint16 */
type Uint16 struct {
Uint16 uint16
Valid bool // Valid is true if Uint16 is not NULL
}
func NewUint16(i uint16) Uint16 {
n := Uint16{}
n.Valid = true
n.Uint16 = i
return n
}
func (ni *Uint16) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
val := (*Uint16)(ptr)
if val.Valid {
stream.WriteVal(val.Uint16)
} else {
stream.WriteVal(nil)
}
}
// IsEmpty detect whether primitive.ObjectID is empty.
func (ni *Uint16) IsEmpty(ptr unsafe.Pointer) bool {
val := (*Uint16)(ptr)
return !val.Valid
}
func (ni *Uint16) UnmarshalCSV(b string) error {
uitmp, err := strconv.ParseUint(b, 10, 16)
ni.Uint16 = uint16(uitmp)
return err
}
// MarshalCSV marshals CSV
func (ni Uint16) MarshalCSV() (string, error) {
if ni.Valid {
return strconv.FormatUint(uint64(ni.Uint16), 10), nil
}
return "", nil
}
func (ni *Uint16) UnmarshalJSON(b []byte) error {
var i uint16
if err := json.Unmarshal(b, &i); err != nil {
return err
}
if bytes.Equal(b, []byte("null")) {
ni.Valid = false
return nil
}
ni.Uint16 = i
ni.Valid = true
return nil
}
func (ni Uint16) MarshalJSON() ([]byte, error) {
if ni.Valid {
return json.Marshal(ni.Uint16)
}
return json.Marshal(nil)
}
func (ni *Uint16) Scan(value any) error {
if value == nil {
ni.Uint16, ni.Valid = 0, false
return nil
}
ni.Valid = true
return convertAssignRows(&ni.Uint16, value)
}
func (ni Uint16) Value() (driver.Value, error) {
if !ni.Valid {
return nil, nil
}
return ni.Uint16, nil
}