From 4855e19fb3d76b970f6b74c8144a82c0b3299355 Mon Sep 17 00:00:00 2001 From: Levente Batuska Date: Sun, 5 Jan 2025 22:23:51 +0100 Subject: [PATCH] fix(Type): make sure when checking if an inner type is Scanner we use pointers --- type/optional.go | 11 +++++++++-- type/result.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/type/optional.go b/type/optional.go index a6e0f77..19e17b4 100644 --- a/type/optional.go +++ b/type/optional.go @@ -125,8 +125,15 @@ func (opt *Optional[T]) Scan(src interface{}) error { return nil } - // If T is a scanner - if scanner, ok := any(opt.value).(sql.Scanner); ok { + // If T is a scanner (Scan is usually implemented on pointers so we need a pointer) + var scanner sql.Scanner = nil + var ok bool = false + if reflect.TypeOf(opt.value).Kind() == reflect.Ptr { + scanner, ok = any(opt.value).(sql.Scanner) + } else { + scanner, ok = any(&opt.value).(sql.Scanner) + } + if ok { if err := scanner.Scan(src); err != nil { return err } diff --git a/type/result.go b/type/result.go index 57a8d72..9402769 100644 --- a/type/result.go +++ b/type/result.go @@ -151,8 +151,15 @@ func (res *Result[T]) Scan(src interface{}) error { return nil } - // If T is a scanner - if scanner, ok := any(res.value).(sql.Scanner); ok { + // If T is a scanner (Scan is usually implemented on pointers so we need a pointer) + var scanner sql.Scanner = nil + var ok bool = false + if reflect.TypeOf(res.value).Kind() == reflect.Ptr { + scanner, ok = any(res.value).(sql.Scanner) + } else { + scanner, ok = any(&res.value).(sql.Scanner) + } + if ok { if err := scanner.Scan(src); err != nil { return err }