This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
321 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
const B32_CHARACTERS = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" | ||
|
||
func GenerateUUID() string { | ||
return uuid.NewString() | ||
} | ||
|
||
func UUIDtoULID(uuidStr string) (string, error) { | ||
_, err := uuid.Parse(uuidStr) | ||
if err != nil { | ||
return "", err | ||
} | ||
uuidStr = strings.ReplaceAll(uuidStr, "-", "") | ||
uuidBytes, _ := hex.DecodeString(uuidStr) | ||
|
||
return crockfordEncode(uuidBytes), nil | ||
} | ||
|
||
func crockfordEncode(input []byte) string { | ||
var output []int | ||
bitsRead := 0 | ||
buffer := 0 | ||
|
||
// Work from the end of the buffer | ||
reversedInput := reverseBuffer(input) | ||
|
||
for _, byteVal := range reversedInput { | ||
// Add current byte to start of buffer | ||
buffer |= int(byteVal) << bitsRead | ||
bitsRead += 8 | ||
|
||
for bitsRead >= 5 { | ||
output = append([]int{buffer & 0x1f}, output...) | ||
buffer >>= 5 | ||
bitsRead -= 5 | ||
} | ||
} | ||
|
||
if bitsRead > 0 { | ||
output = append([]int{buffer & 0x1f}, output...) | ||
} | ||
|
||
var resultBuffer bytes.Buffer | ||
for _, byteVal := range output { | ||
resultBuffer.WriteString(string(B32_CHARACTERS[byteVal])) | ||
} | ||
|
||
return resultBuffer.String() | ||
} | ||
|
||
func reverseBuffer(input []byte) []byte { | ||
length := len(input) | ||
reversed := make([]byte, length) | ||
for i := 0; i < length; i++ { | ||
reversed[i] = input[length-i-1] | ||
} | ||
return reversed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGenerateUUID(t *testing.T) { | ||
guid := GenerateUUID() | ||
if len(guid) != 36 { | ||
t.Errorf("Expected GUID length to be 36, but got %d", len(guid)) | ||
} | ||
} | ||
|
||
func TestUUIDtoULID(t *testing.T) { | ||
t.Run("Valid UUID", func(t *testing.T) { | ||
uuid := "860d3040-1e23-416f-a1fd-f4ccc773833d" | ||
expectedULID := "461MR407H385QT3ZFMSK3Q70SX" | ||
ulid, err := UUIDtoULID(uuid) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
if ulid != expectedULID { | ||
t.Errorf("Expected ULID to be %s, but got %s", expectedULID, ulid) | ||
} | ||
}) | ||
t.Run("Invalid UUID", func(t *testing.T) { | ||
uuid := "860d3040-1e23-416f-a1fd-f4ccc773833" | ||
_, err := UUIDtoULID(uuid) | ||
if err == nil { | ||
t.Errorf("Expected error, but got nil") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.