controller

package
v0.17.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package controller contains root neuron structure. It is responsible for getting access to registered models and their repositories. It contains the configuration, validators and default query processor.

Index

Constants

View Source
const DefaultStoreName = "nrn_default_store"

DefaultStoreName is the name of the default store.

Variables

View Source
var (
	// ErrController defines major classification for the controller.
	ErrController = errors.New("controller")
	// ErrRepository is an error related with the repository.
	ErrRepository = errors.Wrap(ErrController, "repository")
	// ErrRepositoryNotFound is an error when repository is not found.
	ErrRepositoryNotFound = errors.Wrap(ErrRepository, "not found")
	// ErrRepositoryAlreadyRegistered class of errors when repository is already registered.
	ErrRepositoryAlreadyRegistered = errors.Wrap(ErrRepository, "already registered")
	// ErrStoreAlreadySet is an error when the store is already set at some name.
	ErrStoreAlreadySet = errors.Wrap(ErrController, "store already set")
)

Functions

func CtxStore added in v0.16.0

func CtxStore(parent context.Context, c *Controller) context.Context

CtxStore stores the controller in the context.

Types

type Closer added in v0.17.0

type Closer interface {
	Close(ctx context.Context) error
}

Closer is an interface that closes all connection for given instance.

type Controller

type Controller struct {
	// Config is the configuration struct for the controller.
	Options *Options
	// ModelMap is a mapping for the model's structures.
	ModelMap *mapping.ModelMap
	// Repositories are the controller registered repositories.
	Repositories map[string]repository.Repository
	// ModelRepositories is the mapping between the model and related repositories.
	ModelRepositories map[*mapping.ModelStruct]repository.Repository
	// DefaultService is the default service or given controller.
	DefaultRepository repository.Repository
	// Stores is the mapping of the stores with their names.
	Stores map[string]store.Store
	// DefaultStore is the default key-value store used by this controller.
	DefaultStore store.Store
}

Controller is the structure that contains, initialize and control the flow of the application. It contains repositories, model definitions.

func CtxGet added in v0.16.0

func CtxGet(ctx context.Context) (*Controller, bool)

CtxGet gets the controller from the context.

func New

func New(options *Options) *Controller

New creates new controller for provided options.

func NewDefault

func NewDefault() *Controller

NewDefault creates new default controller based on the default config.

func (*Controller) CloseAll added in v0.15.0

func (c *Controller) CloseAll(ctx context.Context) error

CloseAll gently closes repository connections.

func (*Controller) DialAll added in v0.15.0

func (c *Controller) DialAll(ctx context.Context) error

DialAll establish connections for all repositories.

func (*Controller) GetRepository added in v0.15.0

func (c *Controller) GetRepository(model mapping.Model) (repository.Repository, error)

GetRepository gets the repository for the provided model.

func (*Controller) GetRepositoryByModelStruct added in v0.16.0

func (c *Controller) GetRepositoryByModelStruct(mStruct *mapping.ModelStruct) (repository.Repository, error)

GetRepositoryByModelStruct gets the service by model struct.

func (*Controller) HealthCheck added in v0.15.0

func (c *Controller) HealthCheck(ctx context.Context) (*repository.HealthResponse, error)

HealthCheck checks all repositories health.

func (*Controller) ListModels added in v0.15.0

func (c *Controller) ListModels() []*mapping.ModelStruct

ListModels returns a list of registered models for given controller.

func (*Controller) MapRepositoryModels added in v0.16.0

func (c *Controller) MapRepositoryModels(r repository.Repository, models ...mapping.Model) (err error)

MapRepositoryModels maps models to their repositories. If the model doesn't have repository name mapped then the controller would match default repository.

func (*Controller) MigrateModels added in v0.15.0

func (c *Controller) MigrateModels(ctx context.Context, models ...mapping.Model) error

MigrateModels updates or creates provided models representation in their related repositories. A representation of model might be a database table, collection etc. Model's repository must implement repository.Migrator.

func (*Controller) ModelStruct

func (c *Controller) ModelStruct(model mapping.Model) (*mapping.ModelStruct, error)

ModelStruct gets the model struct on the base of the provided model

func (*Controller) MustModelStruct added in v0.15.0

func (c *Controller) MustModelStruct(model mapping.Model) *mapping.ModelStruct

MustModelStruct gets the model struct from the cached model Map. Panics if the model does not exists in the map.

func (*Controller) Now added in v0.15.0

func (c *Controller) Now() time.Time

Now gets and returns current timestamp. If the configs specify the function might return UTC timestamp.

func (*Controller) RegisterModels

func (c *Controller) RegisterModels(models ...mapping.Model) (err error)

RegisterModels registers provided models within the context of the provided Controller.

func (*Controller) RegisterRepositories added in v0.16.0

func (c *Controller) RegisterRepositories(repositories ...repository.Repository)

RegisterRepositories registers provided repositories.

func (*Controller) RegisterRepositoryModels added in v0.17.0

func (c *Controller) RegisterRepositoryModels() error

RegisterRepositoryModels registers models in the repositories that implements repository.ModelRegistrar.

func (*Controller) RegisterStore added in v0.17.0

func (c *Controller) RegisterStore(name string, s store.Store) error

RegisterStore registers store 's' at 'name'.

func (*Controller) SetDefaultRepository added in v0.16.0

func (c *Controller) SetDefaultRepository(r repository.Repository) error

SetDefaultRepository sets the default repository for given controller.

func (*Controller) SetDefaultStore added in v0.17.0

func (c *Controller) SetDefaultStore(s store.Store) error

SetDefaultStore sets the default store.in the controller.

func (*Controller) SetUnmappedModelRepositories added in v0.17.0

func (c *Controller) SetUnmappedModelRepositories() error

SetUnmappedModelRepositories checks if all models have their repositories mapped.

type Dialer added in v0.17.0

type Dialer interface {
	Dial(ctx context.Context) error
}

Dialer is an interface that starts the connection for the store.

type Initializer added in v0.17.0

type Initializer interface {
	Initialize(c *Controller) error
}

Initializer is an interface used to initialize services, repositories etc.

type Options added in v0.16.0

type Options struct {
	// NamingConvention is the naming convention used while preparing the models.
	// Allowed values:
	// - camel
	// - lower_camel
	// - snake
	// - kebab
	NamingConvention mapping.NamingConvention
	// SynchronousConnections defines if the query relation includes would be taken concurrently.
	SynchronousConnections bool
	// UTCTimestamps is the flag that defines the format of the timestamps.
	UTCTimestamps bool
	// DefaultNotNullFields defines if the model non-pointer fields should be marked as not null by default.
	DefaultNotNullFields bool
	// ModelNotNullFields defines not null fields for specified model only.
	ModelNotNullFields map[mapping.Model]struct{}
}

Options defines the configuration for the Options.

Jump to

Keyboard shortcuts

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