Skip to content

Commit b9698f8

Browse files
author
Dean Karn
authored
fix slq.Scanner for Option (#39)
1 parent 3070d83 commit b9698f8

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [5.21.3] - 2023-10-11
10+
### Fixed
11+
- Fix SQL Scanner interface not returning None for Option when source data is nil.
12+
913
## [5.21.2] - 2023-07-13
1014
### Fixed
1115
- Updated default form/url.Value encoder/decoder with fix for bubbling up invalid array index values.
@@ -74,7 +78,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7478
### Added
7579
- Added `timext.NanoTime` for fast low level monotonic time with nanosecond precision.
7680

77-
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.21.2...HEAD
81+
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.21.3...HEAD
82+
[5.21.3]: https://github.com/go-playground/pkg/compare/v5.21.2..v5.21.3
7883
[5.21.2]: https://github.com/go-playground/pkg/compare/v5.21.1..v5.21.2
7984
[5.21.1]: https://github.com/go-playground/pkg/compare/v5.21.0..v5.21.1
8085
[5.21.0]: https://github.com/go-playground/pkg/compare/v5.20.0..v5.21.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pkg
22

3-
![Project status](https://img.shields.io/badge/version-5.21.2-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.21.3-green.svg)
44
[![Lint & Test](https://github.com/go-playground/pkg/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/pkg/actions/workflows/go.yml)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
66
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)

values/option/option.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ func (o Option[T]) Value() (driver.Value, error) {
145145
// Scan implements the sql.Scanner interface.
146146
func (o *Option[T]) Scan(value any) error {
147147

148+
if value == nil {
149+
*o = None[T]()
150+
return nil
151+
}
152+
148153
val := reflect.ValueOf(&o.value)
149154

150155
if val.Type().Implements(scanType) {
@@ -156,10 +161,6 @@ func (o *Option[T]) Scan(value any) error {
156161
return nil
157162
}
158163

159-
if value == nil {
160-
*o = None[T]()
161-
return nil
162-
}
163164
val = val.Elem()
164165

165166
switch val.Kind() {

values/option/option_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ type customScanner struct {
166166
}
167167

168168
func (c *customScanner) Scan(src interface{}) error {
169+
if src == nil {
170+
return nil
171+
}
169172
c.S = src.(string)
170173
return nil
171174
}
@@ -242,6 +245,12 @@ func TestSQLScanner(t *testing.T) {
242245
Equal(t, err, nil)
243246
Equal(t, custom, Some(customScanner{S: "GOT HERE"}))
244247

248+
// custom scanner scan nil
249+
var customNil Option[customScanner]
250+
err = customNil.Scan(nil)
251+
Equal(t, err, nil)
252+
Equal(t, customNil, None[customScanner]())
253+
245254
// test unmarshal to struct
246255
type myStruct struct {
247256
Name string `json:"name"`

0 commit comments

Comments
 (0)