modules

package module
v0.0.0-...-3fc3e0a Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2017 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-modules

Simple API for (static) module support in Go.

Stable version can be retrieved with:

go get gopkg.in/brunoga/go-modules.v1.

Documentation

Overview

Package modules implements support for generic modules. It allows programs to easily support the concept of having extendable code by providing an interface for automatic registration querying and usage of existing modules. Each module is completelly self contained and, usually, creating a module is just a matter of adding a new source code file for it without having to change anything else in the code that would use it.

Index

Constants

View Source
const (
	ApiVersion = 1
)

Variables

This section is empty.

Functions

func GetAllModulesCount

func GetAllModulesCount() int

GetAllModulesCount returns the total number of registered modules.

func GetModuleCountByGenericId

func GetModuleCountByGenericId(genericModuleId string) int

GetModuleCountByGenericId returns the number of registered modules with the given generic id.

func GetModuleCountByType

func GetModuleCountByType(moduleType string) int

GetModuleCountByType returns the number of registered modules of a specific type.

func LoadPluginModule

func LoadPluginModule(path string) error

func RegisterModule

func RegisterModule(module Module) error

RegisterModule registers a new module for usage.

func UnregisterModule

func UnregisterModule(module Module) error

UnregisterModule unregisters the given module.

Types

type FullModuleMap

type FullModuleMap map[string]ModuleMap

FullModuleMap is a container for ModuleMaps keyed by generic id.

func GetAllModules

func GetAllModules() FullModuleMap

GetAllModules returns a ModuleMap containing all registered modules.

func GetModulesByType

func GetModulesByType(moduleType string) FullModuleMap

GetModulesByType returns a FullModuleMap with all modules of the given type.

type GenericModule

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

GenericModule defines a generic module implementation. This is mainly to be used for struct embedding as it is not very useful by itself.

func NewGenericModule

func NewGenericModule(name, version, genericId, specificId, moduleType string) *GenericModule

NewGenericModule creates and returns a new GenericModule.

func (*GenericModule) Configure

func (gm *GenericModule) Configure(parameters *ParameterMap) error

func (*GenericModule) GenericId

func (gm *GenericModule) GenericId() string

func (*GenericModule) Name

func (gm *GenericModule) Name() string

func (*GenericModule) New

func (gm *GenericModule) New(specificId string) (Module, error)

func (*GenericModule) Parameters

func (gm *GenericModule) Parameters() *ParameterMap

func (*GenericModule) Ready

func (gm *GenericModule) Ready() bool

func (*GenericModule) Register

func (gm *GenericModule) Register() error

func (*GenericModule) SetReady

func (gm *GenericModule) SetReady(ready bool)

SetReady allows setting the ready status of the module.

func (*GenericModule) SpecificId

func (gm *GenericModule) SpecificId() string

func (*GenericModule) String

func (gm *GenericModule) String() string

Implements the Stringer interface.

func (*GenericModule) Type

func (gm *GenericModule) Type() string

func (*GenericModule) Unregister

func (gm *GenericModule) Unregister() error

func (*GenericModule) Version

func (gm *GenericModule) Version() string

type Module

type Module interface {
	// Name returns the human-readable name for the module.
	Name() string

	// Version returns the human-readable version for the module.
	Version() string

	// GenericId returns a short generic identifier for this module. Usually
	// it identifies the "class" of this Module (for example,
	// "directory-reader" and, together with SpecificId() below, must
	// uniquely identify an instance of this Module.
	GenericId() string

	// SpecificId returns a short identifier for this module. It serves to
	// identify a specific Module instance in a given "class" (see
	// GenericId()). For example, assuming a GenericId() of
	// "directory-reader", this could return something that represents a
	// specific directory. For example, it could return "home" to indicate
	// that it is a "directory-reader" that operates in the "home"
	// directory.
	SpecificId() string

	// Type returns the specific module type. This is used do group modules
	// in specific domains so one can query about all modules on them. A
	// type could, for example, be a refrence to the program that uses
	// those modules (in other words, it could be the program name).
	Type() string

	// Register does any initialization required during module
	// registration. It is implicitly called by RegisterModule.
	Register() error

	// Unregister does any cleanup required during module removal. It is
	// implicitly called by UnregisterModule.
	Unregister() error

	// Parameters returns a ParameterMap with the configurable settings for
	// this module as keys and empty strings as values (except for entries
	// with default values). One could, then, fill/change the values in this
	// ParameterMap and pass it to Configure.
	Parameters() *ParameterMap

	// Configure configures the module usin the given ParameterMap. Usually
	// this needs to be called before actually trying to use the module.
	// Parameters above can be used to check what are the possible
	// configuration options.
	Configure(params *ParameterMap) error

	// New creates and registers a new instance of this module with
	// the given specific id. Returns a reference to the new Module and a
	// nil error on success and a non-nil error on failure. Note that
	// this must be implemented by each Module that cares about multiple
	// instances of it existing at the same time and it is the
	// responsibility of each implementation to register (calling
	// RegisterModule()) the new instance.
	New(specificId string) (Module, error)

	// Ready returns true if the module is ready to be used. False
	// otherwise. If the module requires configuration, this will return
	// false until Configure is called succesfully.
	Ready() bool
}

Module is the interface that all modules must adhere to. This includes methods required by all module implementations. Note this is the bare minimum required and modules that just implement this are pretty useless.

func GetDefaultModuleByGenericId

func GetDefaultModuleByGenericId(genericModuleId string) Module

GetDefaultModuleByGenericId returns the default module represented by the given genericModuleId. There may not be a default instance available.

func GetModuleById

func GetModuleById(genericModuleId, specificModuleId string) Module

GetModuleById returns the Module instance associated with the given genericModuleId and SpecificModuleId.

type ModuleMap

type ModuleMap map[string]Module

ModuleMap is a container for Modules keyed by specific id.

func GetModulesByGenericId

func GetModulesByGenericId(genericModuleId string) ModuleMap

GetModulesByGenericId returns a ModuleMap with all modules with the given genericModuleId.

type ParameterMap

type ParameterMap map[string]string

ParameterMap defines a list of parameters used for configuring a module.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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