Skip to content

Commit

Permalink
fix : null int parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
dadang committed Jan 16, 2024
1 parent ad3e37c commit a9e7e9c
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"database/sql/driver"
"errors"
"strconv"

"github.com/goccy/go-json"
)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit a9e7e9c

Please sign in to comment.