types

Official documentation for the Parsley types package.
import "github.com/matzefriedrich/parsley/pkg/types"

Index

  • Constants
  • Variables
  • func NewDependencyError(msg string) error
  • func NewReflectionError(msg string, initializers ...ParsleyErrorFunc) error
  • func NewRegistryError(msg string, initializers ...ParsleyErrorFunc) error
  • func NewResolverError(msg string, initializers ...ParsleyErrorFunc) error
  • type DependencyError
  • type DependencyInfo
  • type FunctionInfo
  • type FunctionParameterInfo
  • type LifetimeScope
  • type ModuleFunc
  • type NamedService
  • type ParsleyAggregateError
    • func (f ParsleyAggregateError) Error() string
    • func (f ParsleyAggregateError) Errors() []error
    • func (f ParsleyAggregateError) Is(err error) bool
  • type ParsleyError
    • func (f ParsleyError) Error() string
    • func (f ParsleyError) Is(err error) bool
    • func (f ParsleyError) Unwrap() error
  • type ParsleyErrorFunc
    • func ForServiceType(serviceType string) ParsleyErrorFunc
    • func WithAggregatedCause(errs ...error) ParsleyErrorFunc
    • func WithCause(err error) ParsleyErrorFunc
  • type ParsleyErrorWithServiceTypeName
  • type ReflectionError
  • type RegistryError
    • func (r *RegistryError) MatchesServiceType(name string) bool
    • func (r *RegistryError) ServiceTypeName(name string)
  • type Resolver
  • type ResolverError
    • func (r *ResolverError) ServiceTypeName(name string)
  • type ResolverOptionsFunc
  • type ServiceKey
    • func NewServiceKey(value string) ServiceKey
    • func (s ServiceKey) String() string
  • type ServiceRegistration
  • type ServiceRegistrationList
  • type ServiceRegistrationSetup
  • type ServiceRegistry
  • type ServiceRegistryAccessor
  • type ServiceType
    • func MakeServiceType[T any]() ServiceType
    • func ServiceTypeFrom(t reflect.Type) ServiceType

Constants

const (
    ErrorRequiresFunctionValue               = "the given value is not function"
    ErrorCannotRegisterModule                = "failed to register module"
    ErrorTypeAlreadyRegistered               = "type already registered"
    ErrorServiceAlreadyLinkedWithAnotherList = "service already linked with another list"
    ErrorFailedToRegisterType                = "failed to register type"
)

const (
    ErrorServiceTypeNotRegistered               = "service type is not registered"
    ErrorRequiredServiceNotRegistered           = "required service type is not registered"
    ErrorCannotResolveService                   = "cannot resolve service"
    ErrorAmbiguousServiceInstancesResolved      = "the resolve operation resulted in multiple service instances"
    ErrorActivatorFunctionInvalidReturnType     = "activator function has an invalid return type"
    ErrorCircularDependencyDetected             = "circular dependency detected"
    ErrorCannotBuildDependencyGraph             = "failed to build dependency graph"
    ErrorInstanceCannotBeNil                    = "instance cannot be nil"
    ErrorServiceTypeMustBeInterface             = "service type must be an interface"
    ErrorCannotRegisterTypeWithResolverOptions  = "cannot register type with resolver options"
    ErrorCannotCreateInstanceOfUnregisteredType = "failed to create instance of unregistered type"
)

const (
    ErrorInstanceAlreadySet = "instance already set"
)

Variables

var (

    // ErrRequiresFunctionValue indicates that the provided value is not a function.
    ErrRequiresFunctionValue = errors.New(ErrorRequiresFunctionValue)

    // ErrCannotRegisterModule indicates that the module registration process has failed.
    ErrCannotRegisterModule = errors.New(ErrorCannotRegisterModule)

    // ErrTypeAlreadyRegistered indicates that an attempt was made to register a type that is already registered.
    ErrTypeAlreadyRegistered = errors.New(ErrorTypeAlreadyRegistered)

    // ErrFailedToRegisterType indicates that the attempt to register a type has failed.
    ErrFailedToRegisterType = errors.New(ErrorFailedToRegisterType)
)

