Skip to content

Commit 2ec9004

Browse files
authored
Merge pull request #2145 from grachevko/string
Implement pgtype.UUID.String()
2 parents 3f84e89 + 8723855 commit 2ec9004

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pgtype/uuid.go

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func (src UUID) Value() (driver.Value, error) {
9696
return encodeUUID(src.Bytes), nil
9797
}
9898

99+
func (src UUID) String() string {
100+
if !src.Valid {
101+
return ""
102+
}
103+
104+
return encodeUUID(src.Bytes)
105+
}
106+
99107
func (src UUID) MarshalJSON() ([]byte, error) {
100108
if !src.Valid {
101109
return []byte("null"), nil

pgtype/uuid_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ func TestUUIDCodec(t *testing.T) {
6363
})
6464
}
6565

66+
func TestUUID_String(t *testing.T) {
67+
tests := []struct {
68+
name string
69+
src pgtype.UUID
70+
want string
71+
}{
72+
{
73+
name: "first",
74+
src: pgtype.UUID{
75+
Bytes: [16]byte{29, 72, 90, 122, 109, 24, 69, 153, 140, 108, 52, 66, 86, 22, 136, 122},
76+
Valid: true,
77+
},
78+
want: "1d485a7a-6d18-4599-8c6c-34425616887a",
79+
},
80+
{
81+
name: "third",
82+
src: pgtype.UUID{
83+
Bytes: [16]byte{},
84+
},
85+
want: "",
86+
},
87+
}
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
got := tt.src.String()
91+
if !reflect.DeepEqual(got, tt.want) {
92+
t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want)
93+
}
94+
})
95+
}
96+
}
97+
6698
func TestUUID_MarshalJSON(t *testing.T) {
6799
tests := []struct {
68100
name string

0 commit comments

Comments
 (0)