plumbing

package
v2.0.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package plumbing defines generic types for the dependency injection mechanisms in rig.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

type Factory[R any, T any] func(R) (T, bool)

Factory is a function that takes a parameter of type R and returns a value of type T or an error.

type LazyService

type LazyService[S any, T any] struct {
	// contains filtered or unexported fields
}

LazyService is a generic lazy-initializer for a provider that can take a different types of parameter.

func NewLazyService

func NewLazyService[S any, T any](p provider[S, T], source S) *LazyService[S, T]

NewLazyService creates a new instance of Service with the given provider.

func (*LazyService[R, T]) Get

func (s *LazyService[R, T]) Get() (T, error)

Get retrieves the service value, initializing it if necessary.

type Provider

type Provider[R any, T any] struct {
	// contains filtered or unexported fields
}

Provider is a generic provider of values of type T that can be initialized with a value of type R.

func NewProvider

func NewProvider[R any, T any](err error) *Provider[R, T]

NewProvider creates a new instance of Provider. The error is returned if no factory can produce a value of type T.

func (*Provider[R, T]) Get

func (p *Provider[R, T]) Get(r R) (T, error)

Get retrieves the first value of type T from the Factories in the Provider. If none can be found, the error supplied at creation time is returned. The first factory that does not error is moved to the front of the list to optimize future lookups.

func (*Provider[R, T]) GetAll

func (p *Provider[R, T]) GetAll(r R) ([]T, error)

GetAll retrieves all values of type T from the Factories in the Provider. If none that does not error can be found, the error supplied at creation time is returned.

func (*Provider[R, T]) Register

func (p *Provider[R, T]) Register(f Factory[R, T])

Register adds a new factory to the provider.

type Service

type Service[T any] interface {
	Get() (T, error)
}

Service is anything that can be retrieved via Get and that can fail and return an error.

Jump to

Keyboard shortcuts

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