Skip to content

Commit

Permalink
Merge pull request #24 from AelitaStyles/fix-uuid
Browse files Browse the repository at this point in the history
Fix UUID being read/written incorrectly #26
  • Loading branch information
Aleksandr Sokolovskii authored Jan 4, 2019
2 parents 1142dac + d658c7c commit 9561946
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions binary/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ func ToTime(t time.Time) Time {
return Time(t3)
}

// Flips a UUID buffer into the right order
func uuidFlip(id *uuid.UUID) {
for i := 3; i >= 0; i-- {
opp := 7-i
id[i], id[opp] = id[opp], id[i]
}
for i := 3; i >= 0; i-- {
opp := 15-i
id[i+8], id[opp] = id[opp], id[i+8]
}
}

// WriteType writes object type code
func WriteType(w io.Writer, code byte) error {
return WriteByte(w, code)
Expand Down Expand Up @@ -250,6 +262,7 @@ func WriteOUUID(w io.Writer, v uuid.UUID) error {
if err := WriteType(w, typeUUID); err != nil {
return err
}
uuidFlip(&v)
return binary.Write(w, binary.LittleEndian, v)
}

Expand Down Expand Up @@ -732,6 +745,7 @@ func ReadOString(r io.Reader) (string, error) {
func ReadUUID(r io.Reader) (uuid.UUID, error) {
var o uuid.UUID
err := binary.Read(r, binary.LittleEndian, &o)
uuidFlip(&o)
return o, err
}

Expand Down

0 comments on commit 9561946

Please sign in to comment.