-
Notifications
You must be signed in to change notification settings - Fork 6
/
null.go
51 lines (42 loc) · 819 Bytes
/
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
package pgsql
import (
"database/sql"
"database/sql/driver"
_ "unsafe"
)
func Null[T comparable](v *T) interface {
driver.Valuer
sql.Scanner
} {
return &null[T]{v}
}
type null[T comparable] struct {
value *T
}
func (s *null[T]) Scan(src any) error {
*s.value = *(new(T))
if src == nil {
return nil
}
return convertAssign(s.value, src)
}
func (s null[T]) Value() (driver.Value, error) {
if s.value == nil || isZero(*s.value) || *s.value == *(new(T)) {
return nil, nil
}
if v, ok := any(s.value).(driver.Valuer); ok {
return v.Value()
}
return *s.value, nil
}
type zeroer interface {
IsZero() bool
}
func isZero(v any) bool {
if z, ok := v.(zeroer); ok {
return z.IsZero()
}
return false
}
//go:linkname convertAssign database/sql.convertAssign
func convertAssign(dst, src any) error