ioc

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2024 License: MIT Imports: 15 Imported by: 0

README

IOC

The IOC library is a dependency injection and management tool implemented in Golang.

Features:

  • Dependency Injection: Simplifies object creation and management using the ioc library.
  • Named Providers: Uses named providers to create and manage the same type of object.
  • Health Checks: Integrates health check functionality to ensure the health status of system components.

Provider

A Provider is a component responsible for creating instances of a specific type.

package main

import (
	"github.com/aiechoic/services/ioc"
)

type Engine struct{ Name string }

var engineProvider = ioc.NewProvider(func(c *ioc.Container) (*Engine, error) {
	engine := &Engine{}
	return engine, nil
})

Container

A Container is a central registry that stores singleton objects.

func main() {
    c := ioc.NewContainer()
    var engine *Engine = engineProvider.Get(c)
}

more examples can be found in the examples directory.

License

This project is licensed under the MIT License - see the LICENSE file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigEnv

type ConfigEnv string
var (
	ConfigEnvTest ConfigEnv = "test"
	ConfigEnvDev  ConfigEnv = "dev"
	ConfigEnvProd ConfigEnv = "prod"
)

type Container

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

func NewContainer

func NewContainer() *Container

func (*Container) CheckHealth

func (c *Container) CheckHealth(ctx context.Context) []*healthy.Error

func (*Container) Close

func (c *Container) Close() error

func (*Container) CloseWithContext

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

func (*Container) LoadConfig

func (c *Container) LoadConfig(dir string, env ConfigEnv) error

func (*Container) OnClose

func (c *Container) OnClose(closer func() error)

func (*Container) OnHealthCheck

func (c *Container) OnHealthCheck(checker func() *healthy.Error)

func (*Container) RunHealthCheck

func (c *Container) RunHealthCheck(ticker, timeout time.Duration, handler func(errs []*healthy.Error))

func (*Container) UnmarshalAndWatchConfig

func (c *Container) UnmarshalAndWatchConfig(name string, defaultContent []byte, handler func(v *viper.Viper)) error

func (*Container) UnmarshalConfig

func (c *Container) UnmarshalConfig(name string, v any, defaultContent []byte) error

func (*Container) WatchConfig

func (c *Container) WatchConfig(name string, handler func(v *viper.Viper)) error

type MultiError

type MultiError []error

func (MultiError) Error

func (m MultiError) Error() string

type Provider

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

func NewProvider

func NewProvider[T any](new func(c *Container) (T, error)) *Provider[T]

func (*Provider[T]) Get

func (f *Provider[T]) Get(c *Container) (t T, err error)

func (*Provider[T]) GetNew

func (f *Provider[T]) GetNew(c *Container) (T, error)

func (*Provider[T]) IsSet

func (f *Provider[T]) IsSet(c *Container) bool

func (*Provider[T]) MustGet

func (f *Provider[T]) MustGet(c *Container) T

func (*Provider[T]) MustGetNew

func (f *Provider[T]) MustGetNew(c *Container) T

func (*Provider[T]) MustRefresh

func (f *Provider[T]) MustRefresh(c *Container) T

func (*Provider[T]) Refresh

func (f *Provider[T]) Refresh(c *Container) (ins T, err error)

func (*Provider[T]) Set

func (f *Provider[T]) Set(c *Container, ins T)

type Providers

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

func NewProviders

func NewProviders[T any]() *Providers[T]

func (*Providers[T]) GetProvider

func (r *Providers[T]) GetProvider(name string, new func(c *Container) (T, error)) *Provider[T]

type Vipers

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

func NewVipers

func NewVipers(dir string, env ConfigEnv) (*Vipers, error)

func (*Vipers) GetOrCreateViper

func (c *Vipers) GetOrCreateViper(name string, defaultContent []byte) (*viper.Viper, error)

func (*Vipers) Unmarshal

func (c *Vipers) Unmarshal(name string, v any, def []byte) error

func (*Vipers) UnmarshalAndWatch

func (c *Vipers) UnmarshalAndWatch(name string, defaultContent []byte, callback func(v *viper.Viper)) error

func (*Vipers) WatchConfig

func (c *Vipers) WatchConfig(name string, callback func(v *viper.Viper)) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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