Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
matrix:
module:
- "config"
- "generate"
- "log"

runs-on: ubuntu-latest
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/generate-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "generate-ci"

on:
push:
branches:
- "feat**"
- "fix**"
- "hotfix**"
- "chore**"
paths:
- "generate/**.go"
- "generate/go.mod"
- "generate/go.sum"
pull_request:
types:
- opened
- synchronize
- reopened
branches:
- main
paths:
- "generate/**.go"
- "generate/go.mod"
- "generate/go.sum"

jobs:
ci:
uses: ./.github/workflows/common-ci.yml
secrets: inherit
with:
module: "generate"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Yokai

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

> Simple, modular, and observable Go framework.
Expand Down
66 changes: 66 additions & 0 deletions generate/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
run:
timeout: 5m
concurrency: 8

linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- dogsled
- dupl
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- forbidigo
- forcetypeassert
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- gofmt
- goheader
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- grouper
- importas
- ineffassign
- interfacebloat
- logrlint
- maintidx
- makezero
- misspell
- nestif
- nilerr
- nilnil
- nlreturn
- nolintlint
- nosprintfhostport
- prealloc
- predeclared
- promlinter
- reassign
- staticcheck
- tenv
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- whitespace
72 changes: 72 additions & 0 deletions generate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Generate Module

