Skip to content

Commit

Permalink
add generation options, update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 15, 2024
1 parent 7b63a90 commit 29ce75f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 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 ./...
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
<a name="New"></a>
## func [New](<https://github.com/agentstation/publicid/blob/master/publicid.go#L36>)
## func [New](<https://github.com/agentstation/publicid/blob/master/publicid.go#L52>)
```go
func New(opts ...Option) (string, error)
Expand All @@ -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.
<a name="NewLong"></a>
## func [NewLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L39>)
## func [NewLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L55>)
```go
func NewLong(opts ...Option) (string, error)
Expand All @@ -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.
<a name="Validate"></a>
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L63>)
## func [Validate](<https://github.com/agentstation/publicid/blob/master/publicid.go#L79>)
```go
func Validate(id string) error
Expand All @@ -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.

<a name="ValidateLong"></a>
## func [ValidateLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L67>)
## func [ValidateLong](<https://github.com/agentstation/publicid/blob/master/publicid.go#L83>)

```go
func ValidateLong(id string) error
Expand All @@ -180,15 +182,33 @@ Option is a function type for configuring ID generation.
type Option func(*config)
```
<a name="Alphabet"></a>
### func [Alphabet](<https://github.com/agentstation/publicid/blob/master/publicid.go#L45>)
```go
func Alphabet(a string) Option
```
Alphabet returns an Option to set the alphabet to be used for ID generation.
<a name="Attempts"></a>
### func [Attempts](<https://github.com/agentstation/publicid/blob/master/publicid.go#L29>)
### func [Attempts](<https://github.com/agentstation/publicid/blob/master/publicid.go#L31>)
```go
func Attempts(n int) Option
```
Attempts returns an Option to set the number of attempts for ID generation.
<a name="Len"></a>
### func [Len](<https://github.com/agentstation/publicid/blob/master/publicid.go#L38>)
```go
func Len(n int) Option
```
Len returns an Option to set the length of the ID to be generated.
Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
Expand Down
20 changes: 18 additions & 2 deletions publicid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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...) }

Expand All @@ -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
}
Expand Down

0 comments on commit 29ce75f

Please sign in to comment.