var (

    // ErrServiceTypeNotRegistered is returned when attempting to resolve a service type that has not been registered.
    ErrServiceTypeNotRegistered = errors.New(ErrorServiceTypeNotRegistered)

    // ErrRequiredServiceNotRegistered is returned when a required service type is not registered.
    ErrRequiredServiceNotRegistered = errors.New(ErrorRequiredServiceNotRegistered)

    // ErrActivatorFunctionInvalidReturnType is returned when an activator function has an invalid return type.
    ErrActivatorFunctionInvalidReturnType = errors.New(ErrorCannotResolveService)

    // ErrCannotBuildDependencyGraph is returned when the resolver fails to build a dependency graph due to missing dependencies or other issues.
    ErrCannotBuildDependencyGraph = errors.New(ErrorCannotBuildDependencyGraph)

    // ErrCircularDependencyDetected is returned when a circular dependency is detected during the resolution process.
    ErrCircularDependencyDetected = errors.New(ErrorCircularDependencyDetected)

    // ErrInstanceCannotBeNil is returned when an instance provided is nil, but a non-nil value is required.
    ErrInstanceCannotBeNil = errors.New(ErrorInstanceCannotBeNil)

    // ErrServiceTypeMustBeInterface is returned when a service type is not an interface.
    ErrServiceTypeMustBeInterface = errors.New(ErrorServiceTypeMustBeInterface)

    // ErrCannotRegisterTypeWithResolverOptions is returned when the resolver failed to register a type via resolver options.
    ErrCannotRegisterTypeWithResolverOptions = errors.New(ErrorCannotRegisterTypeWithResolverOptions)

    // ErrCannotCreateInstanceOfUnregisteredType is returned when the resolver fails to instantiate a type that has not been registered.
    ErrCannotCreateInstanceOfUnregisteredType = errors.New(ErrorCannotCreateInstanceOfUnregisteredType)
)

var (
    // ErrInstanceAlreadySet is returned when there is an attempt to set an instance that is already set.
    ErrInstanceAlreadySet = errors.New(ErrorInstanceAlreadySet)
)

func NewDependencyError

func NewDependencyError(msg string) error

NewDependencyError creates a new DependencyError with the provided message.

func NewReflectionError

func NewReflectionError(msg string, initializers ...ParsleyErrorFunc) error

NewReflectionError creates a new ReflectionError with a specified message and optional initializers.

func NewRegistryError

func NewRegistryError(msg string, initializers ...ParsleyErrorFunc) error

NewRegistryError creates a new RegistryError with the given message and initializers to modify the error.

func NewResolverError

func NewResolverError(msg string, initializers ...ParsleyErrorFunc) error

NewResolverError creates a new ResolverError with the provided message and applies optional ParsleyErrorFunc initializers.

type DependencyError

DependencyError represents an error that occurs due to a missing or failed dependency. This error type encapsulates a ParsleyError.

type DependencyError struct {
    ParsleyError
}

type DependencyInfo

DependencyInfo provides functionality to manage dependency information.

type DependencyInfo interface {
    // AddRequiredServiceInfo adds a child dependency to the current dependency info.
    AddRequiredServiceInfo(child DependencyInfo)

    // CreateInstance creates an instance of the service associated with this dependency info.
    CreateInstance(ctx context.Context) (interface{}, error)

    // Consumer returns the parent dependency for the current dependency info.
    Consumer() DependencyInfo

    // HasInstance checks if an instance has already been created for the dependency represented by the current DependencyInfo object.
    HasInstance() bool

    // Instance retrieves the created instance of the service associated with this dependency info.
    Instance() interface{}

    // Registration gets the service registration of the current dependency info.
    Registration() ServiceRegistration

    // RequiredServiceTypes gets the service types required by this dependency info.
    RequiredServiceTypes() []ServiceType

    // RequiredServices retrieves the instances of services required by this dependency info.
    RequiredServices() ([]interface{}, error)

    // ServiceTypeName gets the name of the service type associated with this dependency info.
    ServiceTypeName() string

    // SetInstance sets the instance for the current dependency info.
    SetInstance(instance interface{}) error
}

type FunctionInfo

FunctionInfo Stores information about a service activator function. This interface supports the internal infrastructure.

type FunctionInfo interface {
    fmt.Stringer
    Name() string
    Parameters() []FunctionParameterInfo
    ReturnType() ServiceType
    ParameterTypes() []ServiceType
}

type FunctionParameterInfo

type FunctionParameterInfo interface {
    fmt.Stringer
    Type() ServiceType
}

type LifetimeScope

LifetimeScope represents the duration for which a service or object instance is retained.

type LifetimeScope uint

const (

    // LifetimeTransient represents a transient lifetime where a new instance is created each time it is requested.
    LifetimeTransient LifetimeScope = iota

    // LifetimeScoped represents a scoped lifetime where a single instance is created per scope.
    LifetimeScoped

    // LifetimeSingleton represents a single instance scope that persists for the lifetime of the application.
    LifetimeSingleton
)

