fix Scanner implementation

This commit is contained in:
Pavel Merzlyakov 2022-10-03 17:42:28 +03:00
parent 99ccac5948
commit 35e0da91ca
1 changed files with 21 additions and 8 deletions

29
sql.go
View File

@ -2,23 +2,36 @@ package versioning
import ( import (
"database/sql/driver" "database/sql/driver"
"strconv"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func (v *Version) Scan(value interface{}) error { func (v *Version) Scan(value interface{}) error {
switch code := value.(type) { var code uint32
case int64:
parsed, err := ParseFromCode(uint32(code)) switch val := value.(type) {
if err != nil { case []byte:
return err c, err := strconv.ParseUint(string(val), 10, 32)
} if err != nil {
return errors.WithStack(err)
}
code = uint32(c)
case int64:
code = uint32(val)
*v = parsed
return nil
default: default:
return errors.Errorf("incompatible version code type - %T", value) return errors.Errorf("incompatible version code type - %T", value)
} }
parsed, err := ParseFromCode(code)
if err != nil {
return err
}
*v = parsed
return nil
} }
func (v Version) Value() (driver.Value, error) { func (v Version) Value() (driver.Value, error) {