fxgenerate

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 2 Imported by: 1

README

Fx Generate Module

ci go report codecov Deps PkgGoDev

Fx module for generate.

Installation

go get github.com/ankorstore/yokai/fxgenerate

Documentation

Loading

To load the module in your Fx application:

package main

import (
	"github.com/ankorstore/yokai/fxgenerate"
	"go.uber.org/fx"
)

func main() {
	fx.New(fxgenerate.FxGenerateModule).Run()
}
Generators
UUID
Usage

This module provides a UuidGenerator, made available into the Fx container.

package main

import (
	"fmt"

	"github.com/ankorstore/yokai/generate/uuid"
	"github.com/ankorstore/yokai/fxgenerate"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		fxgenerate.FxGenerateModule, // load the module
		fx.Invoke(func(generator *uuid.UuidGenerator) { // invoke the uuid generator
			fmt.Printf("uuid: %s", generator.Generate()) // uuid: dcb5d8b3-4517-4957-a42c-604d11758561
		}),
	).Run()
}
Testing

This module provides the possibility to make your UuidGenerator generate deterministic values, for testing purposes.

You need to:

  • first provide into the Fx container the deterministic value to be used for generation, annotated with name:"generate-test-uuid-value"
  • then decorate into the Fx container the UuidGeneratorFactory with the provided TestUuidGeneratorFactory
package main

import (
	"fmt"

	"github.com/ankorstore/yokai/fxgenerate"
	fxtestuuid "github.com/ankorstore/yokai/fxgenerate/fxgeneratetest/uuid"
	"github.com/ankorstore/yokai/generate/uuid"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		fxgenerate.FxGenerateModule, // load the module
		fx.Provide(                  // provide and annotate the deterministic value
			fx.Annotate(
				func() string {
					return "some deterministic value"
				},
				fx.ResultTags(`name:"generate-test-uuid-value"`),
			),
		),
		fx.Decorate(fxtestuuid.NewFxTestUuidGeneratorFactory), // override the module with the TestUuidGeneratorFactory
		fx.Invoke(func(generator *uuid.UuidGenerator) {        // invoke the generator
			fmt.Printf("uuid: %s", generator.Generate())       // uuid: some deterministic value
		}),
	).Run()
}
Override

By default, the uuid.UuidGenerator is created by the DefaultUuidGeneratorFactory.

If needed, you can provide your own factory and override the module:

package main

import (
	"fmt"

	"github.com/ankorstore/yokai/fxgenerate"
	testuuid "github.com/ankorstore/yokai/fxgenerate/testdata/uuid"
	"github.com/ankorstore/yokai/generate/uuid"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		fxgenerate.FxGenerateModule,                             // load the module
		fx.Decorate(testuuid.NewTestStaticUuidGeneratorFactory), // override the module with a custom factory
		fx.Invoke(func(generator *uuid.UuidGenerator) {          // invoke the custom generator
			fmt.Printf("uuid: %s", generator.Generate())         // uuid: static
		}),
	).Run()
}

Documentation

Index

Constants

View Source
const ModuleName = "generate"

ModuleName is the module name.

Variables

FxGenerateModule is the Fx generate module.

Functions

func NewFxUuidGenerator

func NewFxUuidGenerator(p FxUuidGeneratorParam) (uuid.UuidGenerator, error)

NewFxUuidGenerator returns a uuid.UuidGenerator.

Types

type FxUuidGeneratorParam

type FxUuidGeneratorParam struct {
	fx.In
	Factory uuid.UuidGeneratorFactory
}

FxUuidGeneratorParam allows injection of the required dependencies in NewFxUuidGenerator.

Directories

Path Synopsis
fxgeneratetest

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL