Skip to content

Commit

Permalink
remove magic uuid length num
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 15, 2024
1 parent 44a1ee0 commit f7ff906
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Package uuidkey encodes UUIDs to a readable Key format via the Base32\-Crockford
## Index
- [Constants](<#constants>)
- [type Key](<#Key>)
- [func Encode\(uuid string\) \(Key, error\)](<#Encode>)
- [func FromString\(key string\) \(Key, error\)](<#FromString>)
Expand All @@ -99,6 +100,18 @@ Package uuidkey encodes UUIDs to a readable Key format via the Base32\-Crockford
- [func \(k Key\) Valid\(\) bool](<#Key.Valid>)
## Constants
<a name="UUIDLength"></a>
```go
const (
// UUIDLength is the standard length of a UUID string, including hyphens.
// Reference: RFC 4122 (https://tools.ietf.org/html/rfc4122)
UUIDLength = 36
)
```
<a name="Key"></a>
## type [Key](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L16>)
Expand All @@ -109,7 +122,7 @@ type Key string
```
<a name="Encode"></a>
### func [Encode](<https://github.com/agentstation/uuidkey/blob/master/codec.go#L27>)
### func [Encode](<https://github.com/agentstation/uuidkey/blob/master/codec.go#L33>)
```go
func Encode(uuid string) (Key, error)
Expand All @@ -127,7 +140,7 @@ func FromString(key string) (Key, error)
FromString will convert a Key formatted string type into a Key type.
<a name="Key.Decode"></a>
### func \(Key\) [Decode](<https://github.com/agentstation/uuidkey/blob/master/codec.go#L56>)
### func \(Key\) [Decode](<https://github.com/agentstation/uuidkey/blob/master/codec.go#L62>)
```go
func (k Key) Decode() string
Expand Down
10 changes: 8 additions & 2 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/richardlehane/crock32"
)

const (
// UUIDLength is the standard length of a UUID string, including hyphens.
// Reference: RFC 4122 (https://tools.ietf.org/html/rfc4122)
UUIDLength = 36
)

// encode will convert your given int64 into base32 crockford encoding format
func encode(n uint64) string {
encoded := crock32.Encode(n)
Expand All @@ -25,8 +31,8 @@ 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) != 36 { // check the UUID string has the correct length
return "", fmt.Errorf("invalid UUID length: expected 36 characters, got %d", len(uuid))
if len(uuid) != UUIDLength {
return "", fmt.Errorf("invalid UUID length: expected %d characters, got %d", UUIDLength, len(uuid))
}

// select the 5 parts of the UUID string
Expand Down

0 comments on commit f7ff906

Please sign in to comment.