mirror of https://github.com/jackc/pgx.git
129 lines
2.2 KiB
Go
129 lines
2.2 KiB
Go
package pgtype
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
)
|
|
|
|
type Bool struct {
|
|
Bool bool
|
|
Status Status
|
|
}
|
|
|
|
func (dst *Bool) Set(src interface{}) error {
|
|
switch value := src.(type) {
|
|
case bool:
|
|
*dst = Bool{Bool: value, Status: Present}
|
|
case string:
|
|
bb, err := strconv.ParseBool(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*dst = Bool{Bool: bb, Status: Present}
|
|
default:
|
|
if originalSrc, ok := underlyingBoolType(src); ok {
|
|
return dst.Set(originalSrc)
|
|
}
|
|
return fmt.Errorf("cannot convert %v to Bool", value)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (dst *Bool) Get() interface{} {
|
|
switch dst.Status {
|
|
case Present:
|
|
return dst.Bool
|
|
case Null:
|
|
return nil
|
|
default:
|
|
return dst.Status
|
|
}
|
|
}
|
|
|
|
func (src *Bool) AssignTo(dst interface{}) error {
|
|
switch src.Status {
|
|
case Present:
|
|
switch v := dst.(type) {
|
|
case *bool:
|
|
*v = src.Bool
|
|
return nil
|
|
default:
|
|
if nextDst, retry := GetAssignToDstType(dst); retry {
|
|
return src.AssignTo(nextDst)
|
|
}
|
|
}
|
|
case Null:
|
|
return nullAssignTo(dst)
|
|
}
|
|
|
|
return fmt.Errorf("cannot decode %v into %T", src, dst)
|
|
}
|
|
|
|
func (dst *Bool) DecodeText(ci *ConnInfo, src []byte) error {
|
|
if src == nil {
|
|
*dst = Bool{Status: Null}
|
|
return nil
|
|
}
|
|
|
|
if len(src) != 1 {
|
|
return fmt.Errorf("invalid length for bool: %v", len(src))
|
|
}
|
|
|
|
*dst = Bool{Bool: src[0] == 't', Status: Present}
|
|
return nil
|
|
}
|
|
|
|
func (dst *Bool) DecodeBinary(ci *ConnInfo, src []byte) error {
|
|
if src == nil {
|
|
*dst = Bool{Status: Null}
|
|
return nil
|
|
}
|
|
|
|
if len(src) != 1 {
|
|
return fmt.Errorf("invalid length for bool: %v", len(src))
|
|
}
|
|
|
|
*dst = Bool{Bool: src[0] == 1, Status: Present}
|
|
return nil
|
|
}
|
|
|
|
func (src Bool) EncodeText(ci *ConnInfo, w io.Writer) (bool, error) {
|
|
switch src.Status {
|
|
case Null:
|
|
return true, nil
|
|
case Undefined:
|
|
return false, errUndefined
|
|
}
|
|
|
|
var buf []byte
|
|
if src.Bool {
|
|
buf = []byte{'t'}
|
|
} else {
|
|
buf = []byte{'f'}
|
|
}
|
|
|
|
_, err := w.Write(buf)
|
|
return false, err
|
|
}
|
|
|
|
func (src Bool) EncodeBinary(ci *ConnInfo, w io.Writer) (bool, error) {
|
|
switch src.Status {
|
|
case Null:
|
|
return true, nil
|
|
case Undefined:
|
|
return false, errUndefined
|
|
}
|
|
|
|
var buf []byte
|
|
if src.Bool {
|
|
buf = []byte{1}
|
|
} else {
|
|
buf = []byte{0}
|
|
}
|
|
|
|
_, err := w.Write(buf)
|
|
return false, err
|
|
}
|