From 5767fd05fcfa3c4569d3119ccf916c0c473ec01e Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Sat, 5 Oct 2024 03:52:15 -0700 Subject: [PATCH 1/2] add MarshalAppend for guid --- utils/guid/id.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/utils/guid/id.go b/utils/guid/id.go index 0b6bd175..38455245 100644 --- a/utils/guid/id.go +++ b/utils/guid/id.go @@ -21,6 +21,7 @@ import ( mrand "math/rand/v2" "os" "sync" + "unsafe" "github.com/jxskiss/base62" "github.com/lithammer/shortuuid/v4" @@ -150,7 +151,11 @@ func guidPrefix[T livekit.Guid]() string { } func Marshal[T livekit.Guid](id T) livekit.GuidBlock { - var b livekit.GuidBlock + return livekit.GuidBlock(MarshalAppend(nil, id)) +} + +func MarshalAppend[T livekit.Guid](b []byte, id T) []byte { + b = append(b, make([]byte, 9)...) idb := []byte(id)[len(id)-Size:] for i := 0; i < 3; i++ { j := i * 3 @@ -175,5 +180,5 @@ func Unmarshal[T livekit.Guid](b livekit.GuidBlock) T { idb[k+2] = b57Chars[(b[j+1]&15)<<2|b[j+2]>>6] idb[k+3] = b57Chars[b[j+2]&63] } - return T(id) + return T(unsafe.String(unsafe.SliceData(idb), len(idb))) } From 5ee163f0d6e1a9f4739d3cd5cd4f0565064a3b97 Mon Sep 17 00:00:00 2001 From: Paul Wells Date: Sat, 5 Oct 2024 03:54:18 -0700 Subject: [PATCH 2/2] wrong bytes --- utils/guid/id.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/guid/id.go b/utils/guid/id.go index 38455245..4929face 100644 --- a/utils/guid/id.go +++ b/utils/guid/id.go @@ -180,5 +180,5 @@ func Unmarshal[T livekit.Guid](b livekit.GuidBlock) T { idb[k+2] = b57Chars[(b[j+1]&15)<<2|b[j+2]>>6] idb[k+3] = b57Chars[b[j+2]&63] } - return T(unsafe.String(unsafe.SliceData(idb), len(idb))) + return T(unsafe.String(unsafe.SliceData(id), len(id))) }