mirror of https://github.com/jackc/pgx.git
Use Go casing convention for UUID
parent
27ab289096
commit
3bdc94cee2
|
@ -11,43 +11,43 @@ import (
|
||||||
|
|
||||||
var errUndefined = errors.New("cannot encode status undefined")
|
var errUndefined = errors.New("cannot encode status undefined")
|
||||||
|
|
||||||
type Uuid struct {
|
type UUID struct {
|
||||||
UUID uuid.UUID
|
UUID uuid.UUID
|
||||||
Status pgtype.Status
|
Status pgtype.Status
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Uuid) Set(src interface{}) error {
|
func (dst *UUID) Set(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case uuid.UUID:
|
case uuid.UUID:
|
||||||
*dst = Uuid{UUID: value, Status: pgtype.Present}
|
*dst = UUID{UUID: value, Status: pgtype.Present}
|
||||||
case [16]byte:
|
case [16]byte:
|
||||||
*dst = Uuid{UUID: uuid.UUID(value), Status: pgtype.Present}
|
*dst = UUID{UUID: uuid.UUID(value), Status: pgtype.Present}
|
||||||
case []byte:
|
case []byte:
|
||||||
if len(value) != 16 {
|
if len(value) != 16 {
|
||||||
return fmt.Errorf("[]byte must be 16 bytes to convert to Uuid: %d", len(value))
|
return fmt.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
|
||||||
}
|
}
|
||||||
*dst = Uuid{Status: pgtype.Present}
|
*dst = UUID{Status: pgtype.Present}
|
||||||
copy(dst.UUID[:], value)
|
copy(dst.UUID[:], value)
|
||||||
case string:
|
case string:
|
||||||
uuid, err := uuid.FromString(value)
|
uuid, err := uuid.FromString(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*dst = Uuid{UUID: uuid, Status: pgtype.Present}
|
*dst = UUID{UUID: uuid, Status: pgtype.Present}
|
||||||
default:
|
default:
|
||||||
// If all else fails see if pgtype.Uuid can handle it. If so, translate through that.
|
// If all else fails see if pgtype.UUID can handle it. If so, translate through that.
|
||||||
pgUuid := &pgtype.Uuid{}
|
pgUUID := &pgtype.UUID{}
|
||||||
if err := pgUuid.Set(value); err != nil {
|
if err := pgUUID.Set(value); err != nil {
|
||||||
return fmt.Errorf("cannot convert %v to Uuid", value)
|
return fmt.Errorf("cannot convert %v to UUID", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst = Uuid{UUID: uuid.UUID(pgUuid.Bytes), Status: pgUuid.Status}
|
*dst = UUID{UUID: uuid.UUID(pgUUID.Bytes), Status: pgUUID.Status}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Uuid) Get() interface{} {
|
func (dst *UUID) Get() interface{} {
|
||||||
switch dst.Status {
|
switch dst.Status {
|
||||||
case pgtype.Present:
|
case pgtype.Present:
|
||||||
return dst.UUID
|
return dst.UUID
|
||||||
|
@ -58,7 +58,7 @@ func (dst *Uuid) Get() interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src *Uuid) AssignTo(dst interface{}) error {
|
func (src *UUID) AssignTo(dst interface{}) error {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case pgtype.Present:
|
case pgtype.Present:
|
||||||
switch v := dst.(type) {
|
switch v := dst.(type) {
|
||||||
|
@ -86,9 +86,9 @@ func (src *Uuid) AssignTo(dst interface{}) error {
|
||||||
return fmt.Errorf("cannot assign %v into %T", src, dst)
|
return fmt.Errorf("cannot assign %v into %T", src, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Uuid) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
func (dst *UUID) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
*dst = Uuid{Status: pgtype.Null}
|
*dst = UUID{Status: pgtype.Null}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,26 +97,26 @@ func (dst *Uuid) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst = Uuid{UUID: u, Status: pgtype.Present}
|
*dst = UUID{UUID: u, Status: pgtype.Present}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Uuid) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
func (dst *UUID) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
*dst = Uuid{Status: pgtype.Null}
|
*dst = UUID{Status: pgtype.Null}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(src) != 16 {
|
if len(src) != 16 {
|
||||||
return fmt.Errorf("invalid length for Uuid: %v", len(src))
|
return fmt.Errorf("invalid length for UUID: %v", len(src))
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst = Uuid{Status: pgtype.Present}
|
*dst = UUID{Status: pgtype.Present}
|
||||||
copy(dst.UUID[:], src)
|
copy(dst.UUID[:], src)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src *Uuid) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
func (src *UUID) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case pgtype.Null:
|
case pgtype.Null:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -127,7 +127,7 @@ func (src *Uuid) EncodeText(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||||
return append(buf, src.UUID.String()...), nil
|
return append(buf, src.UUID.String()...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src *Uuid) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
func (src *UUID) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case pgtype.Null:
|
case pgtype.Null:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -139,9 +139,9 @@ func (src *Uuid) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan implements the database/sql Scanner interface.
|
// Scan implements the database/sql Scanner interface.
|
||||||
func (dst *Uuid) Scan(src interface{}) error {
|
func (dst *UUID) Scan(src interface{}) error {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
*dst = Uuid{Status: pgtype.Null}
|
*dst = UUID{Status: pgtype.Null}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,6 +156,6 @@ func (dst *Uuid) Scan(src interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value implements the database/sql/driver Valuer interface.
|
// Value implements the database/sql/driver Valuer interface.
|
||||||
func (src *Uuid) Value() (driver.Value, error) {
|
func (src *UUID) Value() (driver.Value, error) {
|
||||||
return pgtype.EncodeValueText(src)
|
return pgtype.EncodeValueText(src)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,34 +9,34 @@ import (
|
||||||
"github.com/jackc/pgx/pgtype/testutil"
|
"github.com/jackc/pgx/pgtype/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUuidTranscode(t *testing.T) {
|
func TestUUIDTranscode(t *testing.T) {
|
||||||
testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{
|
testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{
|
||||||
&satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
&satori.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
&satori.Uuid{Status: pgtype.Null},
|
&satori.UUID{Status: pgtype.Null},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUuidSet(t *testing.T) {
|
func TestUUIDSet(t *testing.T) {
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
source interface{}
|
source interface{}
|
||||||
result satori.Uuid
|
result satori.UUID
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
source: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
source: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
||||||
result: satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
result: satori.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
||||||
result: satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
result: satori.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
source: "00010203-0405-0607-0809-0a0b0c0d0e0f",
|
source: "00010203-0405-0607-0809-0a0b0c0d0e0f",
|
||||||
result: satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
result: satori.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range successfulTests {
|
for i, tt := range successfulTests {
|
||||||
var r satori.Uuid
|
var r satori.UUID
|
||||||
err := r.Set(tt.source)
|
err := r.Set(tt.source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%d: %v", i, err)
|
t.Errorf("%d: %v", i, err)
|
||||||
|
@ -48,9 +48,9 @@ func TestUuidSet(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUuidAssignTo(t *testing.T) {
|
func TestUUIDAssignTo(t *testing.T) {
|
||||||
{
|
{
|
||||||
src := satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
src := satori.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
var dst [16]byte
|
var dst [16]byte
|
||||||
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ func TestUuidAssignTo(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
src := satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
src := satori.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
var dst []byte
|
var dst []byte
|
||||||
expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func TestUuidAssignTo(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
src := satori.Uuid{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
src := satori.UUID{UUID: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
var dst string
|
var dst string
|
||||||
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ const (
|
||||||
TimestamptzOID = 1184
|
TimestamptzOID = 1184
|
||||||
TimestamptzArrayOID = 1185
|
TimestamptzArrayOID = 1185
|
||||||
RecordOID = 2249
|
RecordOID = 2249
|
||||||
UuidOID = 2950
|
UUIDOID = 2950
|
||||||
JsonbOID = 3802
|
JsonbOID = 3802
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -262,7 +262,7 @@ func init() {
|
||||||
"tsrange": &Tsrange{},
|
"tsrange": &Tsrange{},
|
||||||
"tstzrange": &Tstzrange{},
|
"tstzrange": &Tstzrange{},
|
||||||
"unknown": &Unknown{},
|
"unknown": &Unknown{},
|
||||||
"uuid": &Uuid{},
|
"uuid": &UUID{},
|
||||||
"varbit": &Varbit{},
|
"varbit": &Varbit{},
|
||||||
"varchar": &Varchar{},
|
"varchar": &Varchar{},
|
||||||
"xid": &Xid{},
|
"xid": &Xid{},
|
||||||
|
|
|
@ -6,38 +6,38 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Uuid struct {
|
type UUID struct {
|
||||||
Bytes [16]byte
|
Bytes [16]byte
|
||||||
Status Status
|
Status Status
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Uuid) Set(src interface{}) error {
|
func (dst *UUID) Set(src interface{}) error {
|
||||||
switch value := src.(type) {
|
switch value := src.(type) {
|
||||||
case [16]byte:
|
case [16]byte:
|
||||||
*dst = Uuid{Bytes: value, Status: Present}
|
*dst = UUID{Bytes: value, Status: Present}
|
||||||
case []byte:
|
case []byte:
|
||||||
if len(value) != 16 {
|
if len(value) != 16 {
|
||||||
return fmt.Errorf("[]byte must be 16 bytes to convert to Uuid: %d", len(value))
|
return fmt.Errorf("[]byte must be 16 bytes to convert to UUID: %d", len(value))
|
||||||
}
|
}
|
||||||
*dst = Uuid{Status: Present}
|
*dst = UUID{Status: Present}
|
||||||
copy(dst.Bytes[:], value)
|
copy(dst.Bytes[:], value)
|
||||||
case string:
|
case string:
|
||||||
uuid, err := parseUuid(value)
|
uuid, err := parseUUID(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*dst = Uuid{Bytes: uuid, Status: Present}
|
*dst = UUID{Bytes: uuid, Status: Present}
|
||||||
default:
|
default:
|
||||||
if originalSrc, ok := underlyingPtrType(src); ok {
|
if originalSrc, ok := underlyingPtrType(src); ok {
|
||||||
return dst.Set(originalSrc)
|
return dst.Set(originalSrc)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("cannot convert %v to Uuid", value)
|
return fmt.Errorf("cannot convert %v to UUID", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Uuid) Get() interface{} {
|
func (dst *UUID) Get() interface{} {
|
||||||
switch dst.Status {
|
switch dst.Status {
|
||||||
case Present:
|
case Present:
|
||||||
return dst.Bytes
|
return dst.Bytes
|
||||||
|
@ -48,7 +48,7 @@ func (dst *Uuid) Get() interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src *Uuid) AssignTo(dst interface{}) error {
|
func (src *UUID) AssignTo(dst interface{}) error {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case Present:
|
case Present:
|
||||||
switch v := dst.(type) {
|
switch v := dst.(type) {
|
||||||
|
@ -60,7 +60,7 @@ func (src *Uuid) AssignTo(dst interface{}) error {
|
||||||
copy(*v, src.Bytes[:])
|
copy(*v, src.Bytes[:])
|
||||||
return nil
|
return nil
|
||||||
case *string:
|
case *string:
|
||||||
*v = encodeUuid(src.Bytes)
|
*v = encodeUUID(src.Bytes)
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
if nextDst, retry := GetAssignToDstType(v); retry {
|
if nextDst, retry := GetAssignToDstType(v); retry {
|
||||||
|
@ -74,8 +74,8 @@ func (src *Uuid) AssignTo(dst interface{}) error {
|
||||||
return fmt.Errorf("cannot assign %v into %T", src, dst)
|
return fmt.Errorf("cannot assign %v into %T", src, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseUuid converts a string UUID in standard form to a byte array.
|
// parseUUID converts a string UUID in standard form to a byte array.
|
||||||
func parseUuid(src string) (dst [16]byte, err error) {
|
func parseUUID(src string) (dst [16]byte, err error) {
|
||||||
src = src[0:8] + src[9:13] + src[14:18] + src[19:23] + src[24:]
|
src = src[0:8] + src[9:13] + src[14:18] + src[19:23] + src[24:]
|
||||||
buf, err := hex.DecodeString(src)
|
buf, err := hex.DecodeString(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -86,46 +86,46 @@ func parseUuid(src string) (dst [16]byte, err error) {
|
||||||
return dst, err
|
return dst, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// encodeUuid converts a uuid byte array to UUID standard string form.
|
// encodeUUID converts a uuid byte array to UUID standard string form.
|
||||||
func encodeUuid(src [16]byte) string {
|
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])
|
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 {
|
func (dst *UUID) DecodeText(ci *ConnInfo, src []byte) error {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
*dst = Uuid{Status: Null}
|
*dst = UUID{Status: Null}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(src) != 36 {
|
if len(src) != 36 {
|
||||||
return fmt.Errorf("invalid length for Uuid: %v", len(src))
|
return fmt.Errorf("invalid length for UUID: %v", len(src))
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := parseUuid(string(src))
|
buf, err := parseUUID(string(src))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst = Uuid{Bytes: buf, Status: Present}
|
*dst = UUID{Bytes: buf, Status: Present}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dst *Uuid) DecodeBinary(ci *ConnInfo, src []byte) error {
|
func (dst *UUID) DecodeBinary(ci *ConnInfo, src []byte) error {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
*dst = Uuid{Status: Null}
|
*dst = UUID{Status: Null}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(src) != 16 {
|
if len(src) != 16 {
|
||||||
return fmt.Errorf("invalid length for Uuid: %v", len(src))
|
return fmt.Errorf("invalid length for UUID: %v", len(src))
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst = Uuid{Status: Present}
|
*dst = UUID{Status: Present}
|
||||||
copy(dst.Bytes[:], src)
|
copy(dst.Bytes[:], src)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src *Uuid) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
|
func (src *UUID) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case Null:
|
case Null:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -133,10 +133,10 @@ func (src *Uuid) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||||
return nil, errUndefined
|
return nil, errUndefined
|
||||||
}
|
}
|
||||||
|
|
||||||
return append(buf, encodeUuid(src.Bytes)...), nil
|
return append(buf, encodeUUID(src.Bytes)...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (src *Uuid) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
|
func (src *UUID) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||||
switch src.Status {
|
switch src.Status {
|
||||||
case Null:
|
case Null:
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -148,9 +148,9 @@ func (src *Uuid) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan implements the database/sql Scanner interface.
|
// Scan implements the database/sql Scanner interface.
|
||||||
func (dst *Uuid) Scan(src interface{}) error {
|
func (dst *UUID) Scan(src interface{}) error {
|
||||||
if src == nil {
|
if src == nil {
|
||||||
*dst = Uuid{Status: Null}
|
*dst = UUID{Status: Null}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,6 +167,6 @@ func (dst *Uuid) Scan(src interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value implements the database/sql/driver Valuer interface.
|
// Value implements the database/sql/driver Valuer interface.
|
||||||
func (src *Uuid) Value() (driver.Value, error) {
|
func (src *UUID) Value() (driver.Value, error) {
|
||||||
return EncodeValueText(src)
|
return EncodeValueText(src)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,34 +8,34 @@ import (
|
||||||
"github.com/jackc/pgx/pgtype/testutil"
|
"github.com/jackc/pgx/pgtype/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUuidTranscode(t *testing.T) {
|
func TestUUIDTranscode(t *testing.T) {
|
||||||
testutil.TestSuccessfulTranscode(t, "uuid", []interface{}{
|
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}, Status: pgtype.Present},
|
&pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
&pgtype.Uuid{Status: pgtype.Null},
|
&pgtype.UUID{Status: pgtype.Null},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUuidSet(t *testing.T) {
|
func TestUUIDSet(t *testing.T) {
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
source interface{}
|
source interface{}
|
||||||
result pgtype.Uuid
|
result pgtype.UUID
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
source: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
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}, Status: pgtype.Present},
|
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
source: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
|
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}, Status: pgtype.Present},
|
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
source: "00010203-0405-0607-0809-0a0b0c0d0e0f",
|
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}, Status: pgtype.Present},
|
result: pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range successfulTests {
|
for i, tt := range successfulTests {
|
||||||
var r pgtype.Uuid
|
var r pgtype.UUID
|
||||||
err := r.Set(tt.source)
|
err := r.Set(tt.source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%d: %v", i, err)
|
t.Errorf("%d: %v", i, err)
|
||||||
|
@ -47,9 +47,9 @@ func TestUuidSet(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUuidAssignTo(t *testing.T) {
|
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}, Status: pgtype.Present}
|
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
var dst [16]byte
|
var dst [16]byte
|
||||||
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
expected := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ 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}, Status: pgtype.Present}
|
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
var dst []byte
|
var dst []byte
|
||||||
expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
expected := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ 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}, Status: pgtype.Present}
|
src := pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Status: pgtype.Present}
|
||||||
var dst string
|
var dst string
|
||||||
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue