staticplug

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: BSD-3-Clause Imports: 6 Imported by: 2

README

Static plugins for Go binaries

Latest release CI workflow Go reference

The staticplug Go module implements compile-time plugins. Each plugin is a type that gets compiled into the executable. Once the executable is built its plugins and their functionality is fixed.

Modules wanting to make use of plugins need to create a registry:

var Registry = staticplug.NewRegistry()

Plugins register with the registry as part of the process initialization:

func init() {
  pkg.Registry.MustRegister(&myPlugin{})
}

The extendable module discovers plugins using implemented interfaces:

func Do() {
  plugins, err := Registry.PluginsImplementing((*Calculator)(nil))
  …
  for _, info := range plugins {
    p, err := info.New()
    …
    log.Printf("sum = %d", p.(Calculator).Add(1, 2))
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustTypeOfInterface

func MustTypeOfInterface(iface any) reflect.Type

MustTypeOfInterface wraps TypeOfInterface, converting all errors to panics.

func TypeOfInterface

func TypeOfInterface(iface any) (reflect.Type, error)

TypeOfInterface returns the reflection type representing the dynamic type of iface. Construct a nil value via (*Iface)(nil).

Values of reflect.Type are used directly.

Logically this function is equivalent to the following:

reflect.TypeOf((*Iface)(nil)).Elem()

Types

type Plugin

type Plugin interface {
	PluginInfo() PluginInfo
}

Plugin is the interface shared by all plugin implementations. Most plugins will implement additional interfaces for specific functionality.

type PluginInfo

type PluginInfo struct {
	// Plugin name. Must not be empty.
	Name string

	// Sorting prioritiy. Within the same priority plugins are sorted by name.
	Priority int

	// New returns a new instance of the plugin.
	New func() (Plugin, error)
}

PluginInfo represents a registered plugin.

type Registry

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

func NewRegistry

func NewRegistry() *Registry

NewRegistry instantiates a new plugin registry.

func (*Registry) MustRegister

func (r *Registry) MustRegister(p Plugin)

MustRegister registers a plugin by receiving a plain and empty value of the plugin, i.e. without full initialization. In most cases the plugin package should invoke this function in its "init" function.

func init() {
	registry.MustRegister(&myPlugin{})
}

func (*Registry) PluginByName

func (r *Registry) PluginByName(name string) (PluginInfo, bool)

PluginByName returns a plugin by its name.

func (*Registry) PluginNames

func (r *Registry) PluginNames() []string

PluginNames returns the names of all registered plugins.

func (*Registry) Plugins

func (r *Registry) Plugins() []PluginInfo

Plugins returns all registered plugins.

func (*Registry) PluginsImplementing

func (r *Registry) PluginsImplementing(iface any) ([]PluginInfo, error)

PluginsImplementing returns all plugins implementing a particular interface type. TypeOfInterface is used to determine the interface reflection type of the interface type.

func (*Registry) Register

func (r *Registry) Register(p Plugin) error

Register adds a plugin to the registry. A new plugin instance is created in the course of the registration to validate the produced type.

Jump to

Keyboard shortcuts

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