type ModuleFunc

ModuleFunc defines a function used to register services with the given service registry.

type ModuleFunc func(registry ServiceRegistry) error

type NamedService

NamedService is a generic interface defining a service with a name and an activator function.

type NamedService[T any] interface {
    Name() string
    ActivatorFunc() any
}

type ParsleyAggregateError

ParsleyAggregateError represents an aggregate of multiple errors.

type ParsleyAggregateError struct {
    Msg string
    // contains filtered or unexported fields
}

func (ParsleyAggregateError) Error

func (f ParsleyAggregateError) Error() string

Error returns the message associated with the ParsleyAggregateError.

func (ParsleyAggregateError) Errors

func (f ParsleyAggregateError) Errors() []error

Errors returns the slice of errors contained within ParsleyAggregateError.

func (ParsleyAggregateError) Is

func (f ParsleyAggregateError) Is(err error) bool

Is checks if the given error is equivalent to any error within the ParsleyAggregateError.

type ParsleyError

ParsleyError represents an error with an associated message and optional underlying cause.

type ParsleyError struct {
    Msg string
    // contains filtered or unexported fields
}

func (ParsleyError) Error

func (f ParsleyError) Error() string

Error returns the message associated with the ParsleyError.

func (ParsleyError) Is

func (f ParsleyError) Is(err error) bool

Is compares the current ParsleyError's message with another error's message to determine if they are the same.

func (ParsleyError) Unwrap

func (f ParsleyError) Unwrap() error

Unwrap returns the underlying cause of the ParsleyError, allowing for error unwrapping functionality.

type ParsleyErrorFunc

ParsleyErrorFunc is a function type that modifies a given error.

type ParsleyErrorFunc func(e error)

func ForServiceType

func ForServiceType(serviceType string) ParsleyErrorFunc

ForServiceType creates a ParsleyErrorFunc that sets the service type name on errors that implement the ParsleyErrorWithServiceTypeName interface.

func WithAggregatedCause

func WithAggregatedCause(errs ...error) ParsleyErrorFunc

WithAggregatedCause returns a ParsleyErrorFunc that sets an aggregated error cause with the provided errors.

func WithCause

func WithCause(err error) ParsleyErrorFunc

WithCause wraps a given error within a ParsleyError.

type ParsleyErrorWithServiceTypeName

ParsleyErrorWithServiceTypeName defines an interface for setting the service type name on errors.

type ParsleyErrorWithServiceTypeName interface {
    ServiceTypeName(name string)
}

type ReflectionError

ReflectionError represents an error specifically related to reflection operations, extending ParsleyError.

type ReflectionError struct {
    ParsleyError
}

type RegistryError

RegistryError represents an error that gets returned for failing registry operations.

type RegistryError struct {
    ParsleyError
    // contains filtered or unexported fields
}

func (*RegistryError) MatchesServiceType

func (r *RegistryError) MatchesServiceType(name string) bool

MatchesServiceType checks if the service type name of the RegistryError matches the specified name.

func (*RegistryError) ServiceTypeName

func (r *RegistryError) ServiceTypeName(name string)

ServiceTypeName sets the service type name of the RegistryError.

type Resolver

Resolver provides methods to resolve registered services based on types.

type Resolver interface {

    // Resolve attempts to resolve all registered services of the specified ServiceType.
    Resolve(ctx context.Context, serviceType ServiceType) ([]interface{}, error)

    // ResolveWithOptions resolves services of the specified type using additional options and returns a list of resolved services or an error.
    ResolveWithOptions(ctx context.Context, serviceType ServiceType, options ...ResolverOptionsFunc) ([]interface{}, error)
}

type ResolverError

ResolverError represents an error that gets returned for failing service resolver operations.

type ResolverError struct {
    ParsleyError
    // contains filtered or unexported fields
}

func (*ResolverError) ServiceTypeName

func (r *ResolverError) ServiceTypeName(name string)

ServiceTypeName sets the service type name for the ResolverError instance.

type ResolverOptionsFunc

ResolverOptionsFunc represents a function that configures a service registry used by the resolver.

type ResolverOptionsFunc func(registry ServiceRegistry) error

type ServiceKey

ServiceKey represents a unique key for identifying services in the service registry.

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

func NewServiceKey

func NewServiceKey(value string) ServiceKey

NewServiceKey creates a new ServiceKey with the given value.

