embark

package module
v0.0.0-...-4ee2d5f Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: MIT Imports: 5 Imported by: 0

README

Embark

Embark is a lightweight dependency injection package for Go, designed to simplify the management of dependencies in your Go applications.

Features

  • Simple API: Embark offers a straightforward API that makes it easy to integrate with your Go projects.
  • Lightweight: With minimal overhead, Embark doesn't bog down your application.
  • Flexible: Supports various use cases from simple to complex dependency injection scenarios.

Getting Started

Prerequisites
  • Go 1.22.0 or later
Installation

To start using Embark, install it using go get github.com/bnprtr/embark:

Example

For more examples, find the examples folder.

type Service struct {
 log *slog.Logger
}

func (s *Service) OnInit(ctx context.Context, container *embark.Container) error {
 // query for the logger after the container is initialized
 log, found := embark.NewQuery[*slog.Logger](container).First()
 if !found {
  return fmt.Errorf("logger not found")
 }
 s.log = log
 return nil
}

func (s *Service) Hello() {
 s.log.Info("hello world!")
}

func main() {
 service := Service{}
 // provide the service and slog's default logger for dependency injection
 embark.Provide(&service)
 embark.Provide(slog.Default())
 embark.Done(context.Background())
 service.Hello()
}

Documentation

Overview

Package embark provides a lightweight dependency injection framework for Go applications. It simplifies managing dependencies, offering a straightforward API and flexibility for various use cases from simple to complex scenarios.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Provide

func Provide[T any](component T) T

Provide registers an instance for dependency injection. It accepts any type and registers it within the internal container.

Types

type Container

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

Container holds the dependency injection container's state and provides methods to register and resolve dependencies.

func Done

func Done(ctx context.Context) (*Container, error)

Done is expected to be called after registration of dependency injected components during program startup. OnInit and OnPostInit callbacks of registered components will be invoked as a result. Once the Init and PostInit phase is completed, Done will return with any resulting errors.

func (*Container) HealthCheck

func (c *Container) HealthCheck() error

func (*Container) Shutdown

func (c *Container) Shutdown(ctx context.Context) error

type Filter

type Filter[T any] func(component T) bool

Filter can be provided in query functions to filter out multiple components that match the type query.

type HealthCheckHandler

type HealthCheckHandler interface {
	HealthCheck() error
}

type OnInitHandler

type OnInitHandler interface {
	OnInit(ctx context.Context, container *Container) error
}

type PostInitHandler

type PostInitHandler interface {
	OnPostInit(ctx context.Context, container *Container) error
}

type Query

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

Query is used to query the Container for components.

func NewQuery

func NewQuery[T any](container *Container) *Query[T]

NewQuery creates a new Query instance for the container

func (Query[T]) All

func (qb Query[T]) All(filters ...Filter[T]) []T

All returns all registered components that match the query.

func (Query[T]) First

func (qb Query[T]) First(filters ...Filter[T]) (T, bool)

First returns the first component found that matches the query type and filters. returns the entry and a boolean representing if a match was found.

func (Query[T]) Must

func (qb Query[T]) Must(filters ...Filter[T]) T

Must finds one and only one matching component. If more than no results or more than one matching component is found the function will panic.

type ShutdownFunc

type ShutdownFunc func(ctx context.Context) error

type ShutdownHandler

type ShutdownHandler interface {
	Shutdown(ctx context.Context) error
}

Directories

Path Synopsis
examples
basic command
lifecycle command
query-all command
query-filters command
simple-service command
values command

Jump to

Keyboard shortcuts

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