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 f50fbb3..30a3f70 100644 --- a/README.md +++ b/README.md @@ -132,11 +132,13 @@ Package publicid generates and validates NanoID strings designed to be publicly - [func Validate\(id string\) error](<#Validate>) - [func ValidateLong\(id string\) error](<#ValidateLong>) - [type Option](<#Option>) + - [func Alphabet\(a string\) Option](<#Alphabet>) - [func Attempts\(n int\) Option](<#Attempts>) + - [func Len\(n int\) Option](<#Len>) -## func [New]() +## func [New]() ```go func New(opts ...Option) (string, error) @@ -145,7 +147,7 @@ func New(opts ...Option) (string, error) New generates a unique nanoID with a length of 8 characters and the given options. -## func [NewLong]() +## func [NewLong]() ```go func NewLong(opts ...Option) (string, error) @@ -154,7 +156,7 @@ func NewLong(opts ...Option) (string, error) NewLong generates a unique nanoID with a length of 12 characters and the given options. -## func [Validate]() +## func [Validate]() ```go func Validate(id string) error @@ -163,7 +165,7 @@ func Validate(id string) error Validate checks if a given field name's public ID value is valid according to the constraints defined by package publicid. -## func [ValidateLong]() +## func [ValidateLong]() ```go func ValidateLong(id string) error @@ -180,8 +182,17 @@ Option is a function type for configuring ID generation. type Option func(*config) ``` + +### func [Alphabet]() + +```go +func Alphabet(a string) Option +``` + +Alphabet returns an Option to set the alphabet to be used for ID generation. + -### func [Attempts]() +### func [Attempts]() ```go func Attempts(n int) Option @@ -189,6 +200,15 @@ func Attempts(n int) Option Attempts returns an Option to set the number of attempts for ID generation. + +### func [Len]() + +```go +func Len(n int) Option +``` + +Len returns an Option to set the length of the ID to be generated. + Generated by [gomarkdoc]() diff --git a/publicid.go b/publicid.go index 84c040b..0523a88 100644 --- a/publicid.go +++ b/publicid.go @@ -23,6 +23,8 @@ type Option func(*config) // config holds the configuration for ID generation. type config struct { attempts int + length int + alphabet string } // Attempts returns an Option to set the number of attempts for ID generation. @@ -32,6 +34,20 @@ func Attempts(n int) Option { } } +// Len returns an Option to set the length of the ID to be generated. +func Len(n int) Option { + return func(c *config) { + c.length = n + } +} + +// Alphabet returns an Option to set the alphabet to be used for ID generation. +func Alphabet(a string) Option { + return func(c *config) { + c.alphabet = a + } +} + // New generates a unique nanoID with a length of 8 characters and the given options. func New(opts ...Option) (string, error) { return generateID(shortLen, opts...) } @@ -41,14 +57,14 @@ func NewLong(opts ...Option) (string, error) { return generateID(longLen, opts.. // generateID is a helper function to generate IDs with the given length and options. func generateID(length int, opts ...Option) (string, error) { // set default configuration values - cfg := &config{attempts: 1} + cfg := &config{attempts: 1, length: length, alphabet: alphabet} for _, opt := range opts { opt(cfg) } // try to generate the ID var lastErr error for i := 0; i < cfg.attempts; i++ { - id, err := generator(alphabet, length) + id, err := generator(cfg.alphabet, cfg.length) if err == nil { return id, nil }