diff --git a/README.md b/README.md index cb0f0ca..61c6d3b 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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>) @@ -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. @@ -159,10 +162,10 @@ Parse converts a Key formatted string into a Key type. ### func \(Key\) [Decode]() ```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. ### func \(Key\) [String]() diff --git a/codec.go b/codec.go index dca6048..f390b19 100644 --- a/codec.go +++ b/codec.go @@ -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)) } @@ -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 @@ -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 } diff --git a/uuid_benchmark_test.go b/uuid_benchmark_test.go index 15c10a2..5ce9109 100644 --- a/uuid_benchmark_test.go +++ b/uuid_benchmark_test.go @@ -37,7 +37,7 @@ 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) } } @@ -45,7 +45,7 @@ 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() } } @@ -53,7 +53,7 @@ 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() } } diff --git a/uuidkey.go b/uuidkey.go index 80e5072..4f97a8c 100644 --- a/uuidkey.go +++ b/uuidkey.go @@ -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. @@ -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() } diff --git a/uuidkey_test.go b/uuidkey_test.go index 53320c2..843ed84 100644 --- a/uuidkey_test.go +++ b/uuidkey_test.go @@ -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)