diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 169ebde..a46d69b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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 @@ -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 diff --git a/Makefile b/Makefile index 9ba8044..b6b062a 100644 --- a/Makefile +++ b/Makefile @@ -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=. \ No newline at end of file +.PHONY: coverage +coverage: ## Run tests and generate coverage report + @echo "Running tests and generating coverage report..." + go test -race -coverprofile=coverage.txt -covermode=atomic ./... \ No newline at end of file diff --git a/README.md b/README.md index 4ee4175..9e79d7f 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,25 @@ Package uuidkey encodes UUIDs to a readable Key format via the Base32\-Crockford ## Constants +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 +) +``` + ```go @@ -113,7 +132,7 @@ const ( ``` -## type [Key]() +## type [Key]() Key is a UUID Key string. @@ -131,7 +150,7 @@ func Encode(uuid string) (Key, error) Encode will encode a given UUID string into a Key with basic length validation. -### func [FromString]() +### func [FromString]() ```go func FromString(key string) (Key, error) @@ -149,7 +168,7 @@ func (k Key) Decode() string Decode will decode a given Key into a UUID string without validation. -### func \(Key\) [String]() +### func \(Key\) [String]() ```go func (k Key) String() string @@ -158,7 +177,7 @@ func (k Key) String() string String will convert your Key into a string. -### func \(Key\) [UUIDString]() +### func \(Key\) [UUIDString]() ```go func (k Key) UUIDString() (string, error) @@ -167,7 +186,7 @@ func (k Key) UUIDString() (string, error) UUIDString will validate and convert a given Key into a UUID string. -### func \(Key\) [Valid]() +### func \(Key\) [Valid]() ```go func (k Key) Valid() bool @@ -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 diff --git a/uuidkey.go b/uuidkey.go index b25c8bf..eef7269 100644 --- a/uuidkey.go +++ b/uuidkey.go @@ -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. @@ -48,7 +57,7 @@ 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 @@ -56,8 +65,8 @@ func (k Key) Valid() bool { 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 @@ -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.