Convert UUID to Codec

query-exec-mode
Jack Christensen 2022-01-22 10:53:47 -06:00
parent b9b5e35d0f
commit 740263c0d4
8 changed files with 309 additions and 1308 deletions

View File

@ -559,3 +559,39 @@ type fmtStringerWrapper struct {
func (w fmtStringerWrapper) TextValue() (Text, error) {
return Text{String: w.s.String(), Valid: true}, nil
}
type byte16Wrapper [16]byte
func (w *byte16Wrapper) ScanUUID(v UUID) error {
if !v.Valid {
return fmt.Errorf("cannot scan NULL into *[16]byte")
}
*w = byte16Wrapper(v.Bytes)
return nil
}
func (w byte16Wrapper) UUIDValue() (UUID, error) {
return UUID{Bytes: [16]byte(w), Valid: true}, nil
}
type byteSliceWrapper []byte
func (w *byteSliceWrapper) ScanUUID(v UUID) error {
if !v.Valid {
*w = nil
return nil
}
*w = make(byteSliceWrapper, 16)
copy(*w, v.Bytes[:])
return nil
}
func (w byteSliceWrapper) UUIDValue() (UUID, error) {
if w == nil {
return UUID{}, nil
}
uuid := UUID{Valid: true}
copy(uuid.Bytes[:], w)
return uuid, nil
}

View File

@ -296,7 +296,7 @@ func NewConnInfo() *ConnInfo {
ci.RegisterDataType(DataType{Name: "_timestamptz", OID: TimestamptzArrayOID, Codec: &ArrayCodec{ElementCodec: TimestamptzCodec{}, ElementOID: TimestamptzOID}})
ci.RegisterDataType(DataType{Name: "_macaddr", OID: MacaddrArrayOID, Codec: &ArrayCodec{ElementCodec: MacaddrCodec{}, ElementOID: MacaddrOID}})
ci.RegisterDataType(DataType{Name: "_tid", OID: TIDArrayOID, Codec: &ArrayCodec{ElementCodec: TIDCodec{}, ElementOID: TIDOID}})
ci.RegisterDataType(DataType{Value: &UUIDArray{}, Name: "_uuid", OID: UUIDArrayOID})
ci.RegisterDataType(DataType{Name: "_uuid", OID: UUIDArrayOID, Codec: &ArrayCodec{ElementCodec: UUIDCodec{}, ElementOID: UUIDOID}})
ci.RegisterDataType(DataType{Name: "_jsonb", OID: JSONBArrayOID, Codec: &ArrayCodec{ElementCodec: JSONBCodec{}, ElementOID: JSONBOID}})
ci.RegisterDataType(DataType{Name: "_json", OID: JSONArrayOID, Codec: &ArrayCodec{ElementCodec: JSONCodec{}, ElementOID: JSONOID}})
ci.RegisterDataType(DataType{Name: "_varchar", OID: VarcharArrayOID, Codec: &ArrayCodec{ElementCodec: TextCodec{}, ElementOID: VarcharOID}})
@ -350,7 +350,7 @@ func NewConnInfo() *ConnInfo {
// ci.RegisterDataType(DataType{Value: &Tstzrange{}, Name: "tstzrange", OID: TstzrangeOID})
// ci.RegisterDataType(DataType{Value: &TstzrangeArray{}, Name: "_tstzrange", OID: TstzrangeArrayOID})
ci.RegisterDataType(DataType{Name: "unknown", OID: UnknownOID, Codec: TextCodec{}})
ci.RegisterDataType(DataType{Value: &UUID{}, Name: "uuid", OID: UUIDOID})
ci.RegisterDataType(DataType{Name: "uuid", OID: UUIDOID, Codec: UUIDCodec{}})
ci.RegisterDataType(DataType{Name: "varbit", OID: VarbitOID, Codec: BitsCodec{}})
ci.RegisterDataType(DataType{Name: "varchar", OID: VarcharOID, Codec: TextCodec{}})
ci.RegisterDataType(DataType{Name: "xid", OID: XIDOID, Codec: Uint32Codec{}})
@ -888,6 +888,10 @@ func tryWrapBuiltinTypeScanPlan(dst interface{}) (plan WrappedScanPlanNextSetter
return &wrapMapStringToPointerStringScanPlan{}, (*mapStringToPointerStringWrapper)(dst), true
case *map[string]string:
return &wrapMapStringToStringScanPlan{}, (*mapStringToStringWrapper)(dst), true
case *[16]byte:
return &wrapByte16ScanPlan{}, (*byte16Wrapper)(dst), true
case *[]byte:
return &wrapByteSliceScanPlan{}, (*byteSliceWrapper)(dst), true
}
return nil, nil, false
@ -1083,6 +1087,26 @@ func (plan *wrapMapStringToStringScanPlan) Scan(ci *ConnInfo, oid uint32, format
return plan.next.Scan(ci, oid, formatCode, src, (*mapStringToStringWrapper)(dst.(*map[string]string)))
}
type wrapByte16ScanPlan struct {
next ScanPlan
}
func (plan *wrapByte16ScanPlan) SetNext(next ScanPlan) { plan.next = next }
func (plan *wrapByte16ScanPlan) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
return plan.next.Scan(ci, oid, formatCode, src, (*byte16Wrapper)(dst.(*[16]byte)))
}
type wrapByteSliceScanPlan struct {
next ScanPlan
}
func (plan *wrapByteSliceScanPlan) SetNext(next ScanPlan) { plan.next = next }
func (plan *wrapByteSliceScanPlan) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
return plan.next.Scan(ci, oid, formatCode, src, (*byteSliceWrapper)(dst.(*[]byte)))
}
type pointerEmptyInterfaceScanPlan struct {
codec Codec
}
@ -1425,6 +1449,10 @@ func tryWrapBuiltinTypeEncodePlan(value interface{}) (plan WrappedEncodePlanNext
return &wrapMapStringToPointerStringEncodePlan{}, mapStringToPointerStringWrapper(value), true
case map[string]string:
return &wrapMapStringToStringEncodePlan{}, mapStringToStringWrapper(value), true
case [16]byte:
return &wrapByte16EncodePlan{}, byte16Wrapper(value), true
case []byte:
return &wrapByteSliceEncodePlan{}, byteSliceWrapper(value), true
case fmt.Stringer:
return &wrapFmtStringerEncodePlan{}, fmtStringerWrapper{value}, true
}
@ -1622,6 +1650,26 @@ func (plan *wrapMapStringToStringEncodePlan) Encode(value interface{}, buf []byt
return plan.next.Encode(mapStringToStringWrapper(value.(map[string]string)), buf)
}
type wrapByte16EncodePlan struct {
next EncodePlan
}
func (plan *wrapByte16EncodePlan) SetNext(next EncodePlan) { plan.next = next }
func (plan *wrapByte16EncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(byte16Wrapper(value.([16]byte)), buf)
}
type wrapByteSliceEncodePlan struct {
next EncodePlan
}
func (plan *wrapByteSliceEncodePlan) SetNext(next EncodePlan) { plan.next = next }
func (plan *wrapByteSliceEncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
return plan.next.Encode(byteSliceWrapper(value.([]byte)), buf)
}
type wrapFmtStringerEncodePlan struct {
next EncodePlan
}

View File

@ -7,144 +7,26 @@ import (
"fmt"
)
type UUIDScanner interface {
ScanUUID(v UUID) error
}
type UUIDValuer interface {
UUIDValue() (UUID, error)
}
type UUID struct {
Bytes [16]byte
Valid bool
UUIDDecoderWrapper func(interface{}) UUIDDecoder
Getter func(UUID) interface{}
}
func (n *UUID) NewTypeValue() Value {
return &UUID{
UUIDDecoderWrapper: n.UUIDDecoderWrapper,
Getter: n.Getter,
}
}
func (n *UUID) TypeName() string {
return "uuid"
}
func (dst *UUID) setNil() {
dst.Bytes = [16]byte{}
dst.Valid = false
}
func (dst *UUID) setByteArray(value [16]byte) {
dst.Bytes = value
dst.Valid = true
}
func (dst *UUID) setByteSlice(value []byte) error {
if value != nil {
if len(value) != 16 {
return fmt.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
}
copy(dst.Bytes[:], value)
dst.Valid = true
} else {
dst.setNil()
}
func (b *UUID) ScanUUID(v UUID) error {
*b = v
return nil
}
func (dst *UUID) setString(value string) error {
uuid, err := parseUUID(value)
if err != nil {
return err
}
dst.setByteArray(uuid)
return nil
}
func (dst *UUID) Set(src interface{}) error {
if src == nil {
dst.setNil()
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 [16]byte:
dst.setByteArray(value)
case []byte:
return dst.setByteSlice(value)
case string:
return dst.setString(value)
case *string:
if value == nil {
dst.setNil()
} else {
return dst.setString(*value)
}
default:
if originalSrc, ok := underlyingUUIDType(src); ok {
return dst.Set(originalSrc)
}
return fmt.Errorf("cannot convert %v to UUID", value)
}
return nil
}
func (dst UUID) Get() interface{} {
if dst.Getter != nil {
return dst.Getter(dst)
}
if !dst.Valid {
return nil
}
return dst.Bytes
}
type UUIDDecoder interface {
DecodeUUID(*UUID) error
}
func (src *UUID) AssignTo(dst interface{}) error {
if d, ok := dst.(UUIDDecoder); ok {
return d.DecodeUUID(src)
} else {
if src.UUIDDecoderWrapper != nil {
d = src.UUIDDecoderWrapper(dst)
if d != nil {
return d.DecodeUUID(src)
}
}
}
if !src.Valid {
return NullAssignTo(dst)
}
switch v := dst.(type) {
case *[16]byte:
*v = src.Bytes
return nil
case *[]byte:
*v = make([]byte, 16)
copy(*v, src.Bytes[:])
return nil
case *string:
*v = encodeUUID(src.Bytes)
return nil
default:
if nextDst, retry := GetAssignToDstType(v); retry {
return src.AssignTo(nextDst)
}
}
return nil
func (b UUID) UUIDValue() (UUID, error) {
return b, nil
}
// parseUUID converts a string UUID in standard form to a byte array.
@ -173,68 +55,21 @@ func encodeUUID(src [16]byte) string {
return fmt.Sprintf("%x-%x-%x-%x-%x", src[0:4], src[4:6], src[6:8], src[8:10], src[10:16])
}
func (dst *UUID) DecodeText(ci *ConnInfo, src []byte) error {
if src == nil {
dst.setNil()
return nil
}
if len(src) != 36 {
return fmt.Errorf("invalid length for UUID: %v", len(src))
}
buf, err := parseUUID(string(src))
if err != nil {
return err
}
dst.setByteArray(buf)
return nil
}
func (dst *UUID) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
dst.setNil()
return nil
}
if len(src) != 16 {
return fmt.Errorf("invalid length for UUID: %v", len(src))
}
return dst.setByteSlice(src)
}
func (src UUID) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if !src.Valid {
return nil, nil
}
return append(buf, encodeUUID(src.Bytes)...), nil
}
func (src UUID) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if !src.Valid {
return nil, nil
}
return append(buf, src.Bytes[:]...), nil
}
// Scan implements the database/sql Scanner interface.
func (dst *UUID) Scan(src interface{}) error {
if src == nil {
dst.setNil()
*dst = UUID{}
return nil
}
switch src := src.(type) {
case string:
return dst.DecodeText(nil, []byte(src))
case []byte:
srcCopy := make([]byte, len(src))
copy(srcCopy, src)
return dst.DecodeText(nil, srcCopy)
buf, err := parseUUID(src)
if err != nil {
return err
}
*dst = UUID{Bytes: buf, Valid: true}
return nil
}
return fmt.Errorf("cannot scan %T", src)
@ -242,7 +77,11 @@ func (dst *UUID) Scan(src interface{}) error {
// Value implements the database/sql/driver Valuer interface.
func (src UUID) Value() (driver.Value, error) {
return EncodeValueText(src)
if !src.Valid {
return nil, nil
}
return encodeUUID(src.Bytes), nil
}
func (src UUID) MarshalJSON() ([]byte, error) {
@ -259,10 +98,151 @@ func (src UUID) MarshalJSON() ([]byte, error) {
func (dst *UUID) UnmarshalJSON(src []byte) error {
if bytes.Compare(src, []byte("null")) == 0 {
return dst.Set(nil)
*dst = UUID{}
return nil
}
if len(src) != 38 {
return fmt.Errorf("invalid length for UUID: %v", len(src))
}
return dst.Set(string(src[1 : len(src)-1]))
buf, err := parseUUID(string(src[1 : len(src)-1]))
if err != nil {
return err
}
*dst = UUID{Bytes: buf, Valid: true}
return nil
}
type UUIDCodec struct{}
func (UUIDCodec) FormatSupported(format int16) bool {
return format == TextFormatCode || format == BinaryFormatCode
}
func (UUIDCodec) PreferredFormat() int16 {
return BinaryFormatCode
}
func (UUIDCodec) PlanEncode(ci *ConnInfo, oid uint32, format int16, value interface{}) EncodePlan {
if _, ok := value.(UUIDValuer); !ok {
return nil
}
switch format {
case BinaryFormatCode:
return encodePlanUUIDCodecBinaryUUIDValuer{}
case TextFormatCode:
return encodePlanUUIDCodecTextUUIDValuer{}
}
return nil
}
type encodePlanUUIDCodecBinaryUUIDValuer struct{}
func (encodePlanUUIDCodecBinaryUUIDValuer) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
uuid, err := value.(UUIDValuer).UUIDValue()
if err != nil {
return nil, err
}
if !uuid.Valid {
return nil, nil
}
return append(buf, uuid.Bytes[:]...), nil
}
type encodePlanUUIDCodecTextUUIDValuer struct{}
func (encodePlanUUIDCodecTextUUIDValuer) Encode(value interface{}, buf []byte) (newBuf []byte, err error) {
uuid, err := value.(UUIDValuer).UUIDValue()
if err != nil {
return nil, err
}
if !uuid.Valid {
return nil, nil
}
return append(buf, encodeUUID(uuid.Bytes)...), nil
}
func (UUIDCodec) PlanScan(ci *ConnInfo, oid uint32, format int16, target interface{}, actualTarget bool) ScanPlan {
switch format {
case BinaryFormatCode:
switch target.(type) {
case UUIDScanner:
return scanPlanBinaryUUIDToUUIDScanner{}
}
case TextFormatCode:
switch target.(type) {
case UUIDScanner:
return scanPlanTextAnyToUUIDScanner{}
}
}
return nil
}
type scanPlanBinaryUUIDToUUIDScanner struct{}
func (scanPlanBinaryUUIDToUUIDScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
scanner := (dst).(UUIDScanner)
if src == nil {
return scanner.ScanUUID(UUID{})
}
if len(src) != 16 {
return fmt.Errorf("invalid length for UUID: %v", len(src))
}
uuid := UUID{Valid: true}
copy(uuid.Bytes[:], src)
return scanner.ScanUUID(uuid)
}
type scanPlanTextAnyToUUIDScanner struct{}
func (scanPlanTextAnyToUUIDScanner) Scan(ci *ConnInfo, oid uint32, formatCode int16, src []byte, dst interface{}) error {
scanner := (dst).(UUIDScanner)
if src == nil {
return scanner.ScanUUID(UUID{})
}
buf, err := parseUUID(string(src))
if err != nil {
return err
}
return scanner.ScanUUID(UUID{Bytes: buf, Valid: true})
}
func (c UUIDCodec) DecodeDatabaseSQLValue(ci *ConnInfo, oid uint32, format int16, src []byte) (driver.Value, error) {
if src == nil {
return nil, nil
}
var uuid UUID
err := codecScan(c, ci, oid, format, src, &uuid)
if err != nil {
return nil, err
}
return encodeUUID(uuid.Bytes), nil
}
func (c UUIDCodec) DecodeValue(ci *ConnInfo, oid uint32, format int16, src []byte) (interface{}, error) {
if src == nil {
return nil, nil
}
var uuid UUID
err := codecScan(c, ci, oid, format, src, &uuid)
if err != nil {
return nil, err
}
return uuid.Bytes, nil
}

View File

@ -1,560 +0,0 @@
// Code generated by erb. DO NOT EDIT.
package pgtype
import (
"database/sql/driver"
"encoding/binary"
"fmt"
"reflect"
"github.com/jackc/pgio"
)
type UUIDArray struct {
Elements []UUID
Dimensions []ArrayDimension
Valid bool
}
func (dst *UUIDArray) Set(src interface{}) error {
// untyped nil and typed nil interfaces are different
if src == nil {
*dst = UUIDArray{}
return nil
}
if value, ok := src.(interface{ Get() interface{} }); ok {
value2 := value.Get()
if value2 != value {
return dst.Set(value2)
}
}
// Attempt to match to select common types:
switch value := src.(type) {
case [][16]byte:
if value == nil {
*dst = UUIDArray{}
} else if len(value) == 0 {
*dst = UUIDArray{Valid: true}
} else {
elements := make([]UUID, len(value))
for i := range value {
if err := elements[i].Set(value[i]); err != nil {
return err
}
}
*dst = UUIDArray{
Elements: elements,
Dimensions: []ArrayDimension{{Length: int32(len(elements)), LowerBound: 1}},
Valid: true,
}
}
case [][]byte:
if value == nil {
*dst = UUIDArray{}
} else if len(value) == 0 {
*dst = UUIDArray{Valid: true}
} else {
elements := make([]UUID, len(value))
for i := range value {
if err := elements[i].Set(value[i]); err != nil {
return err
}
}
*dst = UUIDArray{
Elements: elements,
Dimensions: []ArrayDimension{{Length: int32(len(elements)), LowerBound: 1}},
Valid: true,
}
}
case []string:
if value == nil {
*dst = UUIDArray{}
} else if len(value) == 0 {
*dst = UUIDArray{Valid: true}
} else {
elements := make([]UUID, len(value))
for i := range value {
if err := elements[i].Set(value[i]); err != nil {
return err
}
}
*dst = UUIDArray{
Elements: elements,
Dimensions: []ArrayDimension{{Length: int32(len(elements)), LowerBound: 1}},
Valid: true,
}
}
case []*string:
if value == nil {
*dst = UUIDArray{}
} else if len(value) == 0 {
*dst = UUIDArray{Valid: true}
} else {
elements := make([]UUID, len(value))
for i := range value {
if err := elements[i].Set(value[i]); err != nil {
return err
}
}
*dst = UUIDArray{
Elements: elements,
Dimensions: []ArrayDimension{{Length: int32(len(elements)), LowerBound: 1}},
Valid: true,
}
}
case []UUID:
if value == nil {
*dst = UUIDArray{}
} else if len(value) == 0 {
*dst = UUIDArray{Valid: true}
} else {
*dst = UUIDArray{
Elements: value,
Dimensions: []ArrayDimension{{Length: int32(len(value)), LowerBound: 1}},
Valid: true,
}
}
default:
// Fallback to reflection if an optimised match was not found.
// The reflection is necessary for arrays and multidimensional slices,
// but it comes with a 20-50% performance penalty for large arrays/slices
reflectedValue := reflect.ValueOf(src)
if !reflectedValue.IsValid() || reflectedValue.IsZero() {
*dst = UUIDArray{}
return nil
}
dimensions, elementsLength, ok := findDimensionsFromValue(reflectedValue, nil, 0)
if !ok {
return fmt.Errorf("cannot find dimensions of %v for UUIDArray", src)
}
if elementsLength == 0 {
*dst = UUIDArray{Valid: true}
return nil
}
if len(dimensions) == 0 {
if originalSrc, ok := underlyingSliceType(src); ok {
return dst.Set(originalSrc)
}
return fmt.Errorf("cannot convert %v to UUIDArray", src)
}
*dst = UUIDArray{
Elements: make([]UUID, elementsLength),
Dimensions: dimensions,
Valid: true,
}
elementCount, err := dst.setRecursive(reflectedValue, 0, 0)
if err != nil {
// Maybe the target was one dimension too far, try again:
if len(dst.Dimensions) > 1 {
dst.Dimensions = dst.Dimensions[:len(dst.Dimensions)-1]
elementsLength = 0
for _, dim := range dst.Dimensions {
if elementsLength == 0 {
elementsLength = int(dim.Length)
} else {
elementsLength *= int(dim.Length)
}
}
dst.Elements = make([]UUID, elementsLength)
elementCount, err = dst.setRecursive(reflectedValue, 0, 0)
if err != nil {
return err
}
} else {
return err
}
}
if elementCount != len(dst.Elements) {
return fmt.Errorf("cannot convert %v to UUIDArray, expected %d dst.Elements, but got %d instead", src, len(dst.Elements), elementCount)
}
}
return nil
}
func (dst *UUIDArray) setRecursive(value reflect.Value, index, dimension int) (int, error) {
switch value.Kind() {
case reflect.Array:
fallthrough
case reflect.Slice:
if len(dst.Dimensions) == dimension {
break
}
valueLen := value.Len()
if int32(valueLen) != dst.Dimensions[dimension].Length {
return 0, fmt.Errorf("multidimensional arrays must have array expressions with matching dimensions")
}
for i := 0; i < valueLen; i++ {
var err error
index, err = dst.setRecursive(value.Index(i), index, dimension+1)
if err != nil {
return 0, err
}
}
return index, nil
}
if !value.CanInterface() {
return 0, fmt.Errorf("cannot convert all values to UUIDArray")
}
if err := dst.Elements[index].Set(value.Interface()); err != nil {
return 0, fmt.Errorf("%v in UUIDArray", err)
}
index++
return index, nil
}
func (dst UUIDArray) Get() interface{} {
if !dst.Valid {
return nil
}
return dst
}
func (src *UUIDArray) AssignTo(dst interface{}) error {
if !src.Valid {
return NullAssignTo(dst)
}
if len(src.Dimensions) <= 1 {
// Attempt to match to select common types:
switch v := dst.(type) {
case *[][16]byte:
*v = make([][16]byte, len(src.Elements))
for i := range src.Elements {
if err := src.Elements[i].AssignTo(&((*v)[i])); err != nil {
return err
}
}
return nil
case *[][]byte:
*v = make([][]byte, len(src.Elements))
for i := range src.Elements {
if err := src.Elements[i].AssignTo(&((*v)[i])); err != nil {
return err
}
}
return nil
case *[]string:
*v = make([]string, len(src.Elements))
for i := range src.Elements {
if err := src.Elements[i].AssignTo(&((*v)[i])); err != nil {
return err
}
}
return nil
case *[]*string:
*v = make([]*string, len(src.Elements))
for i := range src.Elements {
if err := src.Elements[i].AssignTo(&((*v)[i])); err != nil {
return err
}
}
return nil
}
}
// Try to convert to something AssignTo can use directly.
if nextDst, retry := GetAssignToDstType(dst); retry {
return src.AssignTo(nextDst)
}
// Fallback to reflection if an optimised match was not found.
// The reflection is necessary for arrays and multidimensional slices,
// but it comes with a 20-50% performance penalty for large arrays/slices
value := reflect.ValueOf(dst)
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
switch value.Kind() {
case reflect.Array, reflect.Slice:
default:
return fmt.Errorf("cannot assign %T to %T", src, dst)
}
if len(src.Elements) == 0 {
if value.Kind() == reflect.Slice {
value.Set(reflect.MakeSlice(value.Type(), 0, 0))
return nil
}
}
elementCount, err := src.assignToRecursive(value, 0, 0)
if err != nil {
return err
}
if elementCount != len(src.Elements) {
return fmt.Errorf("cannot assign %v, needed to assign %d elements, but only assigned %d", dst, len(src.Elements), elementCount)
}
return nil
}
func (src *UUIDArray) assignToRecursive(value reflect.Value, index, dimension int) (int, error) {
switch kind := value.Kind(); kind {
case reflect.Array:
fallthrough
case reflect.Slice:
if len(src.Dimensions) == dimension {
break
}
length := int(src.Dimensions[dimension].Length)
if reflect.Array == kind {
typ := value.Type()
if typ.Len() != length {
return 0, fmt.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
}
value.Set(reflect.New(typ).Elem())
} else {
value.Set(reflect.MakeSlice(value.Type(), length, length))
}
var err error
for i := 0; i < length; i++ {
index, err = src.assignToRecursive(value.Index(i), index, dimension+1)
if err != nil {
return 0, err
}
}
return index, nil
}
if len(src.Dimensions) != dimension {
return 0, fmt.Errorf("incorrect dimensions, expected %d, found %d", len(src.Dimensions), dimension)
}
if !value.CanAddr() {
return 0, fmt.Errorf("cannot assign all values from UUIDArray")
}
addr := value.Addr()
if !addr.CanInterface() {
return 0, fmt.Errorf("cannot assign all values from UUIDArray")
}
if err := src.Elements[index].AssignTo(addr.Interface()); err != nil {
return 0, err
}
index++
return index, nil
}
func (dst *UUIDArray) DecodeText(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = UUIDArray{}
return nil
}
uta, err := ParseUntypedTextArray(string(src))
if err != nil {
return err
}
var elements []UUID
if len(uta.Elements) > 0 {
elements = make([]UUID, len(uta.Elements))
for i, s := range uta.Elements {
var elem UUID
var elemSrc []byte
if s != "NULL" || uta.Quoted[i] {
elemSrc = []byte(s)
}
err = elem.DecodeText(ci, elemSrc)
if err != nil {
return err
}
elements[i] = elem
}
}
*dst = UUIDArray{Elements: elements, Dimensions: uta.Dimensions, Valid: true}
return nil
}
func (dst *UUIDArray) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = UUIDArray{}
return nil
}
var arrayHeader ArrayHeader
rp, err := arrayHeader.DecodeBinary(ci, src)
if err != nil {
return err
}
if len(arrayHeader.Dimensions) == 0 {
*dst = UUIDArray{Dimensions: arrayHeader.Dimensions, Valid: true}
return nil
}
elementCount := arrayHeader.Dimensions[0].Length
for _, d := range arrayHeader.Dimensions[1:] {
elementCount *= d.Length
}
elements := make([]UUID, elementCount)
for i := range elements {
elemLen := int(int32(binary.BigEndian.Uint32(src[rp:])))
rp += 4
var elemSrc []byte
if elemLen >= 0 {
elemSrc = src[rp : rp+elemLen]
rp += elemLen
}
err = elements[i].DecodeBinary(ci, elemSrc)
if err != nil {
return err
}
}
*dst = UUIDArray{Elements: elements, Dimensions: arrayHeader.Dimensions, Valid: true}
return nil
}
func (src UUIDArray) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
if !src.Valid {
return nil, nil
}
if len(src.Dimensions) == 0 {
return append(buf, '{', '}'), nil
}
buf = EncodeTextArrayDimensions(buf, src.Dimensions)
// dimElemCounts is the multiples of elements that each array lies on. For
// example, a single dimension array of length 4 would have a dimElemCounts of
// [4]. A multi-dimensional array of lengths [3,5,2] would have a
// dimElemCounts of [30,10,2]. This is used to simplify when to render a '{'
// or '}'.
dimElemCounts := make([]int, len(src.Dimensions))
dimElemCounts[len(src.Dimensions)-1] = int(src.Dimensions[len(src.Dimensions)-1].Length)
for i := len(src.Dimensions) - 2; i > -1; i-- {
dimElemCounts[i] = int(src.Dimensions[i].Length) * dimElemCounts[i+1]
}
inElemBuf := make([]byte, 0, 32)
for i, elem := range src.Elements {
if i > 0 {
buf = append(buf, ',')
}
for _, dec := range dimElemCounts {
if i%dec == 0 {
buf = append(buf, '{')
}
}
elemBuf, err := elem.EncodeText(ci, inElemBuf)
if err != nil {
return nil, err
}
if elemBuf == nil {
buf = append(buf, `NULL`...)
} else {
buf = append(buf, QuoteArrayElementIfNeeded(string(elemBuf))...)
}
for _, dec := range dimElemCounts {
if (i+1)%dec == 0 {
buf = append(buf, '}')
}
}
}
return buf, nil
}
func (src UUIDArray) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
if !src.Valid {
return nil, nil
}
arrayHeader := ArrayHeader{
Dimensions: src.Dimensions,
}
if dt, ok := ci.DataTypeForName("uuid"); ok {
arrayHeader.ElementOID = int32(dt.OID)
} else {
return nil, fmt.Errorf("unable to find oid for type name %v", "uuid")
}
for i := range src.Elements {
if !src.Elements[i].Valid {
arrayHeader.ContainsNull = true
break
}
}
buf = arrayHeader.EncodeBinary(ci, buf)
for i := range src.Elements {
sp := len(buf)
buf = pgio.AppendInt32(buf, -1)
elemBuf, err := src.Elements[i].EncodeBinary(ci, buf)
if err != nil {
return nil, err
}
if elemBuf != nil {
buf = elemBuf
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
}
}
return buf, nil
}
// Scan implements the database/sql Scanner interface.
func (dst *UUIDArray) Scan(src interface{}) error {
if src == nil {
return dst.DecodeText(nil, nil)
}
switch src := src.(type) {
case string:
return dst.DecodeText(nil, []byte(src))
case []byte:
srcCopy := make([]byte, len(src))
copy(srcCopy, src)
return dst.DecodeText(nil, srcCopy)
}
return fmt.Errorf("cannot scan %T", src)
}
// Value implements the database/sql/driver Valuer interface.
func (src UUIDArray) Value() (driver.Value, error) {
buf, err := src.EncodeText(nil, nil)
if err != nil {
return nil, err
}
if buf == nil {
return nil, nil
}
return string(buf), nil
}

