fxgenerate

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 3 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 V4
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()
}
UUID V7
Usage

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

package main

import (
	"fmt"

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

func main() {
	fx.New(
		fxgenerate.FxGenerateModule,                        // load the module
		fx.Invoke(func(generator uuidv7.UuidV7Generator) {
			uuid, _ := generator.Generate()                 // invoke the uuid v7 generator
			fmt.Printf("uuid: %s", uuid.String())           // uuid: 018fdd68-1b41-7eb0-afad-57f45297c7c1
		}),
	).Run()
}
Testing

This module provides the possibility to make your UuidV7Generator 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-v7-value"
  • then decorate into the Fx container the UuidV7GeneratorFactory with the provided TestUuidGeneratorV7Factory
package main

import (
	"fmt"

	"github.com/ankorstore/yokai/fxgenerate"
	fxtestuuidv7 "github.com/ankorstore/yokai/fxgenerate/fxgeneratetest/uuidv7"
	"github.com/ankorstore/yokai/generate/uuidv7"
	"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 "018fdd68-1b41-7eb0-afad-57f45297c7c1"
				},
				fx.ResultTags(`name:"generate-test-uuid-v7-value"`),
			),
		),
		fx.Decorate(fxtestuuidv7.NewFxTestUuidV7GeneratorFactory), // override the module with the TestUuidGeneratorFactory
		fx.Invoke(func(generator uuidv7.UuidV7Generator) {            // invoke the generator
			uuid, _ := generator.Generate()
			fmt.Printf("uuid: %s", uuid.String())                  // uuid: 018fdd68-1b41-7eb0-afad-57f45297c7c1
		}),
	).Run()
}
Override

If needed, you can provide your own factories 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

NewFxUuidGenerator returns a uuid.UuidGenerator.

func NewFxUuidV7Generator added in v1.2.0

func NewFxUuidV7Generator(p FxUuidV7GeneratorParam) uuidv7.UuidV7Generator

NewFxUuidV7Generator returns a uuidv7.UuidV7Generator.

Types

type FxUuidGeneratorParam

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

FxUuidGeneratorParam allows injection of the required dependencies in NewFxUuidGenerator.

type FxUuidV7GeneratorParam added in v1.2.0

type FxUuidV7GeneratorParam struct {
	fx.In
	Factory uuidv7.UuidV7GeneratorFactory
}

FxUuidV7GeneratorParam allows injection of the required dependencies in NewFxUuidV7Generator.

Directories

Path Synopsis
fxgeneratetest

Jump to

Keyboard shortcuts

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