Skip to content

Commit cfd113c

Browse files
authored
feat(generate): Provided module (#5)
1 parent 82bdf3d commit cfd113c

File tree

14 files changed

+392
-1
lines changed

14 files changed

+392
-1
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
matrix:
2222
module:
2323
- "config"
24+
- "generate"
2425
- "log"
2526

2627
runs-on: ubuntu-latest

.github/workflows/generate-ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "generate-ci"
2+
3+
on:
4+
push:
5+
branches:
6+
- "feat**"
7+
- "fix**"
8+
- "hotfix**"
9+
- "chore**"
10+
paths:
11+
- "generate/**.go"
12+
- "generate/go.mod"
13+
- "generate/go.sum"
14+
pull_request:
15+
types:
16+
- opened
17+
- synchronize
18+
- reopened
19+
branches:
20+
- main
21+
paths:
22+
- "generate/**.go"
23+
- "generate/go.mod"
24+
- "generate/go.sum"
25+
26+
jobs:
27+
ci:
28+
uses: ./.github/workflows/common-ci.yml
29+
secrets: inherit
30+
with:
31+
module: "generate"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Yokai
22

3-
[![Go version](https://img.shields.io/badge/go-%3E%3D1.20-blue)](https://go.dev/)
3+
[![Go version](https://img.shields.io/badge/Go-1.20-blue)](https://go.dev/)
44
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
55

66
> Simple, modular, and observable Go framework.

generate/.golangci.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
run:
2+
timeout: 5m
3+
concurrency: 8
4+
5+
linters:
6+
enable:
7+
- asasalint
8+
- asciicheck
9+
- bidichk
10+
- bodyclose
11+
- containedctx
12+
- contextcheck
13+
- cyclop
14+
- decorder
15+
- dogsled
16+
- dupl
17+
- durationcheck
18+
- errcheck
19+
- errchkjson
20+
- errname
21+
- errorlint
22+
- exhaustive
23+
- forbidigo
24+
- forcetypeassert
25+
- gocognit
26+
- goconst
27+
- gocritic
28+
- gocyclo
29+
- godot
30+
- godox
31+
- gofmt
32+
- goheader
33+
- gomoddirectives
34+
- gomodguard
35+
- goprintffuncname
36+
- gosec
37+
- gosimple
38+
- govet
39+
- grouper
40+
- importas
41+
- ineffassign
42+
- interfacebloat
43+
- logrlint
44+
- maintidx
45+
- makezero
46+
- misspell
47+
- nestif
48+
- nilerr
49+
- nilnil
50+
- nlreturn
51+
- nolintlint
52+
- nosprintfhostport
53+
- prealloc
54+
- predeclared
55+
- promlinter
56+
- reassign
57+
- staticcheck
58+
- tenv
59+
- thelper
60+
- tparallel
61+
- typecheck
62+
- unconvert
63+
- unparam
64+
- unused
65+
- usestdlibvars
66+
- whitespace

generate/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Generate Module
2+
3+
[![ci](https://github.com/ankorstore/yokai/actions/workflows/generate-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/generate-ci.yml)
4+
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/generate)](https://goreportcard.com/report/github.com/ankorstore/yokai/generate)
5+
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=5s0g5WyseS&flag=generate)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/generate)
6+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/generate)](https://pkg.go.dev/github.com/ankorstore/yokai/generate)
7+
8+
> Generation module based on [Google UUID](https://github.com/google/uuid).
9+
10+
<!-- TOC -->
11+
12+
* [Installation](#installation)
13+
* [Documentation](#documentation)
14+
* [UUID](#uuid)
15+
16+
<!-- TOC -->
17+
18+
## Installation
19+
20+
```shell
21+
go get github.com/ankorstore/yokai/generate
22+
```
23+
24+
## Documentation
25+
26+
### UUID
27+
28+
This module provides an [UuidGenerator](uuid/generator.go) interface, allowing to generate UUIDs.
29+
30+
The `DefaultUuidGenerator` is based on [Google UUID](https://github.com/google/uuid).
31+
32+
```go
33+
package main
34+
35+
import (
36+
"fmt"
37+
38+
"github.com/ankorstore/yokai/generate/uuid"
39+
uuidtest "github.com/ankorstore/yokai/generate/generatetest/uuid"
40+
)
41+
42+
func main() {
43+
// default UUID generator
44+
generator := uuid.NewDefaultUuidGenerator()
45+
fmt.Printf("uuid: %s", generator.Generate()) // uuid: dcb5d8b3-4517-4957-a42c-604d11758561
46+
47+
// test UUID generator (with deterministic value for testing)
48+
testGenerator := uuidtest.NewTestUuidGenerator("test")
49+
fmt.Printf("uuid: %s", testGenerator.Generate()) // uuid: test
50+
}
51+
```
52+
53+
The module also provides a [UuidGeneratorFactory](uuid/factory.go) interface, to create
54+
the [UuidGenerator](uuid/generator.go) instances.
55+
56+
The `DefaultUuidGeneratorFactory` generates `DefaultUuidGenerator` instances.
57+
58+
```go
59+
package main
60+
61+
import (
62+
"fmt"
63+
64+
"github.com/ankorstore/yokai/generate/uuid"
65+
)
66+
67+
func main() {
68+
// default UUID generator factory
69+
generator := uuid.NewDefaultUuidGeneratorFactory().Create()
70+
fmt.Printf("uuid: %s", generator.Generate()) // uuid: dcb5d8b3-4517-4957-a42c-604d11758561
71+
}
72+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package uuid
2+
3+
// TestUuidGenerator is a [UuidGenerator] implementation allowing deterministic generations (for testing).
4+
type TestUuidGenerator struct {
5+
value string
6+
}
7+
8+
// NewTestUuidGenerator returns a [TestUuidGenerator], implementing [UuidGenerator].
9+
//
10+
// It accepts a value that will be used for deterministic generation results.
11+
func NewTestUuidGenerator(value string) *TestUuidGenerator {
12+
return &TestUuidGenerator{
13+
value: value,
14+
}
15+
}
16+
17+
// SetValue sets the value to use for deterministic generations.
18+
func (g *TestUuidGenerator) SetValue(value string) *TestUuidGenerator {
19+
g.value = value
20+
21+
return g
22+
}
23+
24+
// Generate returns the configured deterministic value.
25+
func (g *TestUuidGenerator) Generate() string {
26+
return g.value
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package uuid_test
2+
3+
import (
4+
"testing"
5+
6+
uuidtest "github.com/ankorstore/yokai/generate/generatetest/uuid"
7+
"github.com/ankorstore/yokai/generate/uuid"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestNewTestUuidGenerator(t *testing.T) {
12+
t.Parallel()
13+
14+
generator := uuidtest.NewTestUuidGenerator("random")
15+
16+
assert.IsType(t, &uuidtest.TestUuidGenerator{}, generator)
17+
assert.Implements(t, (*uuid.UuidGenerator)(nil), generator)
18+
}
19+
20+
func TestGenerate(t *testing.T) {
21+
t.Parallel()
22+
23+
generator := uuidtest.NewTestUuidGenerator("test")
24+
25+
value1 := generator.Generate()
26+
value2 := generator.Generate()
27+
28+
assert.Equal(t, "test", value1)
29+
assert.Equal(t, "test", value2)
30+
31+
generator.SetValue("other test")
32+
33+
value1 = generator.Generate()
34+
value2 = generator.Generate()
35+
36+
assert.Equal(t, "other test", value1)
37+
assert.Equal(t, "other test", value2)
38+
}

generate/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/ankorstore/yokai/generate
2+
3+
go 1.20
4+
5+
require (
6+
github.com/google/uuid v1.3.0
7+
github.com/stretchr/testify v1.8.4
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

generate/go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
4+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
8+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
12+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

generate/uuid/factory.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package uuid
2+
3+
// UuidGeneratorFactory is the interface for [UuidGenerator] factories.
4+
type UuidGeneratorFactory interface {
5+
Create() UuidGenerator
6+
}
7+
8+
// DefaultUuidGeneratorFactory is the default [UuidGeneratorFactory] implementation.
9+
type DefaultUuidGeneratorFactory struct{}
10+
11+
// NewDefaultUuidGeneratorFactory returns a [DefaultUuidGeneratorFactory], implementing [UuidGeneratorFactory].
12+
func NewDefaultUuidGeneratorFactory() UuidGeneratorFactory {
13+
return &DefaultUuidGeneratorFactory{}
14+
}
15+
16+
// Create returns a new [UuidGenerator].
17+
// For example:
18+
//
19+
// var generator, _ = uuid.NewDefaultConfigFactory().Create()
20+
func (g *DefaultUuidGeneratorFactory) Create() UuidGenerator {
21+
return NewDefaultUuidGenerator()
22+
}

generate/uuid/factory_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package uuid_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ankorstore/yokai/generate/uuid"
7+
googleuuid "github.com/google/uuid"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestNewDefaultUuidGeneratorFactory(t *testing.T) {
12+
t.Parallel()
13+
14+
factory := uuid.NewDefaultUuidGeneratorFactory()
15+
16+
assert.IsType(t, &uuid.DefaultUuidGeneratorFactory{}, factory)
17+
assert.Implements(t, (*uuid.UuidGeneratorFactory)(nil), factory)
18+
}
19+
20+
func TestCreate(t *testing.T) {
21+
t.Parallel()
22+
23+
generator := uuid.NewDefaultUuidGeneratorFactory().Create()
24+
25+
value1 := generator.Generate()
26+
value2 := generator.Generate()
27+
28+
assert.NotEqual(t, value1, value2)
29+
30+
parsedValue1, err := googleuuid.Parse(value1)
31+
assert.NoError(t, err)
32+
33+
parsedValue2, err := googleuuid.Parse(value2)
34+
assert.NoError(t, err)
35+
36+
assert.NotEqual(t, parsedValue1.String(), parsedValue2.String())
37+
38+
assert.Equal(t, value1, parsedValue1.String())
39+
assert.Equal(t, value2, parsedValue2.String())
40+
}

generate/uuid/generator.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package uuid
2+
3+
import googleuuid "github.com/google/uuid"
4+
5+
// UuidGenerator is the interface for UUID generators.
6+
type UuidGenerator interface {
7+
Generate() string
8+
}
9+
10+
// DefaultUuidGenerator is the default [UuidGenerator] implementation.
11+
type DefaultUuidGenerator struct{}
12+
13+
// NewDefaultUuidGenerator returns a [DefaultUuidGenerator], implementing [UuidGenerator].
14+
func NewDefaultUuidGenerator() *DefaultUuidGenerator {
15+
return &DefaultUuidGenerator{}
16+
}
17+
18+
// Generate returns a new UUID, using [Google UUID].
19+
//
20+
// [Google UUID]: https://github.com/google/uuid
21+
func (g *DefaultUuidGenerator) Generate() string {
22+
return googleuuid.New().String()
23+
}

0 commit comments

Comments
 (0)