Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/kshvakov/clickhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
kshvakov committed Aug 7, 2019
2 parents e4ba95f + d6cb182 commit a5f69ea
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
11 changes: 10 additions & 1 deletion lib/column/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"github.com/kshvakov/clickhouse/lib/binary"
)

const UUIDLen = 16
const (
UUIDLen = 16
NullUUID = "00000000-0000-0000-0000-000000000000"
)

var ErrInvalidUUIDFormat = errors.New("invalid UUID format")

Expand Down Expand Up @@ -84,6 +87,12 @@ func swap(src []byte) []byte {

func uuid2bytes(str string) ([]byte, error) {
var uuid [16]byte
strLength := len(str)
if strLength == 0 {
str = NullUUID
} else if strLength != 36 {
return nil, ErrInvalidUUIDFormat
}
if str[8] != '-' || str[13] != '-' || str[18] != '-' || str[23] != '-' {
return nil, ErrInvalidUUIDFormat
}
Expand Down
69 changes: 56 additions & 13 deletions lib/column/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
"testing"
)

func bytes2uuid(src []byte) string {
var uuid [36]byte
hex.Encode(uuid[:], src[:4])
uuid[8] = '-'
hex.Encode(uuid[9:13], src[4:6])
uuid[13] = '-'
hex.Encode(uuid[14:18], src[6:8])
uuid[18] = '-'
hex.Encode(uuid[19:23], src[8:10])
uuid[23] = '-'
hex.Encode(uuid[24:], src[10:])
return string(uuid[:])
}

func Test_UUID2Bytes(t *testing.T) {
bytes2uuid := func(src []byte) string {
var uuid [36]byte
hex.Encode(uuid[:], src[:4])
uuid[8] = '-'
hex.Encode(uuid[9:13], src[4:6])
uuid[13] = '-'
hex.Encode(uuid[14:18], src[6:8])
uuid[18] = '-'
hex.Encode(uuid[19:23], src[8:10])
uuid[23] = '-'
hex.Encode(uuid[24:], src[10:])
return string(uuid[:])
}
origin := "00000000-0000-0000-0000-000000000000"
if uuid, err := uuid2bytes(origin); assert.NoError(t, err) {
assert.Equal(t, origin, bytes2uuid(uuid))
Expand All @@ -35,3 +36,45 @@ func Benchmark_UUID2Bytes(b *testing.B) {
}
}
}

func Test_EmptyStringToNullUUID(t *testing.T) {
origin := ""
if uuid, err := uuid2bytes(origin); assert.NoError(t, err) {
assert.Equal(t, "00000000-0000-0000-0000-000000000000", bytes2uuid(uuid))
}
}

func Test_ErrInvalidUUIDFormat(t *testing.T) {
cases := []struct {
origin string
exceptedError error
}{
{
"",
nil,
},
{
"a",
ErrInvalidUUIDFormat,
},
{
"00000000-0000-0000-00000000000000000",
ErrInvalidUUIDFormat,
},
{
"00000000-0000-0000-0000-0000000000000",
ErrInvalidUUIDFormat,
},
}

for _, Case := range cases {
_, err := uuid2bytes(Case.origin)
if Case.exceptedError != nil {
assert.Error(t, err)
assert.EqualError(t, Case.exceptedError, err.Error())
} else {
assert.NoError(t, err)
}

}
}

0 comments on commit a5f69ea

Please sign in to comment.