versioning/sql.go

40 lines
625 B
Go
Raw Permalink Normal View History

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