From 11c2b4a3a62ba0dee48ee82684e34391f451bba4 Mon Sep 17 00:00:00 2001 From: Jan Saidl Date: Thu, 7 Mar 2024 09:28:56 +0100 Subject: [PATCH] fix uuid generator --- src/uuid/uuid.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/uuid/uuid.go b/src/uuid/uuid.go index a8fb7410..6d76ea58 100644 --- a/src/uuid/uuid.go +++ b/src/uuid/uuid.go @@ -2,15 +2,21 @@ package uuid import ( "encoding/base64" + "strings" "github.com/google/uuid" ) -const encodeUUID = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB" - -var encoding = base64.NewEncoding(encodeUUID) - func GetShort() string { x := uuid.New() - return encoding.EncodeToString(x[:])[0:22] + return encode(x[:]) +} + +func encode(uuid []byte) string { + b64 := base64.RawURLEncoding.EncodeToString(uuid) + // TODO(tikinang): Fix for 1.22, improve. + b64 = strings.ReplaceAll(b64, "-", "A") + b64 = strings.ReplaceAll(b64, "_", "B") + // Should already be 22 chars, just making sure if creators of base64 package change their mind. + return b64[:22] }