This repository was archived by the owner on Jul 25, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +10
-4
lines changed Expand file tree Collapse file tree 2 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -14,16 +14,22 @@ type Null[T comparable] struct {
14
14
}
15
15
16
16
func (n * Null [T ]) Scan (value any ) (err error ) {
17
+ defer func () {
18
+ if r := recover (); r != nil {
19
+ err = errors .From (r )
20
+ }
21
+ }()
22
+
17
23
xType := reflect .TypeOf (n .Data )
18
24
19
25
xValue := reflect .ValueOf (value )
20
26
xValueType := xValue .Type ()
21
27
22
- if ! xValueType .AssignableTo (xType ) {
28
+ if ! xValueType .AssignableTo (xType ) && ! xValueType . ConvertibleTo ( xType ) {
23
29
return errors .New ("Invalid value" )
24
30
}
25
31
26
- n .Data = xValue .Interface ().(T )
32
+ n .Data = xValue .Convert ( xType ). Interface ().(T )
27
33
n .Valid = ! xValue .IsZero ()
28
34
29
35
return
Original file line number Diff line number Diff line change @@ -121,12 +121,12 @@ func (s *DBOTestSuite) TestNullString() {
121
121
func (s * DBOTestSuite ) TestNullUint () {
122
122
var data dbo.Null [uint ]
123
123
124
- data .Scan (uint ( 200 ) )
124
+ data .Scan (200 )
125
125
126
126
assert .Equal (s .T (), data .Data , uint (200 ))
127
127
assert .True (s .T (), data .Valid )
128
128
129
- data .Scan (uint ( 0 ) )
129
+ data .Scan (0 )
130
130
131
131
assert .Equal (s .T (), data .Data , uint (0 ))
132
132
assert .False (s .T (), data .Valid )
You can’t perform that action at this time.
0 commit comments