View File

@ -1,368 +0,0 @@
package pgtype_test
import (
"reflect"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgtype/testutil"
)
func TestUUIDArrayTranscode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "uuid[]", []interface{}{
&pgtype.UUIDArray{
Elements: nil,
Dimensions: nil,
Valid: true,
},
&pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{},
},
Dimensions: []pgtype.ArrayDimension{{Length: 2, LowerBound: 1}},
Valid: true,
},
&pgtype.UUIDArray{},
&pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true},
{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Valid: true},
{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Valid: true},
{},
{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Valid: true},
},
Dimensions: []pgtype.ArrayDimension{{Length: 3, LowerBound: 1}, {Length: 2, LowerBound: 1}},
Valid: true,
},
&pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true},
{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Valid: true},
{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Valid: true},
},
Dimensions: []pgtype.ArrayDimension{
{Length: 2, LowerBound: 4},
{Length: 2, LowerBound: 2},
},
Valid: true,
},
})
}
func TestUUIDArraySet(t *testing.T) {
successfulTests := []struct {
source interface{}
result pgtype.UUIDArray
}{
{
source: nil,
result: pgtype.UUIDArray{},
},
{
source: [][16]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
Valid: true},
},
{
source: [][16]byte{},
result: pgtype.UUIDArray{Valid: true},
},
{
source: ([][16]byte)(nil),
result: pgtype.UUIDArray{},
},
{
source: [][]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
Valid: true},
},
{
source: [][]byte{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
nil,
{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47},
},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true},
{},
{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Valid: true},
},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 4}},
Valid: true},
},
{
source: [][]byte{},
result: pgtype.UUIDArray{Valid: true},
},
{
source: ([][]byte)(nil),
result: pgtype.UUIDArray{},
},
{
source: []string{"00010203-0405-0607-0809-0a0b0c0d0e0f"},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
Valid: true},
},
{
source: []string{},
result: pgtype.UUIDArray{Valid: true},
},
{
source: ([]string)(nil),
result: pgtype.UUIDArray{},
},
{
source: [][][16]byte{{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
Valid: true},
},
{
source: [][][][]string{
{{{
"00010203-0405-0607-0809-0a0b0c0d0e0f",
"10111213-1415-1617-1819-1a1b1c1d1e1f",
"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
{{{
"30313233-3435-3637-3839-3a3b3c3d3e3f",
"40414243-4445-4647-4849-4a4b4c4d4e4f",
"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true},
{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Valid: true},
{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Valid: true},
{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Valid: true},
{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{
{LowerBound: 1, Length: 2},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 3}},
Valid: true},
},
{
source: [2][1][16]byte{{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
Valid: true},
},
{
source: [2][1][1][3]string{
{{{
"00010203-0405-0607-0809-0a0b0c0d0e0f",
"10111213-1415-1617-1819-1a1b1c1d1e1f",
"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
{{{
"30313233-3435-3637-3839-3a3b3c3d3e3f",
"40414243-4445-4647-4849-4a4b4c4d4e4f",
"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
result: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true},
{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Valid: true},
{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Valid: true},
{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Valid: true},
{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{
{LowerBound: 1, Length: 2},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 3}},
Valid: true},
},
}
for i, tt := range successfulTests {
var r pgtype.UUIDArray
err := r.Set(tt.source)
if err != nil {
t.Errorf("%d: %v", i, err)
}
if !reflect.DeepEqual(r, tt.result) {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}
func TestUUIDArrayAssignTo(t *testing.T) {
var byteArraySlice [][16]byte
var byteSliceSlice [][]byte
var stringSlice []string
var byteSlice []byte
var byteArraySliceDim2 [][][16]byte
var stringSliceDim4 [][][][]string
var byteArrayDim2 [2][1][16]byte
var stringArrayDim4 [2][1][1][3]string
simpleTests := []struct {
src pgtype.UUIDArray
dst interface{}
expected interface{}
}{
{
src: pgtype.UUIDArray{
Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
Valid: true,
},
dst: &byteArraySlice,
expected: [][16]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
},
{
src: pgtype.UUIDArray{},
dst: &byteArraySlice,
expected: ([][16]byte)(nil),
},
{
src: pgtype.UUIDArray{
Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
Valid: true,
},
dst: &byteSliceSlice,
expected: [][]byte{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
},
{
src: pgtype.UUIDArray{},
dst: &byteSliceSlice,
expected: ([][]byte)(nil),
},
{
src: pgtype.UUIDArray{Valid: true},
dst: &byteSlice,
expected: []byte{},
},
{
src: pgtype.UUIDArray{Valid: true},
dst: &stringSlice,
expected: []string{},
},
{
src: pgtype.UUIDArray{
Elements: []pgtype.UUID{{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 1}},
Valid: true,
},
dst: &stringSlice,
expected: []string{"00010203-0405-0607-0809-0a0b0c0d0e0f"},
},
{
src: pgtype.UUIDArray{},
dst: &stringSlice,
expected: ([]string)(nil),
},
{
src: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
Valid: true},
dst: &byteArraySliceDim2,
expected: [][][16]byte{{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
},
{
src: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true},
{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Valid: true},
{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Valid: true},
{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Valid: true},
{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{
{LowerBound: 1, Length: 2},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 3}},
Valid: true},
dst: &stringSliceDim4,
expected: [][][][]string{
{{{
"00010203-0405-0607-0809-0a0b0c0d0e0f",
"10111213-1415-1617-1819-1a1b1c1d1e1f",
"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
{{{
"30313233-3435-3637-3839-3a3b3c3d3e3f",
"40414243-4445-4647-4849-4a4b4c4d4e4f",
"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
},
{
src: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{{LowerBound: 1, Length: 2}, {LowerBound: 1, Length: 1}},
Valid: true},
dst: &byteArrayDim2,
expected: [2][1][16]byte{{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}},
},
{
src: pgtype.UUIDArray{
Elements: []pgtype.UUID{
{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
{Bytes: [16]byte{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, Valid: true},
{Bytes: [16]byte{32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47}, Valid: true},
{Bytes: [16]byte{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63}, Valid: true},
{Bytes: [16]byte{64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79}, Valid: true},
{Bytes: [16]byte{80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95}, Valid: true}},
Dimensions: []pgtype.ArrayDimension{
{LowerBound: 1, Length: 2},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 1},
{LowerBound: 1, Length: 3}},
Valid: true},
dst: &stringArrayDim4,
expected: [2][1][1][3]string{
{{{
"00010203-0405-0607-0809-0a0b0c0d0e0f",
"10111213-1415-1617-1819-1a1b1c1d1e1f",
"20212223-2425-2627-2829-2a2b2c2d2e2f"}}},
{{{
"30313233-3435-3637-3839-3a3b3c3d3e3f",
"40414243-4445-4647-4849-4a4b4c4d4e4f",
"50515253-5455-5657-5859-5a5b5c5d5e5f"}}}},
},
}
for i, tt := range simpleTests {
err := tt.src.AssignTo(tt.dst)
if err != nil {
t.Errorf("%d: %v", i, err)
}
if dst := reflect.ValueOf(tt.dst).Elem().Interface(); !reflect.DeepEqual(dst, tt.expected) {
t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
}
}
}

View File

@ -1,153 +1,46 @@
package pgtype_test
import (
"bytes"
"reflect"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgtype/testutil"
"github.com/stretchr/testify/require"
)
func TestUUIDTranscode(t *testing.T) {
testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{
&pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
&pgtype.UUID{},
func TestUUIDCodec(t *testing.T) {
testPgxCodec(t, "uuid", []PgxTranscodeTestCase{
{
pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
new(pgtype.UUID),
isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
},
{
[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
new(pgtype.UUID),
isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
},
{
[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
new(pgtype.UUID),
isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
},
{
"00010203-0405-0607-0809-0a0b0c0d0e0f",
new(pgtype.UUID),
isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
},
{
"000102030405060708090a0b0c0d0e0f",
new(pgtype.UUID),
isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
},
{pgtype.UUID{}, new([]byte), isExpectedEqBytes([]byte(nil))},
{pgtype.UUID{}, new(pgtype.UUID), isExpectedEq(pgtype.UUID{})},
{nil, new(pgtype.UUID), isExpectedEq(pgtype.UUID{})},
})
}
type SomeUUIDWrapper struct {
SomeUUIDType
}
type SomeUUIDType [16]byte
func TestUUIDSet(t *testing.T) {
successfulTests := []struct {
source interface{}
result pgtype.UUID
}{
{
source: nil,
result: pgtype.UUID{},
},
{
source: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
},
{
source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
},
{
source: SomeUUIDType{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
},
{
source: ([]byte)(nil),
result: pgtype.UUID{},
},
{
source: "00010203-0405-0607-0809-0a0b0c0d0e0f",
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
},
{
source: "000102030405060708090a0b0c0d0e0f",
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
},
}
for i, tt := range successfulTests {
var r pgtype.UUID
err := r.Set(tt.source)
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r.Bytes != tt.result.Bytes || r.Valid != tt.result.Valid {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}
func TestUUIDAssignTo(t *testing.T) {
{
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}
var dst [16]byte
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
err := src.AssignTo(&dst)
if err != nil {
t.Error(err)
}
if dst != expected {
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
}
}
{
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}
var dst []byte
expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
err := src.AssignTo(&dst)
if err != nil {
t.Error(err)
}
if bytes.Compare(dst, expected) != 0 {
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
}
}
{
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}
var dst SomeUUIDType
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
err := src.AssignTo(&dst)
if err != nil {
t.Error(err)
}
if dst != expected {
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
}
}
{
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}
var dst string
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"
err := src.AssignTo(&dst)
if err != nil {
t.Error(err)
}
if dst != expected {
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
}
}
{
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}
var dst SomeUUIDWrapper
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
err := src.AssignTo(&dst)
if err != nil {
t.Error(err)
}
if dst.SomeUUIDType != expected {
t.Errorf("expected %v to assign %v, but result was %v", src, expected, dst)
}
}
}
func TestUUID_MarshalJSON(t *testing.T) {
tests := []struct {
name string

View File

@ -8,68 +8,29 @@ import (
type UUID [16]byte
func (dst *UUID) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
var nullable pgtype.UUID
err := nullable.DecodeText(ci, src)
if err != nil {
return err
// ScanUUID implements the UUIDScanner interface.
func (u *UUID) ScanUUID(v pgtype.UUID) error {
if !v.Valid {
*u = UUID{}
return nil
}
if nullable.Valid {
*dst = UUID(nullable.Bytes)
} else {
*dst = UUID{}
}
*u = UUID(v.Bytes)
return nil
}
func (dst *UUID) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
var nullable pgtype.UUID
err := nullable.DecodeBinary(ci, src)
if err != nil {
return err
func (u UUID) UUIDValue() (pgtype.UUID, error) {
if u == (UUID{}) {
return pgtype.UUID{}, nil
}
if nullable.Valid {
*dst = UUID(nullable.Bytes)
} else {
*dst = UUID{}
}
return nil
}
func (src UUID) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
if (src == UUID{}) {
return nil, nil
}
nullable := pgtype.UUID{
Bytes: [16]byte(src),
Valid: true,
}
return nullable.EncodeText(ci, buf)
}
func (src UUID) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
if (src == UUID{}) {
return nil, nil
}
nullable := pgtype.UUID{
Bytes: [16]byte(src),
Valid: true,
}
return nullable.EncodeBinary(ci, buf)
return pgtype.UUID{Bytes: u, Valid: true}, nil
}
// Scan implements the database/sql Scanner interface.
func (dst *UUID) Scan(src interface{}) error {
func (u *UUID) Scan(src interface{}) error {
if src == nil {
*dst = UUID{}
*u = UUID{}
return nil
}
@ -79,12 +40,21 @@ func (dst *UUID) Scan(src interface{}) error {
return err
}
*dst = UUID(nullable.Bytes)
*u = UUID(nullable.Bytes)
return nil
}
// Value implements the database/sql/driver Valuer interface.
func (src UUID) Value() (driver.Value, error) {
return pgtype.EncodeValueText(src)
func (u UUID) Value() (driver.Value, error) {
if u == (UUID{}) {
return nil, nil
}
buf, err := pgtype.UUIDCodec{}.PlanEncode(nil, pgtype.UUIDOID, pgtype.TextFormatCode, u).Encode(u, nil)
if err != nil {
return nil, err
}
return string(buf), nil
}

View File

@ -243,6 +243,8 @@ func mustParseCIDR(t *testing.T, s string) *net.IPNet {
}
func TestStringToNotTextTypeTranscode(t *testing.T) {
t.Skip("TODO - unskip later in v5") // Should this even be a thing... i.e. anything is scanable to a string to a string
t.Parallel()
testWithAndWithoutPreferSimpleProtocol(t, func(t *testing.T, conn *pgx.Conn) {