Skip to content

Commit

Permalink
add basic len check to Decode
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 15, 2024
1 parent b92ee66 commit 3858a80
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ if err != nil {
fmt.Println(uuid) // Output: d1756360-5da0-40df-9926-a76abff5601d
```
4. Decode a Key to a UUID string without validation:
4. Decode a Key to a UUID string with basic length validation:
```go
key, _ := uuidkey.Parse("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
uuid := key.Decode()
uuid, err := key.UUID()
if err != nil {
log.Fatal("Error:", err)
}
fmt.Println(uuid) // Output: d1756360-5da0-40df-9926-a76abff5601d
```
Expand All @@ -91,7 +94,7 @@ Package uuidkey encodes UUIDs to a readable Key format via the Base32\-Crockford
- [type Key](<#Key>)
- [func Encode\(uuid string\) \(Key, error\)](<#Encode>)
- [func Parse\(key string\) \(Key, error\)](<#Parse>)
- [func \(k Key\) Decode\(\) string](<#Key.Decode>)
- [func \(k Key\) Decode\(\) \(string, error\)](<#Key.Decode>)
- [func \(k Key\) String\(\) string](<#Key.String>)
- [func \(k Key\) UUID\(\) \(string, error\)](<#Key.UUID>)
- [func \(k Key\) Valid\(\) bool](<#Key.Valid>)
Expand All @@ -104,7 +107,7 @@ Package uuidkey encodes UUIDs to a readable Key format via the Base32\-Crockford
```go
const (
// KeyLength is the total length of a valid UUID Key, including hyphens.
KeyLength = 31
KeyLength = 31 // 7 + 1 + 7 + 1 + 7 + 1 + 7 = 31 characters
// KeyPartLength is the length of each part in a UUID Key.
// A UUID Key consists of 4 parts separated by hyphens.
Expand Down Expand Up @@ -159,10 +162,10 @@ Parse converts a Key formatted string into a Key type.
### func \(Key\) [Decode](<https://github.com/agentstation/uuidkey/blob/master/codec.go#L62>)
```go
func (k Key) Decode() string
func (k Key) Decode() (string, error)
```
Decode will decode a given Key into a UUID string without validation.
Decode will decode a given Key into a UUID string with basic length validation.
<a name="Key.String"></a>
### func \(Key\) [String](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L28>)
Expand Down
12 changes: 8 additions & 4 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func decode(s string) string {

// Encode will encode a given UUID string into a Key with basic length validation.
func Encode(uuid string) (Key, error) {
if len(uuid) != UUIDLength {
if len(uuid) != UUIDLength { // basic length validation to ensure we can encode
return "", fmt.Errorf("invalid UUID length: expected %d characters, got %d", UUIDLength, len(uuid))
}

Expand All @@ -58,8 +58,12 @@ func Encode(uuid string) (Key, error) {
return Key(e1 + "-" + e2 + "-" + e3 + "-" + e4), nil
}

// Decode will decode a given Key into a UUID string without validation.
func (k Key) Decode() string {
// Decode will decode a given Key into a UUID string with basic length validation.
func (k Key) Decode() (string, error) {
if len(k) != KeyLength { // basic length validation to ensure we can decode
return "", fmt.Errorf("invalid Key length: expected %d characters, got %d", KeyLength, len(k))
}

// select the 4 parts of the key string
key := string(k) // convert the type from a Key to string
s1 := key[0:7] // [38QARV0]-1ET0G6Z-2CJD9VA-2ZZAR0X
Expand All @@ -80,5 +84,5 @@ func (k Key) Decode() string {
n3b := n3[4:8]

// build and return UUID string
return (n1 + "-" + n2a + "-" + n2b + "-" + n3a + "-" + n3b + n4)
return (n1 + "-" + n2a + "-" + n2b + "-" + n3a + "-" + n3b + n4), nil
}
6 changes: 3 additions & 3 deletions uuid_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ func BenchmarkEncode(b *testing.B) {
uuid := "d1756360-5da0-40df-9926-a76abff5601d"
b.ResetTimer()
for i := 0; i < b.N; i++ {
uuidkey.Encode(uuid)
_, _ = uuidkey.Encode(uuid)
}
}

func BenchmarkDecode(b *testing.B) {
key := uuidkey.Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
b.ResetTimer()
for i := 0; i < b.N; i++ {
key.Decode()
_, _ = key.Decode()
}
}

func BenchmarkValidateInvalid(b *testing.B) {
key := uuidkey.Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0") // Invalid key
b.ResetTimer()
for i := 0; i < b.N; i++ {
key.Valid()
_ = key.Valid()
}
}

Expand Down
4 changes: 2 additions & 2 deletions uuidkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Key validation constraint constants
const (
// KeyLength is the total length of a valid UUID Key, including hyphens.
KeyLength = 31
KeyLength = 31 // 7 + 1 + 7 + 1 + 7 + 1 + 7 = 31 characters

// KeyPartLength is the length of each part in a UUID Key.
// A UUID Key consists of 4 parts separated by hyphens.
Expand Down Expand Up @@ -87,5 +87,5 @@ func (k Key) UUID() (string, error) {
if !k.Valid() {
return "", errors.New("invalid UUID key")
}
return k.Decode(), nil
return k.Decode()
}
5 changes: 4 additions & 1 deletion uuidkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func TestEncodeDecode(t *testing.T) {
if err != nil {
t.Fatalf("Encode() returned an unexpected error: %v", err)
}
decodedUUID := key.Decode()
decodedUUID, err := key.Decode()
if err != nil {
t.Fatalf("Decode() returned an unexpected error: %v", err)
}

if decodedUUID != uuidStr {
t.Errorf("Encode/Decode roundtrip failed. Got %s, want %s", decodedUUID, uuidStr)
Expand Down

0 comments on commit 3858a80

Please sign in to comment.