Skip to content

Commit 0876f62

Browse files
authored
Merge pull request #44 from majewsky/eliminate-convertassign
eliminate usage of go:linkname with convertAssign
2 parents 7f202ad + 98f0db5 commit 0876f62

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

convert_assign.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/moznion/go-optional
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/mattn/go-sqlite3 v1.14.22

sql_driver.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package optional
22

33
import (
4+
"database/sql"
45
"database/sql/driver"
56
)
67

78
// Scan assigns a value from a database driver.
89
// This method is required from database/sql.Scanner interface.
910
func (o *Option[T]) Scan(src any) error {
10-
if src == nil {
11-
*o = None[T]()
12-
return nil
13-
}
14-
15-
var v T
16-
err := sqlConvertAssign(&v, src)
11+
// The detour through sql.Null[T] allows us to access the standard rules for
12+
// assigning scanned values into builtin types and std types like *sql.Rows,
13+
// which are not exported from std directly.
14+
var v sql.Null[T]
15+
err := v.Scan(src)
1716
if err != nil {
1817
return err
1918
}
20-
21-
*o = Some[T](v)
19+
if v.Valid {
20+
*o = Some[T](v.V)
21+
} else {
22+
*o = None[T]()
23+
}
2224
return nil
2325
}
2426

0 commit comments

Comments
 (0)