deps

package module
v0.0.0-...-f3640bc Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

deps

deps is a dependency injection framework for go programs (golang). It was inspired by (copied from) Service Weaver, but more simple.

Basic Usage

package main

import (
	"context"
	"fmt"

	"github.com/cgfork/deps"
)

type Foo interface {
	Display()
}

type fooConfig struct {
	Name string
}

type foo struct {
	deps.Implements[Foo]
	deps.WithConfig[fooConfig]
}

func (f *foo) Display() {
	fmt.Println("foo", f.Config().Name)
}

type fooA struct {
	deps.Implements[Foo]       `impl:"fooA"`
	deps.WithConfig[fooConfig] `section:"fooA"`
}

func (f *fooA) Display() {
	fmt.Println("fooA", f.Config().Name)
}

type fooB struct {
	deps.Implements[Foo]       `impl:"fooB"`
	deps.WithConfig[fooConfig] // Default to use fooB section
}

func (f *fooB) Display() {
	fmt.Println("fooB", f.Config().Name)
}

type app struct {
	deps.Implements[deps.System]

	foo  deps.Ref[Foo] // Anonymouse reference
	fooA deps.Ref[Foo] `ref:"fooA"`
	fooB deps.Ref[Foo] `ref:"fooB"`
}

func main() {
	deps.MustProvide[deps.System, app]()
	deps.MustProvide[Foo, foo]()
	deps.MustProvide[Foo, fooA]()
	deps.MustProvide[Foo, fooB]()
	var config = `
	[fooB]
	name = "xyz"

	[fooA]
	name = "abc"

	['main.Foo'] # full-packaged name for iface type
	name = "foo"
	`
	if err := deps.Run[app](context.Background(), deps.Config{
		Config: config,
	}, func(ctx context.Context, app *app) error {
		app.foo.Get().Display()
		app.fooA.Get().Display()
		app.fooB.Get().Display()
		return nil
	}); err != nil {
		panic(err)
	}
}

Run go run main.go, you will get:

foo foo
fooA abc
fooB xyz

Documentation

Index

Constants

View Source
const PkgPath = "github.com/cgfork/deps"

Variables

This section is empty.

Functions

func GetConfig

func GetConfig(impl any) any

GetConfig returns the config stored in the provided implementation, or returns nil if the implementation does not have a config.

func GetImpl

func GetImpl[T any](gr getRuntime) (*T, error)

GetImpl returns the object instance of the implementation T.

func GetIntf

func GetIntf[T any](gr getRuntime, name string) (T, error)

GetIntf returns the implementation of the interface T and with the specified name.

func HasConfig

func HasConfig(impl any) bool

HasConfig returns true if the provided implementation has an embeded deps.WithConfig field.

func IfaceType

func IfaceType[T any]() (reflect.Type, error)

IfaceType returns the reflect.Type for the interface.

func MustProvide

func MustProvide[Iface any, Impl any](opts ...Option)

func ParseTOML

func ParseTOML(input string) (map[string]string, error)

ParseTOML parses the provided TOML input and returns a map of sections.

func Provide

func Provide[Iface any, Impl any](opts ...Option) error

func Run

func Run[T any, P PointerToSystem[T]](ctx context.Context, config Config, start func(context.Context, *T) error) error

Run starts a deps system.

func StructType

func StructType[T any]() (reflect.Type, error)

StructType returns the reflect.Type for the struct.

func Type

func Type[T any]() reflect.Type

Type returns the reflect.Type for T.

This function is particularly useful when T is an interface and it is impossible to get a value with concrete type T.

func ValidateDeps

func ValidateDeps(deps []*Dep) error

ValidateDeps validates the given registrations. It makes sure that every type which is refered by the runtime.Ref field of the impl type has been registered.

Types

type Config

type Config struct {
	Config  string
	Fakes   map[reflect.Type]any
	Present map[string]any
	Root    *slog.Logger
}

type Dep

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

Dep represents the relationship between an interface and its implementation

func NewDep

func NewDep[Iface any, Impl any](opts ...Option) (Dep, error)

func Registered

func Registered() []*Dep

Registered returns the types registered with Register.

func Search(typ reflect.Type) ([]*Dep, bool)

Search returns the registrations of dependencies that implement the given type.

type Implements

type Implements[T any] struct {
	// contains filtered or unexported fields
}

/ Implements is a struct that can be embedded in an implementation.

type InstanceOf

type InstanceOf[T any] interface {
	// contains filtered or unexported methods
}

InstanceOf[T] is the interface implemented by a struct that embeds deps.Implements[T].

type Option

type Option func(*Dep)

Option is used to setup the Dep.

func WithHook

func WithHook(hook func(impl any, caller string) any) Option

WithHook sets the hook function for the Dep.

type PointerToSystem

type PointerToSystem[T any] interface {
	*T

	// Ensure that the T type embeds Implements[System].
	InstanceOf[System]
}

PointerToSystem is a type constraint that asserts *T is an instance of System.

type Ref

type Ref[T any] struct {
	// contains filtered or unexported fields
}

Ref[T] is a field that can be placed inside an implementation struct. T must be a registered type. Runtime will automatically wire such a field with a handle to the corresponding implementation.

func (Ref[T]) Get

func (r Ref[T]) Get() T

Get returns a handle to implementation of type T.

type Runtime

type Runtime interface {
	// GetImpl returns the instance of the given type.
	GetImpl(reflect.Type) (any, error)
	// GetIntf returns the implementation instance of the given interface Type
	// with the given name.
	GetIntf(reflect.Type, string) (any, error)
}

Runtime is the interface for the deps runtime which provides the way to resolve the references or implementations.

func NewRuntime

func NewRuntime(ctx context.Context, regs []*Dep, config Config) (Runtime, error)

NewRuntime returns a new Runtime.

type System

type System interface {
}

System is the interface implemented by a deps system which is started by the `RunSystem` function.

type WithConfig

type WithConfig[T any] struct {
	// contains filtered or unexported fields
}

WithConfig[T] is a type that can be embedded inside a implementation struct. implementation. The runtime will take per-construct configuration information found in the application config file and use it to initialize the contents of T.

func (*WithConfig[T]) Config

func (wc *WithConfig[T]) Config() *T

Config returns the configuration information for the implementation that embeds this deps.WithConfig.

Any fields in T that were not present in the application config file will have their default values.

Any fields in the application config file that are not present in T will be flagged as an error at application startup.

type WithLog

type WithLog struct {
	Log *slog.Logger
}

WithLog is a type that can be embedded in a implementation struct to provide logging capabilities. It will only be used if the implementation struct also embeds Implements[T].

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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