func (ServiceKey) String

func (s ServiceKey) String() string

String Gets the value of the current ServiceKey instance.

type ServiceRegistration

ServiceRegistration represents a service registrations.

type ServiceRegistration interface {

    // Id Returns the unique identifier of the service registration.
    Id() uint64

    // InvokeActivator calls the activator function with the provided parameters and returns the resulting instance and any error.
    InvokeActivator(ctx context.Context, params ...interface{}) (interface{}, error)

    // IsSame checks if the provided ServiceRegistration equals the current ServiceRegistration.
    IsSame(other ServiceRegistration) bool

    // LifetimeScope returns the LifetimeScope associated with the service registration.
    LifetimeScope() LifetimeScope

    // RequiredServiceTypes returns a slice of ServiceType, containing all service types required by the service registration.
    RequiredServiceTypes() []ServiceType

    // ServiceType retrieves the type of the service being registered.
    ServiceType() ServiceType
}

type ServiceRegistrationList

ServiceRegistrationList provides functionality to manage a list of service registrations. This interface supports internal infrastructure services.

type ServiceRegistrationList interface {

    // AddRegistration adds a new service registration to the list.
    AddRegistration(registration ServiceRegistrationSetup) error

    // Id returns the unique identifier of the service registration list.
    Id() uint64

    // Registrations returns a slice of ServiceRegistration, containing all registrations in the list.
    Registrations() []ServiceRegistration

    // IsEmpty checks if the service registration list contains any registrations.
    // It returns true if the list is empty, otherwise false.
    IsEmpty() bool
}

type ServiceRegistrationSetup

ServiceRegistrationSetup extends ServiceRegistration and supports internal infrastructure services.

type ServiceRegistrationSetup interface {
    ServiceRegistration

    // SetId sets the unique identifier for the service registration. This method supports internal infrastructure and is not intended to be used by your code.
    SetId(id uint64) error
}

type ServiceRegistry

ServiceRegistry provides methods to map service types to activator functions. The service registration organizes and stores the metadata required by the service resolver.

type ServiceRegistry interface {
    ServiceRegistryAccessor

    // CreateLinkedRegistry creates and returns a new ServiceRegistry instance linked to the current registry. A linked service registry is an empty service registry.
    CreateLinkedRegistry() ServiceRegistry

    // CreateScope creates and returns a scoped ServiceRegistry instance which inherits all service registrations from the current ServiceRegistry instance.
    CreateScope() ServiceRegistry

    // GetServiceRegistrations retrieves all service registrations.
    GetServiceRegistrations() ([]ServiceRegistration, error)

    // IsRegistered checks if a service of the specified ServiceType is registered in the service registry.
    IsRegistered(serviceType ServiceType) bool

    // Register registers a service with its activator function and defines its lifetime scope with the service registry.
    Register(activatorFunc any, scope LifetimeScope) error

    // RegisterModule registers one or more modules, encapsulated as ModuleFunc, with the service registry. A module is a logical unit of service registrations.
    RegisterModule(modules ...ModuleFunc) error
}

type ServiceRegistryAccessor

ServiceRegistryAccessor provides methods to access and retrieve service registrations from the registry.

type ServiceRegistryAccessor interface {

    // TryGetServiceRegistrations attempts to retrieve all service registrations for the given service type.
    // Returns the service registration list and true if found, otherwise returns false.
    TryGetServiceRegistrations(serviceType ServiceType) (ServiceRegistrationList, bool)

    // TryGetSingleServiceRegistration attempts to retrieve a single service registration for the given service type.
    // Returns the service registration and true if found, otherwise returns false.
    TryGetSingleServiceRegistration(serviceType ServiceType) (ServiceRegistration, bool)
}

type ServiceType

ServiceType represents a service type.

type ServiceType interface {

    // Name returns the name of the service type.
    Name() string

    // PackagePath returns the package path of the service type.
    PackagePath() string

    // ReflectedType returns the underlying reflect.Type representation of the service type.
    ReflectedType() reflect.Type

    // LookupKey retrieves the ServiceKey associated with the service type.
    LookupKey() ServiceKey
}

func MakeServiceType

func MakeServiceType[T any]() ServiceType

MakeServiceType creates a ServiceType instance for the specified generic type T.

func ServiceTypeFrom

func ServiceTypeFrom(t reflect.Type) ServiceType

ServiceTypeFrom creates a ServiceType from the given reflect.Type. Supports pointer, interface, function, slice, and struct types. The function panics, if t is of an unsupported kind is given.