Use Go casing convention for UUID

v3-ann
Jack Christensen 2017-06-03 11:54:57 -05:00
parent 27ab289096
commit 3bdc94cee2
5 changed files with 84 additions and 84 deletions

View File

@ -11,43 +11,43 @@ import (
var errUndefined = errors.New("cannot encode status undefined")
type Uuid struct {
type UUID struct {
UUID uuid.UUID
Status pgtype.Status
}
func (dst *Uuid) Set(src interface{}) error {
func (dst *UUID) Set(src interface{}) error {
switch value := src.(type) {
case uuid.UUID:
*dst = Uuid{UUID: value, Status: pgtype.Present}
*dst = UUID{UUID: value, Status: pgtype.Present}
case [16]byte:
*dst = Uuid{UUID: uuid.UUID(value), Status: pgtype.Present}
*dst = UUID{UUID: uuid.UUID(value), Status: pgtype.Present}
case []byte:
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)
case string:
uuid, err := uuid.FromString(value)
if err != nil {
return err
}
*dst = Uuid{UUID: uuid, Status: pgtype.Present}
*dst = UUID{UUID: uuid, Status: pgtype.Present}
default:
// If all else fails see if pgtype.Uuid can handle it. If so, translate through that.
pgUuid := &pgtype.Uuid{}
if err := pgUuid.Set(value); err != nil {
return fmt.Errorf("cannot convert %v to Uuid", value)
// If all else fails see if pgtype.UUID can handle it. If so, translate through that.
pgUUID := &pgtype.UUID{}
if err := pgUUID.Set(value); err != nil {
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
}
func (dst *Uuid) Get() interface{} {
func (dst *UUID) Get() interface{} {
switch dst.Status {
case pgtype.Present:
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 {
case pgtype.Present:
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)
}
func (dst *Uuid) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
func (dst *UUID) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
if src == nil {
*dst = Uuid{Status: pgtype.Null}
*dst = UUID{Status: pgtype.Null}
return nil
}
@ -97,26 +97,26 @@ func (dst *Uuid) DecodeText(ci *pgtype.ConnInfo, src []byte) error {
return err
}
*dst = Uuid{UUID: u, Status: pgtype.Present}
*dst = UUID{UUID: u, Status: pgtype.Present}
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 {
*dst = Uuid{Status: pgtype.Null}
*dst = UUID{Status: pgtype.Null}
return nil
}
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)
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 {
case pgtype.Null:
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
}
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 {
case pgtype.Null:
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.
func (dst *Uuid) Scan(src interface{}) error {
func (dst *UUID) Scan(src interface{}) error {
if src == nil {
*dst = Uuid{Status: pgtype.Null}
*dst = UUID{Status: pgtype.Null}
return nil
}
@ -156,6 +156,6 @@ func (dst *Uuid) Scan(src interface{}) error {
}
// 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)
}

View File

@ -9,34 +9,34 @@ import (
"github.com/jackc/pgx/pgtype/testutil"
)
func TestUuidTranscode(t *testing.T) {
func TestUUIDTranscode(t *testing.T) {
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{Status: pgtype.Null},
&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},
})
}
func TestUuidSet(t *testing.T) {
func TestUUIDSet(t *testing.T) {
successfulTests := []struct {
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},
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},
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",
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 {
var r satori.Uuid
var r satori.UUID
err := r.Set(tt.source)
if err != nil {
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
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
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
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"

View File

@ -46,7 +46,7 @@ const (
TimestamptzOID = 1184
TimestamptzArrayOID = 1185
RecordOID = 2249
UuidOID = 2950
UUIDOID = 2950
JsonbOID = 3802
)
@ -262,7 +262,7 @@ func init() {
"tsrange": &Tsrange{},
"tstzrange": &Tstzrange{},
"unknown": &Unknown{},
"uuid": &Uuid{},
"uuid": &UUID{},
"varbit": &Varbit{},
"varchar": &Varchar{},
"xid": &Xid{},

View File

@ -6,38 +6,38 @@ import (
"fmt"
)
type Uuid struct {
type UUID struct {
Bytes [16]byte
Status Status
}
func (dst *Uuid) Set(src interface{}) error {
func (dst *UUID) Set(src interface{}) error {
switch value := src.(type) {
case [16]byte:
*dst = Uuid{Bytes: value, Status: Present}
*dst = UUID{Bytes: value, Status: Present}
case []byte:
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)
case string:
uuid, err := parseUuid(value)
uuid, err := parseUUID(value)
if err != nil {
return err
}
*dst = Uuid{Bytes: uuid, Status: Present}
*dst = UUID{Bytes: uuid, Status: Present}
default:
if originalSrc, ok := underlyingPtrType(src); ok {
return dst.Set(originalSrc)
}
return fmt.Errorf("cannot convert %v to Uuid", value)
return fmt.Errorf("cannot convert %v to UUID", value)
}
return nil
}
func (dst *Uuid) Get() interface{} {
func (dst *UUID) Get() interface{} {
switch dst.Status {
case Present:
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 {
case Present:
switch v := dst.(type) {
@ -60,7 +60,7 @@ func (src *Uuid) AssignTo(dst interface{}) error {
copy(*v, src.Bytes[:])
return nil
case *string:
*v = encodeUuid(src.Bytes)
*v = encodeUUID(src.Bytes)
return nil
default:
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)
}
// parseUuid converts a string UUID in standard form to a byte array.
func parseUuid(src string) (dst [16]byte, err error) {
// parseUUID converts a string UUID in standard form to a byte array.
func parseUUID(src string) (dst [16]byte, err error) {
src = src[0:8] + src[9:13] + src[14:18] + src[19:23] + src[24:]
buf, err := hex.DecodeString(src)
if err != nil {
@ -86,46 +86,46 @@ func parseUuid(src string) (dst [16]byte, err error) {
return dst, err
}
// encodeUuid converts a uuid byte array to UUID standard string form.
func encodeUuid(src [16]byte) string {
// encodeUUID converts a uuid byte array to UUID standard string form.
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 {
func (dst *UUID) DecodeText(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = Uuid{Status: Null}
*dst = UUID{Status: Null}
return nil
}
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 {
return err
}
*dst = Uuid{Bytes: buf, Status: Present}
*dst = UUID{Bytes: buf, Status: Present}
return nil
}
func (dst *Uuid) DecodeBinary(ci *ConnInfo, src []byte) error {
func (dst *UUID) DecodeBinary(ci *ConnInfo, src []byte) error {
if src == nil {
*dst = Uuid{Status: Null}
*dst = UUID{Status: Null}
return nil
}
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)
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 {
case Null:
return nil, nil
@ -133,10 +133,10 @@ func (src *Uuid) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
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 {
case Null:
return nil, nil
@ -148,9 +148,9 @@ func (src *Uuid) EncodeBinary(ci *ConnInfo, buf []byte) ([]byte, error) {
}
// Scan implements the database/sql Scanner interface.
func (dst *Uuid) Scan(src interface{}) error {
func (dst *UUID) Scan(src interface{}) error {
if src == nil {
*dst = Uuid{Status: Null}
*dst = UUID{Status: Null}
return nil
}
@ -167,6 +167,6 @@ func (dst *Uuid) Scan(src interface{}) error {
}
// 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)
}

View File

@ -8,34 +8,34 @@ import (
"github.com/jackc/pgx/pgtype/testutil"
)
func TestUuidTranscode(t *testing.T) {
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}, Status: pgtype.Present},
&pgtype.Uuid{Status: pgtype.Null},
&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},
})
}
func TestUuidSet(t *testing.T) {
func TestUUIDSet(t *testing.T) {
successfulTests := []struct {
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},
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},
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",
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 {
var r pgtype.Uuid
var r pgtype.UUID
err := r.Set(tt.source)
if err != nil {
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
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
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
expected := "00010203-0405-0607-0809-0a0b0c0d0e0f"