-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnull.go
More file actions
57 lines (49 loc) · 1.3 KB
/
null.go
File metadata and controls
57 lines (49 loc) · 1.3 KB
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
package nullable
import (
"database/sql/driver"
)
// An Nullable represents a value that may or may not exist.
//
// The zero value represents a non-existent value.
type Nullable[T any] struct {
// FIXME: Valid and Val are too similar
Val T // What should this be called?
Valid bool
}
// New creates a new Nullable without a value.
func Empty[T any]() Nullable[T] {
var empty T
return Nullable[T]{empty, false}
}
// Of creates a new Nullable with a value.
func Of[T any](val T) Nullable[T] {
return Nullable[T]{val, true}
}
// It's invalid to use the returned value if the bool is false.
func (n Nullable[T]) Get() (T, bool) {
return n.Val, n.Valid
}
// Scan implements the Scanner interface.
func (n *Nullable[T]) Scan(value any) error {
if value == nil {
var empty T
n.Valid = false
n.Val = empty
return nil
}
n.Valid = true
return convertAssign(&n.Val, value)
// TODO: Figure out why NullByte uses a different patter for setting Valid
// https://github.com/golang/go/blob/master/src/database/sql/sql.go#L304
//
// err := convertAssign(&n.Val, value)
// n.Valid = err == nil
// return err
}
// Value implements the Valuer interface.
func (n Nullable[T]) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return driver.DefaultParameterConverter.ConvertValue(any(n.Val))
}