From a9e7e9c26ef78f336e9b196e0cae1d9bc26c5846 Mon Sep 17 00:00:00 2001 From: dadang Date: Tue, 16 Jan 2024 11:28:43 +0700 Subject: [PATCH] fix : null int parsing error --- int.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/int.go b/int.go index ef4d803..9b8876d 100644 --- a/int.go +++ b/int.go @@ -4,6 +4,7 @@ import ( "bytes" "database/sql/driver" "errors" + "strconv" "github.com/goccy/go-json" ) @@ -31,12 +32,37 @@ func (ni *NullInt) Scan(value interface{}) error { return nil } - b, ok := value.(int) - if !ok { - return errors.New("type assertion to int is failed") + var res int + + switch b := value.(type) { + case int8: + + res = int(b) + + case int16: + + res = int(b) + + case int32: + + res = int(b) + + case int64: + + res = int(b) + + case []byte: + + a, err := strconv.Atoi(string(b)) + if err != nil { + return errors.New("type assertion to int is failed") + } + + res = a + } - ni.Int, ni.Valid = b, true + ni.Int, ni.Valid = res, true return nil }