Skip to content

Commit

Permalink
Implement pgtype.UUID.String()
Browse files Browse the repository at this point in the history
  • Loading branch information
grachevko committed Oct 9, 2024
1 parent 3f84e89 commit 8723855
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pgtype/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func (src UUID) Value() (driver.Value, error) {
return encodeUUID(src.Bytes), nil
}

func (src UUID) String() string {
if !src.Valid {
return ""
}

return encodeUUID(src.Bytes)
}

func (src UUID) MarshalJSON() ([]byte, error) {
if !src.Valid {
return []byte("null"), nil
Expand Down
32 changes: 32 additions & 0 deletions pgtype/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ func TestUUIDCodec(t *testing.T) {
})
}

func TestUUID_String(t *testing.T) {
tests := []struct {
name string
src pgtype.UUID
want string
}{
{
name: "first",
src: pgtype.UUID{
Bytes: [16]byte{29, 72, 90, 122, 109, 24, 69, 153, 140, 108, 52, 66, 86, 22, 136, 122},
Valid: true,
},
want: "1d485a7a-6d18-4599-8c6c-34425616887a",
},
{
name: "third",
src: pgtype.UUID{
Bytes: [16]byte{},
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.src.String()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want)
}
})
}
}

func TestUUID_MarshalJSON(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 8723855

Please sign in to comment.