Skip to content

Commit c76696d

Browse files
authored
feat(fxgenerate): Provided module (#24)
1 parent 95f6600 commit c76696d

File tree

13 files changed

+490
-6
lines changed

13 files changed

+490
-6
lines changed

.github/workflows/coverage.yml

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

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ Yokai's documentation will be available soon.
1616

1717
## Fx Modules
1818

19-
Yokai is using [Fx](https://github.com/uber-go/fx) for its plugin system.
19+
Yokai's `Fx modules` are the plugins for your Yokai application.
2020

21-
`Yokai's Fx modules` are the plugins for your Yokai application, but they can be used in any [Fx](https://github.com/uber-go/fx) based Go application.
21+
Yokai is using [Fx](https://github.com/uber-go/fx) for its plugin system.
2222

23-
| Fx Module | Description |
24-
|----------------------|--------------------------------|
25-
| [fxconfig](fxconfig) | Fx module for [config](config) |
23+
| Fx Module | Description |
24+
|--------------------------|------------------------------------|
25+
| [fxconfig](fxconfig) | Fx module for [config](config) |
26+
| [fxgenerate](fxgenerate) | Fx module for [generate](generate) |
2627

28+
They can also be used in any [Fx](https://github.com/uber-go/fx) based Go application.
2729

2830
## Modules
2931

30-
`Yokai's modules` are the foundation of the framework, but they can be used in any Go application (no Yokai dependencies).
32+
Yokai's `modules` are the foundation of the framework.
3133

3234
| Module | Description |
3335
|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
@@ -40,6 +42,8 @@ Yokai is using [Fx](https://github.com/uber-go/fx) for its plugin system.
4042
| [orm](orm) | ORM module based on [Gorm](https://gorm.io/) |
4143
| [trace](trace) | Tracing module based on [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-go) |
4244

45+
They can also be used in any Go application (no Yokai or [Fx](https://github.com/uber-go/fx) dependencies).
46+
4347
## Contributing
4448

4549
This repository uses [release-please](https://github.com/googleapis/release-please) to automate Yokai's modules release process.

fxgenerate/.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

fxgenerate/README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Fx Generate Module
2+
3+
[![ci](https://github.com/ankorstore/yokai/actions/workflows/fxgenerate-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/fxgenerate-ci.yml)
4+
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/fxgenerate)](https://goreportcard.com/report/github.com/ankorstore/yokai/fxgenerate)
5+
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR&flag=fxgenerate)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxgenerate)
6+
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxgenerate)
7+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxgenerate)](https://pkg.go.dev/github.com/ankorstore/yokai/fxgenerate)
8+
9+
> [Fx](https://uber-go.github.io/fx/) module for [generate](https://github.com/ankorstore/yokai/tree/main/generate).
10+
11+
<!-- TOC -->
12+
* [Installation](#installation)
13+
* [Documentation](#documentation)
14+
* [Loading](#loading)
15+
* [Generators](#generators)
16+
* [UUID](#uuid)
17+
* [Usage](#usage)
18+
* [Testing](#testing)
19+
* [Override](#override)
20+
<!-- TOC -->
21+
22+
## Installation
23+
24+
```shell
25+
go get github.com/ankorstore/yokai/fxgenerate
26+
```
27+
28+
## Documentation
29+
30+
### Loading
31+
32+
To load the module in your Fx application:
33+
34+
```go
35+
package main
36+
37+
import (
38+
"github.com/ankorstore/yokai/fxgenerate"
39+
"go.uber.org/fx"
40+
)
41+
42+
func main() {
43+
fx.New(fxgenerate.FxGenerateModule).Run()
44+
}
45+
```
46+
47+
### Generators
48+
49+
#### UUID
50+
51+
##### Usage
52+
53+
This module provides a [UuidGenerator](https://github.com/ankorstore/yokai/blob/main/generate/uuid/generator.go), made available into the Fx container.
54+
55+
```go
56+
package main
57+
58+
import (
59+
"fmt"
60+
61+
"github.com/ankorstore/yokai/generate/uuid"
62+
"github.com/ankorstore/yokai/fxgenerate"
63+
"go.uber.org/fx"
64+
)
65+
66+
func main() {
67+
fx.New(
68+
fxgenerate.FxGenerateModule, // load the module
69+
fx.Invoke(func(generator *uuid.UuidGenerator) { // invoke the uuid generator
70+
fmt.Printf("uuid: %s", generator.Generate()) // uuid: dcb5d8b3-4517-4957-a42c-604d11758561
71+
}),
72+
).Run()
73+
}
74+
```
75+
76+
##### Testing
77+
78+
This module provides the possibility to make your [UuidGenerator](https://github.com/ankorstore/yokai/blob/main/generate/uuid/generator.go) generate deterministic values, for testing purposes.
79+
80+
You need to:
81+
82+
- first provide into the Fx container the deterministic value to be used for generation, annotated with `name:"generate-test-uuid-value"`
83+
- then decorate into the Fx container the `UuidGeneratorFactory` with the provided [TestUuidGeneratorFactory](fxgeneratetest/uuid/factory.go)
84+
85+
```go
86+
package main
87+
88+
import (
89+
"fmt"
90+
91+
"github.com/ankorstore/yokai/fxgenerate"
92+
fxtestuuid "github.com/ankorstore/yokai/fxgenerate/fxgeneratetest/uuid"
93+
"github.com/ankorstore/yokai/generate/uuid"
94+
"go.uber.org/fx"
95+
)
96+
97+
func main() {
98+
fx.New(
99+
fxgenerate.FxGenerateModule, // load the module
100+
fx.Provide( // provide and annotate the deterministic value
101+
fx.Annotate(
102+
func() string {
103+
return "some deterministic value"
104+
},
105+
fx.ResultTags(`name:"generate-test-uuid-value"`),
106+
),
107+
),
108+
fx.Decorate(fxtestuuid.NewFxTestUuidGeneratorFactory), // override the module with the TestUuidGeneratorFactory
109+
fx.Invoke(func(generator *uuid.UuidGenerator) { // invoke the generator
110+
fmt.Printf("uuid: %s", generator.Generate()) // uuid: some deterministic value
111+
}),
112+
).Run()
113+
}
114+
```
115+
116+
### Override
117+
118+
By default, the `uuid.UuidGenerator` is created by the [DefaultUuidGeneratorFactory](https://github.com/ankorstore/yokai/blob/main/generate/uuid/factory.go).
119+
120+
If needed, you can provide your own factory and override the module:
121+
122+
```go
123+
package main
124+
125+
import (
126+
"fmt"
127+
128+
"github.com/ankorstore/yokai/fxgenerate"
129+
testuuid "github.com/ankorstore/yokai/fxgenerate/testdata/uuid"
130+
"github.com/ankorstore/yokai/generate/uuid"
131+
"go.uber.org/fx"
132+
)
133+
134+
func main() {
135+
fx.New(
136+
fxgenerate.FxGenerateModule, // load the module
137+
fx.Decorate(testuuid.NewTestStaticUuidGeneratorFactory), // override the module with a custom factory
138+
fx.Invoke(func(generator *uuid.UuidGenerator) { // invoke the custom generator
139+
fmt.Printf("uuid: %s", generator.Generate()) // uuid: static
140+
}),
141+
).Run()
142+
}
143+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package uuid
2+
3+
import (
4+
uuidtest "github.com/ankorstore/yokai/generate/generatetest/uuid"
5+
"github.com/ankorstore/yokai/generate/uuid"
6+
"go.uber.org/fx"
7+
)
8+
9+
// FxTestUuidGeneratorFactoryParam is used to retrieve the provided generate-test-uuid-value from Fx.
10+
type FxTestUuidGeneratorFactoryParam struct {
11+
fx.In
12+
Value string `name:"generate-test-uuid-value"`
13+
}
14+
15+
// TestUuidGeneratorFactory is a [uuid.UuidGeneratorFactory] implementation.
16+
type TestUuidGeneratorFactory struct {
17+
value string
18+
}
19+
20+
// NewFxTestUuidGeneratorFactory returns a new [TestUuidGeneratorFactory], implementing [uuid.UuidGeneratorFactory].
21+
func NewFxTestUuidGeneratorFactory(p FxTestUuidGeneratorFactoryParam) uuid.UuidGeneratorFactory {
22+
return &TestUuidGeneratorFactory{
23+
value: p.Value,
24+
}
25+
}
26+
27+
// Create returns a new [uuid.UuidGenerator].
28+
func (f *TestUuidGeneratorFactory) Create() uuid.UuidGenerator {
29+
return uuidtest.NewTestUuidGenerator(f.value)
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package uuid_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ankorstore/yokai/fxgenerate"
7+
fxgeneratetestuuid "github.com/ankorstore/yokai/fxgenerate/fxgeneratetest/uuid"
8+
"github.com/ankorstore/yokai/generate/uuid"
9+
"github.com/stretchr/testify/assert"
10+
"go.uber.org/fx"
11+
"go.uber.org/fx/fxtest"
12+
)
13+
14+
func TestTestUuidGenerator(t *testing.T) {
15+
t.Parallel()
16+
17+
var generator uuid.UuidGenerator
18+
19+
fxtest.New(
20+
t,
21+
fx.NopLogger,
22+
fxgenerate.FxGenerateModule,
23+
fx.Provide(
24+
fx.Annotate(
25+
func() string {
26+
return "some test value"
27+
},
28+
fx.ResultTags(`name:"generate-test-uuid-value"`),
29+
),
30+
),
31+
fx.Decorate(fxgeneratetestuuid.NewFxTestUuidGeneratorFactory),
32+
fx.Populate(&generator),
33+
).RequireStart().RequireStop()
34+
35+
assert.Equal(t, "some test value", generator.Generate())
36+
}

fxgenerate/go.mod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module github.com/ankorstore/yokai/fxgenerate
2+
3+
go 1.20
4+
5+
require (
6+
github.com/ankorstore/yokai/generate v1.0.0
7+
github.com/google/uuid v1.5.0
8+
github.com/stretchr/testify v1.8.4
9+
go.uber.org/fx v1.20.1
10+
)
11+
12+
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/pkg/errors v0.9.1 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
16+
go.uber.org/atomic v1.9.0 // indirect
17+
go.uber.org/dig v1.17.0 // indirect
18+
go.uber.org/multierr v1.8.0 // indirect
19+
go.uber.org/zap v1.23.0 // indirect
20+
golang.org/x/sys v0.8.0 // indirect
21+
gopkg.in/yaml.v3 v3.0.1 // indirect
22+
)

0 commit comments

Comments
 (0)