|
| 1 | +# Fx Generate Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai/actions/workflows/fxgenerate-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/fxgenerate) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxgenerate) |
| 6 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxgenerate) |
| 7 | +[](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 | +``` |
0 commit comments