-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlnull.go
More file actions
59 lines (53 loc) · 1.77 KB
/
Copy pathsqlnull.go
File metadata and controls
59 lines (53 loc) · 1.77 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
58
59
// Package sqlnull provides utilities for working with sql.Null types in Go.
package sqlnull
import (
"database/sql"
"reflect"
)
// From creates a sql.Null[T] value from a given value of type T.
// If the value is nil, it returns a sql.Null[T] with Valid set to false.
// If the value is a pointer and it is nil, it also returns a sql.Null[T] with Valid set to false.
// If the value is a pointer and it is valid, it returns a sql.Null[T] with V set to the pointer.
func From[T any](v T) sql.Null[T] {
val := reflect.ValueOf(v)
if !val.IsValid() {
return sql.Null[T]{Valid: false}
}
if val.Kind() == reflect.Ptr && val.IsNil() {
return sql.Null[T]{Valid: false}
}
return sql.Null[T]{Valid: true, V: v}
}
// FromPtr creates a sql.Null[T] value from a pointer to T.
// If the pointer is nil, it returns a sql.Null[T] with Valid set to false.
// If the pointer is not nil, it dereferences the pointer and returns a sql.Null[T]
// with V set to the dereferenced value.
func FromPtr[T any](v *T) sql.Null[T] {
if v == nil {
return sql.Null[T]{Valid: false}
}
return sql.Null[T]{Valid: true, V: *v}
}
// Ptr creates a pointer to sql.Null[T] value from a given value of type T.
// If the value is nil, it returns a pointer to sql.Null[T] with Valid set to false.
// If need returns (*sql.Null[T])(nil), use PtrOrNil.
func Ptr[T any](v T) *sql.Null[T] {
val := From(v)
return &val
}
// PtrOrNil creates a pointer to sql.Null[T] value from a given value of type T.
// If the value is nil, it returns nil.
func PtrOrNil[T any](v *T) *sql.Null[T] {
if v == nil {
return nil
}
return Ptr(*v)
}
// ValuePtrOrNil retrieves a pointer to the value from sql.Null[T].
// If Valid is false, it returns nil.
func ValuePtrOrNil[T any](v sql.Null[T]) *T {
if !v.Valid {
return nil
}
return &v.V
}