Skip to content

Commit

Permalink
Merge pull request #129 from Niev/fix/uuid_read_write_bytes_order
Browse files Browse the repository at this point in the history
swap order of UUID bytes 0-7 and 8-15 when reading and writing to clickhouse
  • Loading branch information
kshvakov committed Oct 9, 2018
2 parents 7f127e4 + b0e680e commit 3414b60
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/column/uuid.go
Expand Up @@ -23,6 +23,9 @@ func (*UUID) Read(decoder *binary.Decoder) (interface{}, error) {
if err != nil {
return "", err
}

src = swap(src)

var uuid [36]byte
{
hex.Encode(uuid[:], src[:4])
Expand Down Expand Up @@ -56,12 +59,28 @@ func (u *UUID) Write(encoder *binary.Encoder, v interface{}) (err error) {
Column: u,
}
}

uuid = swap(uuid)

if _, err := encoder.Write(uuid); err != nil {
return err
}
return nil
}

func swap(src []byte) []byte {
_ = src[15]
src[0], src[7] = src[7], src[0]
src[1], src[6] = src[6], src[1]
src[2], src[5] = src[5], src[2]
src[3], src[4] = src[4], src[3]
src[8], src[15] = src[15], src[8]
src[9], src[14] = src[14], src[9]
src[10], src[13] = src[13], src[10]
src[11], src[12] = src[12], src[11]
return src
}

func uuid2bytes(str string) ([]byte, error) {
var uuid [16]byte
if str[8] != '-' || str[13] != '-' || str[18] != '-' || str[23] != '-' {
Expand Down

0 comments on commit 3414b60

Please sign in to comment.