Convert bool to Codec
This commit is contained in:
+221
-121
@@ -7,136 +7,27 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type BoolScanner interface {
|
||||
ScanBool(v bool, valid bool) error
|
||||
}
|
||||
|
||||
type Bool struct {
|
||||
Bool bool
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func (dst *Bool) Set(src interface{}) error {
|
||||
if src == nil {
|
||||
// ScanBool implements the BoolScanner interface.
|
||||
func (dst *Bool) ScanBool(v bool, valid bool) error {
|
||||
if !valid {
|
||||
*dst = Bool{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if value, ok := src.(interface{ Get() interface{} }); ok {
|
||||
value2 := value.Get()
|
||||
if value2 != value {
|
||||
return dst.Set(value2)
|
||||
}
|
||||
}
|
||||
|
||||
switch value := src.(type) {
|
||||
case bool:
|
||||
*dst = Bool{Bool: value, Valid: true}
|
||||
case string:
|
||||
bb, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*dst = Bool{Bool: bb, Valid: true}
|
||||
case *bool:
|
||||
if value == nil {
|
||||
*dst = Bool{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
case *string:
|
||||
if value == nil {
|
||||
*dst = Bool{}
|
||||
} else {
|
||||
return dst.Set(*value)
|
||||
}
|
||||
default:
|
||||
if originalSrc, ok := underlyingBoolType(src); ok {
|
||||
return dst.Set(originalSrc)
|
||||
}
|
||||
return fmt.Errorf("cannot convert %v to Bool", value)
|
||||
}
|
||||
*dst = Bool{Bool: v, Valid: true}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst Bool) Get() interface{} {
|
||||
if !dst.Valid {
|
||||
return nil
|
||||
}
|
||||
|
||||
return dst.Bool
|
||||
}
|
||||
|
||||
func (src *Bool) AssignTo(dst interface{}) error {
|
||||
if !src.Valid {
|
||||
return NullAssignTo(dst)
|
||||
}
|
||||
|
||||
switch v := dst.(type) {
|
||||
case *bool:
|
||||
*v = src.Bool
|
||||
return nil
|
||||
default:
|
||||
if nextDst, retry := GetAssignToDstType(dst); retry {
|
||||
return src.AssignTo(nextDst)
|
||||
}
|
||||
return fmt.Errorf("unable to assign to %T", dst)
|
||||
}
|
||||
}
|
||||
|
||||
func (dst *Bool) DecodeText(ci *ConnInfo, src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Bool{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(src) != 1 {
|
||||
return fmt.Errorf("invalid length for bool: %v", len(src))
|
||||
}
|
||||
|
||||
*dst = Bool{Bool: src[0] == 't', Valid: true}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dst *Bool) DecodeBinary(ci *ConnInfo, src []byte) error {
|
||||
if src == nil {
|
||||
*dst = Bool{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(src) != 1 {
|
||||
return fmt.Errorf("invalid length for bool: %v", len(src))
|
||||
}
|
||||
|
||||
*dst = Bool{Bool: src[0] == 1, Valid: true}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src Bool) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||
if !src.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if src.Bool {
|
||||
buf = append(buf, 't')
|
||||
} else {
|
||||
buf = append(buf, 'f')
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (src Bool) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||
if !src.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if src.Bool {
|
||||
buf = append(buf, 1)
|
||||
} else {
|
||||
buf = append(buf, 0)
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// Scan implements the database/sql Scanner interface.
|
||||
func (dst *Bool) Scan(src interface{}) error {
|
||||
if src == nil {
|
||||
@@ -149,11 +40,19 @@ func (dst *Bool) Scan(src interface{}) error {
|
||||
*dst = Bool{Bool: src, Valid: true}
|
||||
return nil
|
||||
case string:
|
||||
return dst.DecodeText(nil, []byte(src))
|
||||
b, err := strconv.ParseBool(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*dst = Bool{Bool: b, Valid: true}
|
||||
return nil
|
||||
case []byte:
|
||||
srcCopy := make([]byte, len(src))
|
||||
copy(srcCopy, src)
|
||||
return dst.DecodeText(nil, srcCopy)
|
||||
b, err := strconv.ParseBool(string(src))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*dst = Bool{Bool: b, Valid: true}
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot scan %T", src)
|
||||
@@ -195,3 +94,204 @@ func (dst *Bool) UnmarshalJSON(b []byte) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type BoolCodec struct{}
|
||||
|
||||
func (BoolCodec) FormatSupported(format int16) bool {
|
||||
return format == TextFormatCode || format == BinaryFormatCode
|
||||
}
|
||||
|
||||
func (BoolCodec) PreferredFormat() int16 {
|
||||
return BinaryFormatCode
|
||||
}
|
||||
|
||||
func (BoolCodec) Encode(ci *ConnInfo, oid uint32, format int16, value interface{}, buf []byte) (newBuf []byte, err error) {
|
||||
v, valid, err := convertToBoolForEncode(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot convert %v to bool: %v", value, err)
|
||||
}
|
||||
if !valid {
|
||||
return nil, nil
|
||||
}
|
||||
if value == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch format {
|
||||
case BinaryFormatCode:
|
||||
if v {
|
||||
buf = append(buf, 1)
|
||||
} else {
|
||||
buf = append(buf, 0)
|
||||
}
|
||||
return buf, nil
|
||||
case TextFormatCode:
|
||||
if v {
|
||||
buf = append(buf, 't')
|
||||
} else {
|
||||
buf = append(buf, 'f')
|
||||
}
|
||||
return buf, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown format code: %v", format)
|
||||
}
|
||||
}
|
||||
|
||||
func (BoolCodec) PlanScan(ci *ConnInfo, oid uint32, format int16, target interface{}, actualTarget bool) ScanPlan {
|
||||
|
||||
switch format {
|
||||
case BinaryFormatCode:
|
||||
switch target.(type) {
|
||||
case *bool:
|
||||
return scanPlanBinaryBoolToBool{}
|
||||
case BoolScanner:
|
||||
return scanPlanBinaryBoolToBoolScanner{}
|
||||
}
|
||||
case TextFormatCode:
|
||||
switch target.(type) {
|
||||
case *bool:
|
||||
return scanPlanTextAnyToBool{}
|
||||
case BoolScanner:
|
||||
return scanPlanTextAnyToBoolScanner{}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c BoolCodec) DecodeDatabaseSQLValue(ci *ConnInfo, oid uint32, format int16, src []byte) (driver.Value, error) {
|
||||
return c.DecodeValue(ci, oid, format, src)
|
||||
}
|
||||
|
||||
func (c BoolCodec) DecodeValue(ci *ConnInfo, oid uint32, format int16, src []byte) (interface{}, error) {
|
||||
if src == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var b bool
|
||||
scanPlan := c.PlanScan(ci, oid, format, &b, true)
|
||||
if scanPlan == nil {
|
||||
return nil, fmt.Errorf("PlanScan did not find a plan")
|
||||
}
|
||||
err := scanPlan.Scan(ci, oid, format, src, &b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func convertToBoolForEncode(v interface{}) (b bool, valid bool, err error) {
|
||||
if v == nil {
|
||||
return false, false, nil
|
||||
}
|
||||
|
||||
switch v := v.(type) {
|
||||
case bool:
|
||||
return v, true, nil
|
||||
case *bool:
|
||||
if v == nil {
|
||||
return false, false, nil
|
||||
}
|
||||
return *v, true, nil
|
||||
case string:
|
||||
bb, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
return bb, true, nil
|
||||
case *string:
|
||||
if v == nil {
|
||||
return false, false, nil
|
||||
}
|
||||
bb, err := strconv.ParseBool(*v)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
return bb, true, nil
|
||||
default:
|
||||
if originalvalue, ok := underlyingBoolType(v); ok {
|
||||
return convertToBoolForEncode(originalvalue)
|
||||
}
|
||||
return false, false, fmt.Errorf("cannot convert %v to bool", v)
|
||||
}
|
||||
}
|
||||
|
||||
type scanPlanBinaryBoolToBool struct{}
|
||||
|
||||
func (scanPlanBinaryBoolToBool) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
if src == nil {
|
||||
return fmt.Errorf("cannot scan null into %T", dst)
|
||||
}
|
||||
|
||||
if len(src) != 1 {
|
||||
return fmt.Errorf("invalid length for bool: %v", len(src))
|
||||
}
|
||||
|
||||
p, ok := (dst).(*bool)
|
||||
if !ok {
|
||||
return ErrScanTargetTypeChanged
|
||||
}
|
||||
|
||||
*p = src[0] == 1
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type scanPlanTextAnyToBool struct{}
|
||||
|
||||
func (scanPlanTextAnyToBool) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
if src == nil {
|
||||
return fmt.Errorf("cannot scan null into %T", dst)
|
||||
}
|
||||
|
||||
if len(src) != 1 {
|
||||
return fmt.Errorf("invalid length for bool: %v", len(src))
|
||||
}
|
||||
|
||||
p, ok := (dst).(*bool)
|
||||
if !ok {
|
||||
return ErrScanTargetTypeChanged
|
||||
}
|
||||
|
||||
*p = src[0] == 't'
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type scanPlanBinaryBoolToBoolScanner struct{}
|
||||
|
||||
func (scanPlanBinaryBoolToBoolScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
s, ok := (dst).(BoolScanner)
|
||||
if !ok {
|
||||
return ErrScanTargetTypeChanged
|
||||
}
|
||||
|
||||
if src == nil {
|
||||
return s.ScanBool(false, false)
|
||||
}
|
||||
|
||||
if len(src) != 1 {
|
||||
return fmt.Errorf("invalid length for bool: %v", len(src))
|
||||
}
|
||||
|
||||
return s.ScanBool(src[0] == 1, true)
|
||||
}
|
||||
|
||||
type scanPlanTextAnyToBoolScanner struct{}
|
||||
|
||||
func (scanPlanTextAnyToBoolScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
|
||||
s, ok := (dst).(BoolScanner)
|
||||
if !ok {
|
||||
return ErrScanTargetTypeChanged
|
||||
}
|
||||
|
||||
if src == nil {
|
||||
return s.ScanBool(false, false)
|
||||
}
|
||||
|
||||
if len(src) != 1 {
|
||||
return fmt.Errorf("invalid length for bool: %v", len(src))
|
||||
}
|
||||
|
||||
return s.ScanBool(src[0] == 't', true)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user