pgtype.Encode(Binary|Text) do not write length
To aid in composability, these methods no longer write their own length. This is especially useful for text formatted arrays and may be useful for future database/sql compatibility. It also makes the code a little simpler as the types no longer have to compute their own size. Along with this, these methods cannot encode NULL. They now return a boolean if they are NULL. This also benefits text array encoding as numeric arrays require NULL to be exactly `NULL` while string arrays require NULL to be `"NULL"`.
This commit is contained in:
@@ -124,30 +124,26 @@ func (dst *Float4) DecodeBinary(src []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src Float4) EncodeText(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
func (src Float4) EncodeText(w io.Writer) (bool, error) {
|
||||
switch src.Status {
|
||||
case Null:
|
||||
return true, nil
|
||||
case Undefined:
|
||||
return false, errUndefined
|
||||
}
|
||||
|
||||
s := strconv.FormatFloat(float64(src.Float), 'f', -1, 32)
|
||||
_, err := pgio.WriteInt32(w, int32(len(s)))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
_, err = w.Write([]byte(s))
|
||||
return err
|
||||
_, err := io.WriteString(w, strconv.FormatFloat(float64(src.Float), 'f', -1, 32))
|
||||
return false, err
|
||||
}
|
||||
|
||||
func (src Float4) EncodeBinary(w io.Writer) error {
|
||||
if done, err := encodeNotPresent(w, src.Status); done {
|
||||
return err
|
||||
func (src Float4) EncodeBinary(w io.Writer) (bool, error) {
|
||||
switch src.Status {
|
||||
case Null:
|
||||
return true, nil
|
||||
case Undefined:
|
||||
return false, errUndefined
|
||||
}
|
||||
|
||||
_, err := pgio.WriteInt32(w, 4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = pgio.WriteInt32(w, int32(math.Float32bits(src.Float)))
|
||||
return err
|
||||
_, err := pgio.WriteInt32(w, int32(math.Float32bits(src.Float)))
|
||||
return false, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user