di

package
v1.0.83 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package implements functions and data types supporting dependency injection patterns

The fundamental building block is the concept of a Dependency. This describes the abstract concept of a function, service or value together with its type. Examples for dependencies can be as simple as configuration values such as the API URL for a service, the current username, a Dependency could be the map of the configuration environment, an http client or as complex as a service interface. Important is that a Dependency only defines the concept but not the implementation.

The implementation of a Dependency is called a [Provider], the dependency of an `API URL` could e.g. be realized by a provider that consults the environment to read the information or a config file or simply hardcode it. In many cases the implementation of a [DIE.Provider] depends in turn on other [Dependency]s (but never directly on other [DIE.Provider]s), a provider for an `API URL` that reads the information from the environment would e.g. depend on a Dependency that represents this environment.

It is the resposibility of the [DIE.InjectableFactory] to

[Provider]: github.com/IBM/fp-go/di/erasure.Provider

Copyright (c) 2023 IBM Corp. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2023 IBM Corp. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstProvider

func ConstProvider[R any](token InjectionToken[R], value R) DIE.Provider

ConstProvider simple implementation for a provider with a constant value

func MakeProvider0

func MakeProvider0[R any](
	token InjectionToken[R],
	fct IOE.IOEither[error, R],
) DIE.Provider

func MakeProvider1

