depkit

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 4 Imported by: 0

README ΒΆ

Go GitHub go.mod Go version Go Report Card codecov License Github tag

Overview

This package provides a simple way to manage dependencies in a Go application. It allows you to register and retrieve dependencies using Go interfaces.

It simplifies the process of writing unit tests by providing a simple and easy-to-use interface for managing dependencies between different components of your application. It allows you to register dependencies and callbacks, and retrieve them whenever needed, making it easy to test your code in isolation. This results in more maintainable and reliable tests, as well as a faster development process.

Installation

To install this package, use the go get command:

go get github.com/lab210-dev/depkit

πŸ“š Usage

To use this package, you must first register your dependency using the Register function :

package example

import "github.com/lab210-dev/depkit"

type MyService interface {
	DoSomething()
}

type myServiceImpl struct{}

func (s *myServiceImpl) DoSomething() {
	// Do something here...
}

func init() {
	depkit.Register[MyService](new(myServiceImpl))
}

You can now retrieve your service using the Get function :

package example

import "github.com/lab210-dev/depkit"

type MyService interface {
	DoSomething()
}

func main() {
	depkit.Get[MyService]().DoSomething()
}

You can also use the GetAfterRegister function to execute a callback once the service has been registered :

package example

import "github.com/lab210-dev/depkit"

type MyService interface {
	DoSomething()
}

func main() {
    depkit.GetAfterRegister[MyService](func(s MyService) {
        s.DoSomething()
    })
}

To reset all registered dependencies, use the Reset function :

package example

import "github.com/lab210-dev/depkit"

func main() {
	depkit.Reset()
}
Notes
  • Services must be registered using interfaces or func, not concrete types.
  • If you try to retrieve a dependency that has not been registered, an error will panic.

⚑️ Benchmark

goos: darwin
goarch: arm64
pkg: github.com/lab210-dev/depkit
BenchmarkGet
BenchmarkGet-10                 	 6689894	       167.8 ns/op
BenchmarkGetAfterRegister
BenchmarkGetAfterRegister-10    	 5415369	       223.7 ns/op

🀝 Contributions

Contributors to the package are encouraged to help improve the code.

If you have any questions or comments, don't hesitate to contact me.

I hope this package is helpful to you !

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Get ΒΆ

func Get[T any]() (module T)

Get returns the registered service associated with the given interface.

func GetAfterRegister ΒΆ

func GetAfterRegister[T any](fn func(module T))

GetAfterRegister is a special case for the init function, the callback is triggered after the service is registered. If the service has already been registered for the given interface, the callback is executed with the registered service as a parameter. If the service has not yet been registered, the callback is added to the list of registered callbacks for this interface.

func Register ΒΆ

func Register[T any](module T)

Register a service associated with the given interface. If a service is already registered for this interface, the function does nothing. If callbacks have been registered for this interface, they are executed with the registered service as a parameter.

func Reset ΒΆ

func Reset()

Reset resets all registered services by creating new empty maps for the services and callbacks.

Types ΒΆ

type Service ΒΆ

type Service struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*Service) GetCallbacksByIdentifier ΒΆ

func (s *Service) GetCallbacksByIdentifier(identifier string) []any

GetCallbacksByIdentifier : This function returns the list of registered callbacks associated with the given identifier. If no callback is associated with this identifier, the function returns an empty list

func (*Service) GetServiceByIdentifier ΒΆ

func (s *Service) GetServiceByIdentifier(identifier string) any

GetServiceByIdentifier : This function returns the registered service associated with the given identifier. If no service is associated with this identifier, the function returns nil.

func (*Service) SetCallbacksByIdentifier ΒΆ

func (s *Service) SetCallbacksByIdentifier(identifier string, fn any)

SetCallbacksByIdentifier : This function adds a callback to the list of registered callbacks associated with the given identifier.

func (*Service) SetServiceByIdentifier ΒΆ

func (s *Service) SetServiceByIdentifier(identifier string, module any)

SetServiceByIdentifier : This function registers a service associated with the given identifier.

Jump to

Keyboard shortcuts

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