Skip to content

Commit

Permalink
remove magic key numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 15, 2024
1 parent f7ff906 commit 889baea
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
go-version: ${{ matrix.go-version }}

- name: Run go mod download
run: go mod download
run: make install

- name: Lint Go Code
uses: golangci/golangci-lint-action@v3
Expand All @@ -38,7 +38,7 @@ jobs:
run: make vet

- name: Run Tests and Generate Coverage
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
run: make coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
Expand Down
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ lint: ## Run golangci-lint
@echo "Running golangci-lint..."
golangci-lint run ./...

##@ Testing & Benchmarking
##@ Benchmarking, Testing, & Coverage

.PHONY: bench
bench: ## Run Go benchmarks
@echo "Running go benchmarks..."
go test ./... -tags=bench -bench=.

.PHONY: test
test: ## Run Go tests
@echo "Running go tests..."
go test ./... -tags=test

.PHONY: bench
bench: ## Run Go benchmarks
@echo "Running go benchmarks..."
go test ./... -tags=bench -bench=.
.PHONY: coverage
coverage: ## Run tests and generate coverage report
@echo "Running tests and generating coverage report..."
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
34 changes: 27 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ Package uuidkey encodes UUIDs to a readable Key format via the Base32\-Crockford
## Constants
<a name="KeyLength"></a>Key validation constraint constants
```go
const (
// KeyLength is the total length of a valid UUID Key, including hyphens.
KeyLength = 31
// KeyPartLength is the length of each part in a UUID Key.
// A UUID Key consists of 4 parts separated by hyphens.
KeyPartLength = 7
// KeyHyphenCount is the number of hyphens in a valid UUID Key.
KeyHyphenCount = 3
// KeyPartsCount is the number of parts in a valid UUID Key.
KeyPartsCount = KeyHyphenCount + 1
)
```
<a name="UUIDLength"></a>
```go
Expand All @@ -113,7 +132,7 @@ const (
```
<a name="Key"></a>
## type [Key](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L16>)
## type [Key](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L25>)
Key is a UUID Key string.
Expand All @@ -131,7 +150,7 @@ func Encode(uuid string) (Key, error)
Encode will encode a given UUID string into a Key with basic length validation.
<a name="FromString"></a>
### func [FromString](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L24>)
### func [FromString](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L33>)
```go
func FromString(key string) (Key, error)
Expand All @@ -149,7 +168,7 @@ func (k Key) Decode() string
Decode will decode a given Key into a UUID string without validation.
<a name="Key.String"></a>
### func \(Key\) [String](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L19>)
### func \(Key\) [String](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L28>)
```go
func (k Key) String() string
Expand All @@ -158,7 +177,7 @@ func (k Key) String() string
String will convert your Key into a string.
<a name="Key.UUIDString"></a>
### func \(Key\) [UUIDString](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L77>)
### func \(Key\) [UUIDString](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L86>)
```go
func (k Key) UUIDString() (string, error)
Expand All @@ -167,7 +186,7 @@ func (k Key) UUIDString() (string, error)
UUIDString will validate and convert a given Key into a UUID string.
<a name="Key.Valid"></a>
### func \(Key\) [Valid](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L50>)
### func \(Key\) [Valid](<https://github.com/agentstation/uuidkey/blob/master/uuidkey.go#L59>)
```go
func (k Key) Valid() bool
Expand Down Expand Up @@ -224,9 +243,10 @@ Development
vet Run go vet
lint Run golangci-lint
Testing & Benchmarking
test Run Go tests
Benchmarking, Testing, & Coverage
bench Run Go benchmarks
test Run Go tests
coverage Run tests and generate coverage report
```
## Benchmarks
Expand Down
25 changes: 17 additions & 8 deletions uuidkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import (
"errors"
)

// key validation constraint constants
// Key validation constraint constants
const (
key_len = 31
key_part_len = 7
key_hyphens = 3
// KeyLength is the total length of a valid UUID Key, including hyphens.
KeyLength = 31

// KeyPartLength is the length of each part in a UUID Key.
// A UUID Key consists of 4 parts separated by hyphens.
KeyPartLength = 7

// KeyHyphenCount is the number of hyphens in a valid UUID Key.
KeyHyphenCount = 3

// KeyPartsCount is the number of parts in a valid UUID Key.
KeyPartsCount = KeyHyphenCount + 1
)

// Key is a UUID Key string.
Expand Down Expand Up @@ -48,16 +57,16 @@ func FromString(key string) (Key, error) {
// - 38QARV0-1ET0G6Z-2CJD9VA2ZZAR0X (missing hyphen)
// - 38QARV0-1ET0G6-2CJD9VA-2ZZAR0X (part too short)
func (k Key) Valid() bool {
if len(k) != key_len { // check if the key is 31 characters long
if len(k) != KeyLength { // check if the key is 31 characters long
return false
}
hyphenCount := 0
partLen := 0
for _, char := range k {
switch {
case char == '-':
hyphenCount++ // collect the number of hyphens
if partLen != key_part_len { // check parts are 7 characters long
hyphenCount++ // collect the number of hyphens
if partLen != KeyPartLength { // check parts are 7 characters long
return false
}
partLen = 0 // reset the part length
Expand All @@ -70,7 +79,7 @@ func (k Key) Valid() bool {
}
}
// check if the key contains 3 hyphens and the last part is 7 characters long
return hyphenCount == key_hyphens && partLen == key_part_len
return hyphenCount == KeyHyphenCount && partLen == KeyPartLength
}

// UUIDString will validate and convert a given Key into a UUID string.
Expand Down

0 comments on commit 889baea

Please sign in to comment.