func MakeProvider1[T1 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	f func(T1) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider1 creates a [DIE.Provider] for an InjectionToken from a function with 1 dependencies

func MakeProvider10 added in v1.0.82

func MakeProvider10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	d9 Dependency[T9],
	d10 Dependency[T10],
	f func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider10 creates a [DIE.Provider] for an InjectionToken from a function with 10 dependencies

func MakeProvider2

func MakeProvider2[T1, T2 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	f func(T1, T2) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider2 creates a [DIE.Provider] for an InjectionToken from a function with 2 dependencies

func MakeProvider3 added in v1.0.69

func MakeProvider3[T1, T2, T3 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	f func(T1, T2, T3) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider3 creates a [DIE.Provider] for an InjectionToken from a function with 3 dependencies

func MakeProvider4 added in v1.0.69

func MakeProvider4[T1, T2, T3, T4 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	f func(T1, T2, T3, T4) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider4 creates a [DIE.Provider] for an InjectionToken from a function with 4 dependencies

func MakeProvider5 added in v1.0.82

func MakeProvider5[T1, T2, T3, T4, T5 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	f func(T1, T2, T3, T4, T5) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider5 creates a [DIE.Provider] for an InjectionToken from a function with 5 dependencies

func MakeProvider6 added in v1.0.82

func MakeProvider6[T1, T2, T3, T4, T5, T6 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	f func(T1, T2, T3, T4, T5, T6) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider6 creates a [DIE.Provider] for an InjectionToken from a function with 6 dependencies

func MakeProvider7 added in v1.0.82

func MakeProvider7[T1, T2, T3, T4, T5, T6, T7 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	f func(T1, T2, T3, T4, T5, T6, T7) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider7 creates a [DIE.Provider] for an InjectionToken from a function with 7 dependencies

func MakeProvider8 added in v1.0.82

func MakeProvider8[T1, T2, T3, T4, T5, T6, T7, T8 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	f func(T1, T2, T3, T4, T5, T6, T7, T8) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider8 creates a [DIE.Provider] for an InjectionToken from a function with 8 dependencies

func MakeProvider9 added in v1.0.82

func MakeProvider9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any, R any](
	token InjectionToken[R],
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	d9 Dependency[T9],
	f func(T1, T2, T3, T4, T5, T6, T7, T8, T9) IOE.IOEither[error, R],
) DIE.Provider

MakeProvider9 creates a [DIE.Provider] for an InjectionToken from a function with 9 dependencies

func MakeProviderFactory0

func MakeProviderFactory0[R any](
	fct IOE.IOEither[error, R],
) DIE.ProviderFactory

func MakeProviderFactory1

func MakeProviderFactory1[T1 any, R any](
	d1 Dependency[T1],
	f func(T1) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory1 creates a [DIE.ProviderFactory] from a function with 1 arguments and 1 dependencies

func MakeProviderFactory10 added in v1.0.82

func MakeProviderFactory10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	d9 Dependency[T9],
	d10 Dependency[T10],
	f func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory10 creates a [DIE.ProviderFactory] from a function with 10 arguments and 10 dependencies

func MakeProviderFactory2

func MakeProviderFactory2[T1, T2 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	f func(T1, T2) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory2 creates a [DIE.ProviderFactory] from a function with 2 arguments and 2 dependencies

func MakeProviderFactory3 added in v1.0.69

func MakeProviderFactory3[T1, T2, T3 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	f func(T1, T2, T3) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory3 creates a [DIE.ProviderFactory] from a function with 3 arguments and 3 dependencies

func MakeProviderFactory4 added in v1.0.69

func MakeProviderFactory4[T1, T2, T3, T4 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	f func(T1, T2, T3, T4) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory4 creates a [DIE.ProviderFactory] from a function with 4 arguments and 4 dependencies

func MakeProviderFactory5 added in v1.0.82

func MakeProviderFactory5[T1, T2, T3, T4, T5 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	f func(T1, T2, T3, T4, T5) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory5 creates a [DIE.ProviderFactory] from a function with 5 arguments and 5 dependencies

func MakeProviderFactory6 added in v1.0.82

func MakeProviderFactory6[T1, T2, T3, T4, T5, T6 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	f func(T1, T2, T3, T4, T5, T6) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory6 creates a [DIE.ProviderFactory] from a function with 6 arguments and 6 dependencies

func MakeProviderFactory7 added in v1.0.82

func MakeProviderFactory7[T1, T2, T3, T4, T5, T6, T7 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	f func(T1, T2, T3, T4, T5, T6, T7) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory7 creates a [DIE.ProviderFactory] from a function with 7 arguments and 7 dependencies

func MakeProviderFactory8 added in v1.0.82

func MakeProviderFactory8[T1, T2, T3, T4, T5, T6, T7, T8 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	f func(T1, T2, T3, T4, T5, T6, T7, T8) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory8 creates a [DIE.ProviderFactory] from a function with 8 arguments and 8 dependencies

func MakeProviderFactory9 added in v1.0.82

func MakeProviderFactory9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any, R any](
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	d9 Dependency[T9],
	f func(T1, T2, T3, T4, T5, T6, T7, T8, T9) IOE.IOEither[error, R],
) DIE.ProviderFactory

MakeProviderFactory9 creates a [DIE.ProviderFactory] from a function with 9 arguments and 9 dependencies

func Resolve

Resolve performs a type safe resolution of a dependency

Types

type Dependency

type Dependency[T any] interface {
	DIE.Dependency
	// Unerase converts a value with erased type signature into a strongly typed value
	Unerase(val any) E.Either[error, T]
}

Dependency describes the relationship to a service, that has a type and a behaviour such as required, option or lazy

type InjectionToken

type InjectionToken[T any] interface {
	Dependency[T]
	// Identity idenifies this dependency as a mandatory, required dependency, it will be resolved eagerly and injected as `T`.
	// If the dependency cannot be resolved, the resolution process fails
	Identity() Dependency[T]
	// Option identifies this dependency as optional, it will be resolved eagerly and injected as [O.Option[T]].
	// If the dependency cannot be resolved, the resolution process continues and the dependency is represented as [O.None[T]]
	Option() Dependency[O.Option[T]]
	// IOEither identifies this dependency as mandatory but it will be resolved lazily as a [IOE.IOEither[error, T]]. This
	// value is memoized to make sure the dependency is a singleton.
	// If the dependency cannot be resolved, the resolution process fails
	IOEither() Dependency[IOE.IOEither[error, T]]
	// IOOption identifies this dependency as optional but it will be resolved lazily as a [IOO.IOOption[T]]. This
	// value is memoized to make sure the dependency is a singleton.
	// If the dependency cannot be resolved, the resolution process continues and the dependency is represented as the none value.
	IOOption() Dependency[IOO.IOOption[T]]
}

InjectionToken uniquely identifies a dependency by giving it an Id, Type and name

func MakeToken

func MakeToken[T any](name string) InjectionToken[T]

MakeToken create a unique InjectionToken for a specific type

func MakeTokenWithDefault

func MakeTokenWithDefault[T any](name string, providerFactory DIE.ProviderFactory) InjectionToken[T]

MakeToken create a unique InjectionToken for a specific type

func MakeTokenWithDefault0

func MakeTokenWithDefault0[R any](name string, fct IOE.IOEither[error, R]) InjectionToken[R]

MakeTokenWithDefault0 creates a unique InjectionToken for a specific type with an attached default [DIE.Provider]

func MakeTokenWithDefault1

func MakeTokenWithDefault1[T1 any, R any](
	name string,
	d1 Dependency[T1],
	f func(T1) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault1 creates an InjectionToken with a default implementation with 1 dependenciess

func MakeTokenWithDefault10 added in v1.0.82

func MakeTokenWithDefault10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	d9 Dependency[T9],
	d10 Dependency[T10],
	f func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault10 creates an InjectionToken with a default implementation with 10 dependenciess

func MakeTokenWithDefault2

func MakeTokenWithDefault2[T1, T2 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	f func(T1, T2) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault2 creates an InjectionToken with a default implementation with 2 dependenciess

func MakeTokenWithDefault3 added in v1.0.69

func MakeTokenWithDefault3[T1, T2, T3 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	f func(T1, T2, T3) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault3 creates an InjectionToken with a default implementation with 3 dependenciess

func MakeTokenWithDefault4 added in v1.0.69

func MakeTokenWithDefault4[T1, T2, T3, T4 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	f func(T1, T2, T3, T4) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault4 creates an InjectionToken with a default implementation with 4 dependenciess

func MakeTokenWithDefault5 added in v1.0.82

func MakeTokenWithDefault5[T1, T2, T3, T4, T5 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	f func(T1, T2, T3, T4, T5) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault5 creates an InjectionToken with a default implementation with 5 dependenciess

func MakeTokenWithDefault6 added in v1.0.82

func MakeTokenWithDefault6[T1, T2, T3, T4, T5, T6 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	f func(T1, T2, T3, T4, T5, T6) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault6 creates an InjectionToken with a default implementation with 6 dependenciess

func MakeTokenWithDefault7 added in v1.0.82

func MakeTokenWithDefault7[T1, T2, T3, T4, T5, T6, T7 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	f func(T1, T2, T3, T4, T5, T6, T7) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault7 creates an InjectionToken with a default implementation with 7 dependenciess

func MakeTokenWithDefault8 added in v1.0.82

func MakeTokenWithDefault8[T1, T2, T3, T4, T5, T6, T7, T8 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	f func(T1, T2, T3, T4, T5, T6, T7, T8) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault8 creates an InjectionToken with a default implementation with 8 dependenciess

func MakeTokenWithDefault9 added in v1.0.82

func MakeTokenWithDefault9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any, R any](
	name string,
	d1 Dependency[T1],
	d2 Dependency[T2],
	d3 Dependency[T3],
	d4 Dependency[T4],
	d5 Dependency[T5],
	d6 Dependency[T6],
	d7 Dependency[T7],
	d8 Dependency[T8],
	d9 Dependency[T9],
	f func(T1, T2, T3, T4, T5, T6, T7, T8, T9) IOE.IOEither[error, R],
) InjectionToken[R]

MakeTokenWithDefault9 creates an InjectionToken with a default implementation with 9 dependenciess

type MultiInjectionToken

type MultiInjectionToken[T any] interface {
	// Container returns the injection token used to request an array of all provided items
	Container() InjectionToken[[]T]
	// Item returns the injection token used to provide an item
	Item() InjectionToken[T]
}

MultiInjectionToken uniquely identifies a dependency by giving it an Id, Type and name that can have multiple implementations. Implementations are provided via the [MultiInjectionToken.Item] injection token.

func MakeMultiToken

func MakeMultiToken[T any](name string) MultiInjectionToken[T]

MakeMultiToken creates a MultiInjectionToken

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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