pgx/pgtype/jsonb.go
Jack Christensen 19c6689752 Add pgtype.Record and prerequisite restructuring
Because reading a record type requires the decoder to be able to look up oid
to type mapping and types such as hstore have types that are not fixed between
different PostgreSQL servers it was necessary to restructure the pgtype system
so all encoders and decodes take a *ConnInfo that includes oid/name/type
information.
2017-03-18 12:01:16 -05:00

69 lines
1.2 KiB
Go

package pgtype
import (
"fmt"
"io"
)
type Jsonb Json
func (dst *Jsonb) Set(src interface{}) error {
return (*Json)(dst).Set(src)
}
func (dst *Jsonb) Get() interface{} {
return (*Json)(dst).Get()
}
func (src *Jsonb) AssignTo(dst interface{}) error {
return (*Json)(src).AssignTo(dst)
}
func (dst *Jsonb) DecodeText(ci *ConnInfo, src []byte) error {
return (*Json)(dst).DecodeText(ci, src)
}
func (dst *Jsonb) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = Jsonb{Status: Null}
return nil
}
if len(src) == 0 {
return fmt.Errorf("jsonb too short")
}
if src[0] != 1 {
return fmt.Errorf("unknown jsonb version number %d", src[0])
}
src = src[1:]
buf := make([]byte, len(src))
copy(buf, src)
*dst = Jsonb{Bytes: buf, Status: Present}
return nil
}
func (src Jsonb) EncodeText(ci *ConnInfo, w io.Writer) (bool, error) {
return (Json)(src).EncodeText(ci, w)
}
func (src Jsonb) EncodeBinary(ci *ConnInfo, w io.Writer) (bool, error) {
switch src.Status {
case Null:
return true, nil
case Undefined:
return false, errUndefined
}
_, err := w.Write([]byte{1})
if err != nil {
return false, err
}
_, err = w.Write(src.Bytes)
return false, err
}