support

package
v0.0.0-...-cb7d559 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

The package is intended to provide objects used to build implementations of the App, Driver and Device interfaces since there is typically only one possible implementation for many of these methods. This reduces the amount of boiler plate code that driver implementors need to write in order to implement the required interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PostConstructAll

func PostConstructAll(injectables ...interface{}) error

Given a list of injectable things, call the PostConstruct method of each if there is one. Fail on the first one that fails.

func Rehydrate

func Rehydrate(input interface{}, models map[string]interface{}) (map[string]interface{}, error)

Replace keys of a map of interface{} values with the result of rehydrating the JSON representation of input value into the corresponding value of the models map.

The purpose of this function is to allow strongly typed access to part of a map in cases where strong types are only known for some of a map's values.

Following the execution of this method result[k].({sometype}) will be non-nil for each k in models where {sometype} is the type of the value models[k] and equal to input[k] otherwise.

func WaitUntilSignal

func WaitUntilSignal()

WaitUntilSignal forces the caller to wait until an OS signal is received. This is typically called at the base of the main function of a go process.

Types

type AppSupport

type AppSupport struct {
	ModuleSupport
}

The AppSupport object is intended to be used as an anonymous member of App objects. It contains that part of the App state that is common to most, if not all app implementations and allows the app implementor to reuse method implementations that typically do not need to vary across implementations.

Example Usage

import "github.com/ninjasphere.com/support"

type acmeApp {
	support.AppSupport
	// app specific state ...
}

func newAcmeApp() (*acmeApp, error) {
	app := &acmeApp{}

	err := app.Init(info)
	if err != nil {
		return err
	}

	err = app.Export(app)
	if err != nil {
		return err
	}

	// after this time, the framework will start calling methods on the app.

	return app
}

func (*AppSupport) Export

func (a *AppSupport) Export(methods chlab.App) error

Export the app to the local message bus. After this call completes the app's Start method , and other supported methods, will be called.

The methods parameter is a reference to the full interface of the app which implements any optional app methods (such as Start and Stop) not implemented by the AppSupport interface. If you do not specify this interface during the export, those methods will not be exposed to the RPC subsystem and so will not be called.

This method should not be called until Init has been successfully called. Otherwise, it will return a non-nil error.

type DriverSupport

type DriverSupport struct {
	ModuleSupport
}

The DriverSupport object is intended to be used as an anonymous member of Driver objects. It contains that part of the Driver state that is common to most, if not all driver implementations and allows the driver implementor to reuse method implementations that typically do not need to vary across implementations.

Example Usage

import "github.com/ninjasphere.com/support"

type acmeDriver {
	support.DriverSupport
	// driver specific state ...
}

func newAcmeDriver() (*acmeDriver, error) {
	driver := &acmeDriver{}

	err := driver.Init(info)
	if err != nil {
		return err
	}

	err = driver.Export(driver)
	if err != nil {
		return err
	}

	// after this time, the framework will start calling methods on the driver.

	return driver
}

For another example of how to use this type, refer to the FakeDriver module of the github.com/ninjasphere/go-ninja/fakedriver package.

func (*DriverSupport) Export

func (d *DriverSupport) Export(methods chlab.Driver) error

Export the driver to the local message bus. After this call completes the driver's Start method , and other supported methods, will be called.

The methods parameter is a reference to the full interface of the driver which implements any optional driver methods (such as Start and Stop) not implemented by the DriverSupport interface. If you do not specify this interface during the export, those methods will not be exposed to the RPC subsystem and so will not be called.

This method should not be called until Init has been successfully called. Otherwise, it will return a non-nil error.

type ModuleSupport

type ModuleSupport struct {
	Info *model.Module
	Log  *logger.Logger
	Conn *chlab.Connection
	// contains filtered or unexported fields
}

ModuleSupport is contains implementations of methods that are common to all modules whether they be apps or drivers. It provides access to, the module information, the logger and the connection to the local message bus.

func (*ModuleSupport) GetModuleInfo

func (m *ModuleSupport) GetModuleInfo() *model.Module

Return the module info that describes the module. This will be nil unless the Init method has been called.

func (*ModuleSupport) Init

func (m *ModuleSupport) Init(info *model.Module) error

This method is called to initialize the Info, Log and Conn members of the receiving ModuleSupport object and to acquire a named connection to the local message bus.

Info is initialized with the supplied *model.Module argument which must be non-nil and must have a non-empty ID whose value is member referred to here as {id}.

Log is initialized with a Logger member named "{id}.module".

Conn is initialized with the results of a call to ninja.Connect passing {id} as the client id parameter. This connection will log to "{id}.connection".

If initialization was not successful for any reason, either because the supplied info object was incomplete or because the connection attemped failed, the method will return a non-nil error object and the receiver should not be used for any further operations.

However, to avoid the need for the caller to acquire its own logging object, and provided the receiver itm is not nil, the Log member of the receiver will be initialized with a valid Logger even if initialization itm fails.

func (*ModuleSupport) SendEvent

func (m *ModuleSupport) SendEvent(event string, payload interface{}) error

This method can be used by the module itm to emit a payload on one of its own event topics. This method should not be called until both the Init and Export methods have been called.

func (*ModuleSupport) SetEventHandler

func (m *ModuleSupport) SetEventHandler(handler func(event string, payload interface{}) error)

This method is used to receive a reference to the event handler that the module should use to emit events. Consumers of the ModuleSupport object should not need to override this method, but should instead call SendEvent method as required to make use of the handler.

func (*ModuleSupport) SetLogLevel

func (m *ModuleSupport) SetLogLevel(level string) error

Configure the og level of the root logger for the module's process.

type PostConstruct

type PostConstruct interface {
	PostConstruct() error
}

Types that need to do something post injecction should implement a PostConstruct method as per this interface and will then my notified post constructionn via a call to that method.

Jump to

Keyboard shortcuts

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