-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnull.go
61 lines (48 loc) · 1.23 KB
/
null.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
package sqle
import (
"database/sql"
"database/sql/driver"
"encoding/json"
)
var nullJsonBytes = []byte("null")
const nullJson = "null"
type Null[T any] struct {
sql.Null[T]
}
func NewNull[T any](v T, valid bool) Null[T] {
return Null[T]{Null: sql.Null[T]{V: v, Valid: valid}}
}
// Scan implements the [sql.Scanner] interface.
func (t *Null[T]) Scan(value any) error { // skipcq: GO-W1029
return t.Null.Scan(value)
}
// Value implements the [driver.Valuer] interface.
func (t Null[T]) Value() (driver.Value, error) { // skipcq: GO-W1029
return t.Null.Value()
}
// TValue returns the underlying value of the Null struct.
func (t *Null[T]) TValue() T { // skipcq: GO-W1029
return t.Null.V
}
// MarshalJSON implements the json.Marshaler interface
func (t Null[T]) MarshalJSON() ([]byte, error) { // skipcq: GO-W1029
if t.Valid {
return json.Marshal(t.Null.V)
}
return nullJsonBytes, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *Null[T]) UnmarshalJSON(data []byte) error { // skipcq: GO-W1029
if len(data) == 0 || string(data) == nullJson {
t.Null.Valid = false
return nil
}
var v T
err := json.Unmarshal(data, &v)
if err != nil {
return err
}
t.Null.V = v
t.Null.Valid = true
return nil
}