factory

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: Apache-2.0 Imports: 4 Imported by: 22

README

Factory Package

The factory package creates and manages plugins in the Lynx framework. It separates plugin registration from plugin creation behind small interfaces.

File Structure

interfaces.go

Core interfaces:

  • Registry — register, unregister, and query plugins
  • Creator — create plugin instances by name
  • Factory — composes Registry and Creator
typed_factory.go
  • TypedFactory — concurrency-safe registry of plugin creators, with a config-prefix index
  • RegisterTypedPlugin[T] / GetTypedPlugin[T] — generic helpers for type-safe access
  • GlobalTypedFactory() — lazily initialized global factory

Design

  • Interface segregation: registration and creation are separate contracts; Factory joins them.
  • Type safety: generics give callers concrete plugin types without manual assertions.
  • Concurrency safety: TypedFactory guards its maps with an sync.RWMutex. First registration of a name wins; duplicates are ignored so plugin-init ordering races cannot clobber an existing creator.

Usage

// Get the global type-safe factory
typedFactory := factory.GlobalTypedFactory()

// Register a type-safe plugin
factory.RegisterTypedPlugin(typedFactory, "redis", "cache", func() *redis.Plugin {
    return redis.New()
})

// Get a type-safe plugin instance
redisPlugin, err := factory.GetTypedPlugin[*redis.Plugin](typedFactory, "redis")

TypedFactory also implements the non-generic Factory interface (RegisterPlugin, CreatePlugin, GetPluginRegistry, HasPlugin, UnregisterPlugin) for callers that work with plugins.Plugin directly.

Interface Hierarchy

Factory (full capabilities)
├── Registry (registration)
│   ├── RegisterPlugin()
│   ├── UnregisterPlugin()
│   ├── GetPluginRegistry()
│   └── HasPlugin()
└── Creator (creation)
    └── CreatePlugin()

Documentation

Overview

Package factory provides functionality for creating and managing plugins in the Lynx framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTypedPlugin

func CreateTypedPlugin[T plugins.Plugin](factory *TypedFactory, name string) (T, error)

CreateTypedPlugin creates a type-safe plugin instance

func GetTypedPlugin

func GetTypedPlugin[T plugins.Plugin](factory *TypedFactory, name string) (T, error)

GetTypedPlugin gets a type-safe plugin instance

func RegisterTypedPlugin

func RegisterTypedPlugin[T plugins.Plugin](
	factory *TypedFactory,
	name string,
	configPrefix string,
	creator func() T,
)

RegisterTypedPlugin registers a type-safe plugin

Types

type Creator

type Creator interface {
	// CreatePlugin instantiates a new plugin instance by its name.
	// Returns an error if the plugin cannot be created.
	CreatePlugin(name string) (plugins.Plugin, error)
}

Creator defines the interface for creating plugin instances.

type Factory

type Factory interface {
	Creator
	Registry
}

Factory defines the complete interface for plugin management, combining both creation and registry capabilities.

type Registry

type Registry interface {
	// RegisterPlugin adds a new plugin to the registry with its configuration prefix
	// and creation function.
	RegisterPlugin(name string, configPrefix string, creator func() plugins.Plugin)

	// UnregisterPlugin removes a plugin from the registry.
	UnregisterPlugin(name string)

	// GetPluginRegistry returns the mapping of configuration prefixes to plugin names.
	GetPluginRegistry() map[string][]string

	// HasPlugin checks if a plugin is registered with the given name.
	HasPlugin(name string) bool
}

Registry defines the interface for managing plugin registrations.

type TypedFactory

type TypedFactory struct {
	// contains filtered or unexported fields
}

TypedFactory is a concurrency-safe registry of plugin creators keyed by name, with a secondary index from config prefix to the names registered under it.

func GlobalTypedFactory

func GlobalTypedFactory() *TypedFactory

GlobalTypedFactory returns the global type-safe plugin factory.

func NewTypedFactory

func NewTypedFactory() *TypedFactory

NewTypedFactory creates a type-safe plugin factory

func (*TypedFactory) CreatePlugin

func (f *TypedFactory) CreatePlugin(name string) (plugins.Plugin, error)

CreatePlugin creates plugin instance (compatible with old interface)

func (*TypedFactory) GetConfigMapping

func (f *TypedFactory) GetConfigMapping() map[string][]string

GetConfigMapping gets configuration mapping

func (*TypedFactory) GetPluginRegistry

func (f *TypedFactory) GetPluginRegistry() map[string][]string

GetPluginRegistry returns the plugin registry (backward-compatible API).

func (*TypedFactory) HasPlugin

func (f *TypedFactory) HasPlugin(name string) bool

HasPlugin checks if plugin exists

func (*TypedFactory) RegisterPlugin

func (f *TypedFactory) RegisterPlugin(name string, configPrefix string, creator func() plugins.Plugin)

RegisterPlugin registers a plugin (backward-compatible signature for TypedFactory). Recommended usage in plugins: factory.GlobalTypedFactory().RegisterPlugin(...)

func (*TypedFactory) UnregisterPlugin

func (f *TypedFactory) UnregisterPlugin(name string)

UnregisterPlugin unregisters a plugin.

Jump to

Keyboard shortcuts

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