[![ci](https://github.com/ankorstore/yokai/actions/workflows/generate-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/generate-ci.yml)
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/generate)](https://goreportcard.com/report/github.com/ankorstore/yokai/generate)
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=5s0g5WyseS&flag=generate)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/generate)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/generate)](https://pkg.go.dev/github.com/ankorstore/yokai/generate)

> Generation module based on [Google UUID](https://github.com/google/uuid).

<!-- TOC -->

* [Installation](#installation)
* [Documentation](#documentation)
* [UUID](#uuid)

<!-- TOC -->

## Installation

```shell
go get github.com/ankorstore/yokai/generate
```

## Documentation

### UUID

This module provides an [UuidGenerator](uuid/generator.go) interface, allowing to generate UUIDs.

The `DefaultUuidGenerator` is based on [Google UUID](https://github.com/google/uuid).

```go
package main

import (
"fmt"

"github.com/ankorstore/yokai/generate/uuid"
uuidtest "github.com/ankorstore/yokai/generate/generatetest/uuid"
)

func main() {
// default UUID generator
generator := uuid.NewDefaultUuidGenerator()
fmt.Printf("uuid: %s", generator.Generate()) // uuid: dcb5d8b3-4517-4957-a42c-604d11758561

// test UUID generator (with deterministic value for testing)
testGenerator := uuidtest.NewTestUuidGenerator("test")
fmt.Printf("uuid: %s", testGenerator.Generate()) // uuid: test
}
```

The module also provides a [UuidGeneratorFactory](uuid/factory.go) interface, to create
the [UuidGenerator](uuid/generator.go) instances.

The `DefaultUuidGeneratorFactory` generates `DefaultUuidGenerator` instances.

```go
package main

import (
"fmt"

"github.com/ankorstore/yokai/generate/uuid"
)

func main() {
// default UUID generator factory
generator := uuid.NewDefaultUuidGeneratorFactory().Create()
fmt.Printf("uuid: %s", generator.Generate()) // uuid: dcb5d8b3-4517-4957-a42c-604d11758561
}
```
27 changes: 27 additions & 0 deletions generate/generatetest/uuid/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package uuid

// TestUuidGenerator is a [UuidGenerator] implementation allowing deterministic generations (for testing).
type TestUuidGenerator struct {
value string
}

// NewTestUuidGenerator returns a [TestUuidGenerator], implementing [UuidGenerator].
//
// It accepts a value that will be used for deterministic generation results.
func NewTestUuidGenerator(value string) *TestUuidGenerator {
return &TestUuidGenerator{
value: value,
}
}

// SetValue sets the value to use for deterministic generations.
func (g *TestUuidGenerator) SetValue(value string) *TestUuidGenerator {
g.value = value

return g
}

// Generate returns the configured deterministic value.
func (g *TestUuidGenerator) Generate() string {
return g.value
}
38 changes: 38 additions & 0 deletions generate/generatetest/uuid/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package uuid_test

import (
"testing"

uuidtest "github.com/ankorstore/yokai/generate/generatetest/uuid"
"github.com/ankorstore/yokai/generate/uuid"
"github.com/stretchr/testify/assert"
)

func TestNewTestUuidGenerator(t *testing.T) {
t.Parallel()

generator := uuidtest.NewTestUuidGenerator("random")

assert.IsType(t, &uuidtest.TestUuidGenerator{}, generator)
assert.Implements(t, (*uuid.UuidGenerator)(nil), generator)
}

func TestGenerate(t *testing.T) {
t.Parallel()

generator := uuidtest.NewTestUuidGenerator("test")

value1 := generator.Generate()
value2 := generator.Generate()

assert.Equal(t, "test", value1)
assert.Equal(t, "test", value2)

generator.SetValue("other test")

value1 = generator.Generate()
value2 = generator.Generate()

assert.Equal(t, "other test", value1)
assert.Equal(t, "other test", value2)
}
14 changes: 14 additions & 0 deletions generate/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/ankorstore/yokai/generate

go 1.20

require (
github.com/google/uuid v1.3.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions generate/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22 changes: 22 additions & 0 deletions generate/uuid/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uuid

// UuidGeneratorFactory is the interface for [UuidGenerator] factories.
type UuidGeneratorFactory interface {
Create() UuidGenerator
}

// DefaultUuidGeneratorFactory is the default [UuidGeneratorFactory] implementation.
type DefaultUuidGeneratorFactory struct{}

// NewDefaultUuidGeneratorFactory returns a [DefaultUuidGeneratorFactory], implementing [UuidGeneratorFactory].
func NewDefaultUuidGeneratorFactory() UuidGeneratorFactory {
return &DefaultUuidGeneratorFactory{}
}

// Create returns a new [UuidGenerator].
// For example:
//
// var generator, _ = uuid.NewDefaultConfigFactory().Create()
func (g *DefaultUuidGeneratorFactory) Create() UuidGenerator {
return NewDefaultUuidGenerator()
}
40 changes: 40 additions & 0 deletions generate/uuid/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uuid_test

import (
"testing"

"github.com/ankorstore/yokai/generate/uuid"
googleuuid "github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

func TestNewDefaultUuidGeneratorFactory(t *testing.T) {
t.Parallel()

factory := uuid.NewDefaultUuidGeneratorFactory()

assert.IsType(t, &uuid.DefaultUuidGeneratorFactory{}, factory)
assert.Implements(t, (*uuid.UuidGeneratorFactory)(nil), factory)
}

func TestCreate(t *testing.T) {
t.Parallel()

generator := uuid.NewDefaultUuidGeneratorFactory().Create()

value1 := generator.Generate()
value2 := generator.Generate()

assert.NotEqual(t, value1, value2)

parsedValue1, err := googleuuid.Parse(value1)
assert.NoError(t, err)

parsedValue2, err := googleuuid.Parse(value2)
assert.NoError(t, err)

assert.NotEqual(t, parsedValue1.String(), parsedValue2.String())

assert.Equal(t, value1, parsedValue1.String())
assert.Equal(t, value2, parsedValue2.String())
}
23 changes: 23 additions & 0 deletions generate/uuid/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uuid

import googleuuid "github.com/google/uuid"

// UuidGenerator is the interface for UUID generators.
type UuidGenerator interface {
Generate() string
}

// DefaultUuidGenerator is the default [UuidGenerator] implementation.
type DefaultUuidGenerator struct{}

// NewDefaultUuidGenerator returns a [DefaultUuidGenerator], implementing [UuidGenerator].
func NewDefaultUuidGenerator() *DefaultUuidGenerator {
return &DefaultUuidGenerator{}
}

// Generate returns a new UUID, using [Google UUID].
//
// [Google UUID]: https://github.com/google/uuid
func (g *DefaultUuidGenerator) Generate() string {
return googleuuid.New().String()
}
Loading