service

package
v0.0.0-...-29a5503 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2018 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDefEmptyName   = errors.New("service: Definition name must be non-empty and not whitespace only")
	ErrDefEmptyPrefix = errors.New("service: Definition prefix must be non-empty and not witespace only")
	ErrDefEmptyVendor = errors.New("service: Definition vendor must be non-empty and not witespace only")
	ErrDefNilInit     = errors.New("service: Definition must have an Initializer function")
	ErrDefNilType     = errors.New("service: Definition must have a type")
	ErrNilDefs        = errors.New("service: \"definitions\" in ContainerBuilder cannot be nil")
)

Functions

This section is empty.

Types

type Builder

type Builder struct {
	ServiceConfigs map[string]config.ServiceConfig
	// contains filtered or unexported fields
}

Builder is a simple implementation of ContainerBuilder.

func (*Builder) Build

func (b *Builder) Build() (Container, error)

Build creates the container once all definitions have been place in it. Note that dependencies must be inserted in order, for now.

func (*Builder) Insert

func (b *Builder) Insert(def Definition) error

Insert a new definition into the Builder.

type Container

type Container interface {
	// Get should return the service which corresponds to the given name. If
	// the service doesn't exist, there should be an error returned.
	Get(name string) (interface{}, error)
	// Has should return true if a service with the given name exists, false
	// otherwise.
	Has(name string) bool
	// Assign should take a reference to a variable and should assign the service,
	// which corresponds to that variable's type. If such a service doesn't exist
	// an error is returned
	Assign(to interface{}) error
	// Has Assignable should check if the type of the passed variable can be assigned to
	// and return the corresponding bool value
	HasAssignable(typ interface{}) bool
}

Container is the basic interface which is to be implemented by anything providing a service container.

type ContainerBuilder

type ContainerBuilder interface {
	// Insert inserts a service Definition.
	Insert(def Definition)
	// Build should be called when all services have been inserted into the
	// builder, which then returns a finished, usable Container or an error.
	Build() (*Container, error)
}

ContainerBuilder builds the service Container by having service Definitions inserted into it and the build method called.

type Definition

type Definition struct {
	// Vendor is the vendor prefix for the service name.
	// Leading and trailing whitespace will be trimmed.
	// This is the first step torwards service decoration.
	Vendor string
	// Prefix.
	// Leading and trailing whitespace will be trimmed.
	Prefix string
	// Name is what the service should be referred to in the Container.
	// Leading and trailing whitespace will be trimmed.
	Name string
	// Type is the underlying type of that service, this should almost always be an interface
	Type reflect.Type
	// Dependencies contains the Names of the other services this service
	// dependends on.
	Dependencies []string
	// Init is what actually returns the service, using the Dependencies and
	// Configuration.
	Init Initializer
}

Definition contains the metadata and Initializer required to use a service. All valid Definitions require the Name and Init fields, the rest are optional.

type Initializer

type Initializer func(deps, conf config.ServiceConfig) (interface{}, error)

Initializer is the function to be provided by anything that wants to return a service.

type MissingDepError

type MissingDepError struct {
	// DepName is the name of the dependency which could not be found.
	DepName string
	// ServiceName is the name of the service which is missing the
	// dependency.
	ServiceName string
}

MissingDepError implements error, indicating that a dependency is missing from a ContainerBuilder.

func (*MissingDepError) Error

func (e *MissingDepError) Error() string

type MissingServiceError

type MissingServiceError struct {
	// ServiceName is the name of the service which could not be found.
	ServiceName string
}

MissingServiceError implements error, indicating that a service is missing from a service Container.

func (*MissingServiceError) Error

func (e *MissingServiceError) Error() string

type SimpleContainer

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

SimpleContainer is a simple implementation of Container.

func (*SimpleContainer) Assign

func (sc *SimpleContainer) Assign(to interface{}) error

Assign attempts to assign the service to the passed argument, that corresponds with its underlying type if that isn't possible, an error is returned and the passed variable remains unassigned

func (*SimpleContainer) Get

func (sc *SimpleContainer) Get(name string) (interface{}, error)

Get returns the service with the matching name. If it doesn't exist, an error is returned.

func (*SimpleContainer) Has

func (sc *SimpleContainer) Has(name string) bool

Has checks if a service under the given name exists within the container.

func (*SimpleContainer) HasAssignable

func (sc *SimpleContainer) HasAssignable(typ interface{}) bool

HasAssignable checks if the container has a service for the type of the passed argument

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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