From e969cff5f9006d4374b3fef5387a88d744766240 Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Wed, 9 Oct 2024 16:32:37 -0700 Subject: [PATCH] fix guid append write offset --- utils/guid/id.go | 3 ++- utils/guid/id_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/utils/guid/id.go b/utils/guid/id.go index 2ffd852e..89da4e41 100644 --- a/utils/guid/id.go +++ b/utils/guid/id.go @@ -155,10 +155,11 @@ func Marshal[T livekit.Guid](id T) livekit.GuidBlock { } func MarshalAppend[T livekit.Guid](b []byte, id T) []byte { + off := len(b) b = append(b, make([]byte, Size*3/4)...) idb := []byte(id)[len(id)-Size:] for i := 0; i < 3; i++ { - j := i * 3 + j := i*3 + off k := i * 4 b[j] = b57Index[idb[k]]<<2 | b57Index[idb[k+1]]>>4 b[j+1] = b57Index[idb[k+1]]<<4 | b57Index[idb[k+2]]>>2 diff --git a/utils/guid/id_test.go b/utils/guid/id_test.go index d824524d..a03f46b4 100644 --- a/utils/guid/id_test.go +++ b/utils/guid/id_test.go @@ -31,6 +31,16 @@ func TestMarshalUnmarshal(t *testing.T) { require.EqualValues(t, b0, b1) } +func TestMarshalAppend(t *testing.T) { + id0 := livekit.RoomID(New(RoomPrefix)) + id1 := livekit.RoomID(New(RoomPrefix)) + var b []byte + b = MarshalAppend(b, id0) + b = MarshalAppend(b, id1) + require.Equal(t, id0, Unmarshal[livekit.RoomID](livekit.GuidBlock(b[0:]))) + require.Equal(t, id1, Unmarshal[livekit.RoomID](livekit.GuidBlock(b[9:]))) +} + func BenchmarkNew(b *testing.B) { b.Run("new", func(b *testing.B) { var guid string