Skip to content

Commit 6b2f2a4

Browse files
committed
feat: Add LittleEndian transform.
Allows the transformation into legacy Microsoft GUID little endian byte ordering.
1 parent 2d3c2a9 commit 6b2f2a4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

uuid.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ func encodeHex(dst []byte, uuid UUID) {
296296
hex.Encode(dst[24:], uuid[10:])
297297
}
298298

299+
// LittleEndian returns the UUID in GUID little endian byte order.
300+
func (uuid UUID) LittleEndian() []byte {
301+
mixed := []byte{uuid[3], uuid[2], uuid[1], uuid[0], uuid[5], uuid[4], uuid[7], uuid[6]}
302+
return append(mixed, uuid[8:]...)
303+
}
304+
299305
// Variant returns the variant encoded in uuid.
300306
func (uuid UUID) Variant() Variant {
301307
switch {

uuid_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,3 +928,30 @@ func TestVersion7MonotonicityStrict(t *testing.T) {
928928
u1 = u2
929929
}
930930
}
931+
932+
func TestLittleEndian(t *testing.T) {
933+
tcs := []struct {
934+
in string
935+
want []byte
936+
}{
937+
{
938+
in: "7d444840-9dc0-11d1-b245-5ffdce74fad2",
939+
want: []byte{0x40, 0x48, 0x44, 0x7d, 0xc0, 0x9d, 0xd1, 0x11, 0xb2, 0x45, 0x5f, 0xfd, 0xce, 0x74, 0xfa, 0xd2}},
940+
{
941+
in: "f47ac10b-58cc-0372-8567-0e02b2c3d479",
942+
want: []byte{0x0b, 0xc1, 0x7a, 0xf4, 0xcc, 0x58, 0x72, 0x03, 0x85, 0x67, 0x0e, 0x02, 0xb2, 0xc3, 0xd4, 0x79},
943+
},
944+
}
945+
for _, tc := range tcs {
946+
t.Run("LittleEndian "+tc.in, func(t *testing.T) {
947+
u, err := Parse(tc.in)
948+
if err != nil {
949+
t.Fatal(err)
950+
}
951+
got := u.LittleEndian()
952+
if !bytes.Equal(got, tc.want) {
953+
t.Fatalf("%s.LittleEndian() = %v, want %v", tc.in, got, tc.want)
954+
}
955+
})
956+
}
957+
}

0 commit comments

Comments
 (0)