pgx/pgtype/text.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

110 lines
2.1 KiB
Go

package pgtype
import (
"fmt"
"io"
"reflect"
)
type Text struct {
String string
Status Status
}
func (dst *Text) Set(src interface{}) error {
switch value := src.(type) {
case string:
*dst = Text{String: value, Status: Present}
case *string:
if value == nil {
*dst = Text{Status: Null}
} else {
*dst = Text{String: *value, Status: Present}
}
default:
if originalSrc, ok := underlyingStringType(src); ok {
return dst.Set(originalSrc)
}
return fmt.Errorf("cannot convert %v to Text", value)
}
return nil
}
func (dst *Text) Get() interface{} {
switch dst.Status {
case Present:
return dst.String
case Null:
return nil
default:
return dst.Status
}
}
func (src *Text) AssignTo(dst interface{}) error {
switch v := dst.(type) {
case *string:
if src.Status != Present {
return fmt.Errorf("cannot assign %v to %T", src, dst)
}
*v = src.String
default:
if v := reflect.ValueOf(dst); v.Kind() == reflect.Ptr {
el := v.Elem()
switch el.Kind() {
// if dst is a pointer to pointer, strip the pointer and try again
case reflect.Ptr:
if src.Status == Null {
el.Set(reflect.Zero(el.Type()))
return nil
}
if el.IsNil() {
// allocate destination
el.Set(reflect.New(el.Type().Elem()))
}
return src.AssignTo(el.Interface())
case reflect.String:
if src.Status != Present {
return fmt.Errorf("cannot assign %v to %T", src, dst)
}
el.SetString(src.String)
return nil
}
}
return fmt.Errorf("cannot decode %v into %T", src, dst)
}
return nil
}
func (dst *Text) DecodeText(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = Text{Status: Null}
return nil
}
*dst = Text{String: string(src), Status: Present}
return nil
}
func (dst *Text) DecodeBinary(ci *ConnInfo, src []byte) error {
return dst.DecodeText(ci, src)
}
func (src Text) EncodeText(ci *ConnInfo, w io.Writer) (bool, error) {
switch src.Status {
case Null:
return true, nil
case Undefined:
return false, errUndefined
}
_, err := io.WriteString(w, src.String)
return false, err
}
func (src Text) EncodeBinary(ci *ConnInfo, w io.Writer) (bool, error) {
return src.EncodeText(ci, w)
}