fxtrategy

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 1 Imported by: 4

README ¶

Fxtrategy

Eng | 中文

📚 Table of Contents


Introduction

fxtrategy is a strategy injection utility based on Uber FX, leveraging generics and named strategy patterns to enable flexible, modular registration and dynamic retrieval of strategies in Go applications.


Features

  • ✅ Support defining strategy behavior via interfaces
  • ✅ Strongly-typed context via Go generics
  • ✅ Name-based strategy registration and retrieval
  • ✅ Supports multiple contexts (e.g., Fool and Genius)
  • ✅ Fully integrated with Uber FX

Get Start

go get github.com/real-uangi/fxtrategy@latest

Example

Usage
	fx.New(
		fx.Options(fxtrategy.ProvideItem[Fool](newA, "test")),
		fx.Options(fxtrategy.ProvideItem[Fool](newB, "test")),
		fx.Options(fxtrategy.ProvideContext[Fool]("test")),
		fx.Invoke(func(ctx *fxtrategy.Context[Fool]) {
			a, _ := ctx.Get("a")
			a.FoolSpeak()
			b, _ := ctx.Get("b")
			b.FoolSpeak()
		}),
	)
TestSamples
type Fool interface {
	FoolSpeak()
	fxtrategy.Nameable
}

func newA() *FoolA {
	return &FoolA{}
}

func newB() *FoolB {
	return &FoolB{}
}

type FoolA struct {
}

func (a *FoolA) FoolSpeak() {
	fmt.Println("i'm a")
}

func (a *FoolA) ItemName() string {
	return "a"
}

type FoolB struct {
}

func (b *FoolB) FoolSpeak() {
	fmt.Println("i'm b")
}

func (b *FoolB) ItemName() string {
	return "b"
}

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func ProvideContext ¶ added in v1.3.0

func ProvideContext[T Nameable](group string) fx.Option

func ProvideItem ¶ added in v1.3.0

func ProvideItem[T Nameable](constructor any, group string) fx.Option

Types ¶

type Context ¶

type Context[T Nameable] struct {
	// contains filtered or unexported fields
}

Context to inject and use Items

func (*Context[T]) ForEach ¶

func (c *Context[T]) ForEach(fn TravelFunc[T]) error

ForEach execute fn on each item

func (*Context[T]) Get ¶

func (c *Context[T]) Get(name string) (T, bool)

func (*Context[T]) Names ¶

func (c *Context[T]) Names() []string

Names returns all item's name

type Nameable ¶ added in v1.3.0

type Nameable interface {
	ItemName() string
}

type TravelFunc ¶

type TravelFunc[T any] func(string, T) error

Jump to

Keyboard shortcuts

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