Documentation ¶
Overview ¶
Package shaft is an alternative dependency injection framework of golang. It is mainly inspired by go.uber.org/fx and go.uber.org/dig, but comes with support for deferring (to close created resources), and a more simplified semantics.
Just like the fx and dig conterparts, the shaft interpret dependency injection relationship by matching types. However, we apply these rules to simplify when we scan and transform provided functions:
- If the parameter is slice kind `[]T`, we interpret it as providing or consuming a group of `T`, which I find it more intuitive. To provide and consume a slice instead, it is recommended to define something like `type TSlice []T`.
- If a parameter is consumed and provided, we interpret it as a decorated paramter. Which means if the provided function isn't providing any other type, it will be called only after someone providing this type.
- Because you can assign a name to type easily by defining `type Name T`, and we would like to keep it as simple as possible, we don't provide naming support here.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
Option is just a simple forwarding of core.Option.
func Invoke ¶
func Invoke(f interface{}) Option
Invoke a function as consumer.
The provided f must be a function, objects required by the function is present in the argument list. The result of this function is ignored, except for the last result being an error, and the error is returned then.
func Populate ¶
func Populate(objs ...interface{}) Option
Populate objects from the dependency injection.
func Provide ¶
func Provide(f interface{}) Option
Provide a function as constructor.
The provided f must be a function, objects required by the function is present in the argument list, and the objects created by the function is in the result. And the function can return an error as last result optionally.
func Stack ¶
func Stack(f interface{}) Option
Stack a function as constructor.
The provided f must be a function, its first argument must be a function pointer accepting results generated by this function, which is a callback provided by the framework. And the remainder of the arguments must be the objects required by this function. The inner function pointer must return an error, and so do the function, as there could always be some module returning error.
func Supply ¶
func Supply(obj interface{}, infcs ...interface{}) Option
Supply an objects to dependency injection.
The infcs specifies what type would you like the object to be, it might be either be pointer type or slice type, corresponding to the case you want to specify a single object or a group object. Specifying infcs is mandatory when you want to provide the object as instance implementing an interface, or you will lose the type information when you pass the object as parameter.
You might also specify the interface types of this object when supplying, otherwise the actual underlying object will have been supplied to them.
Directories ¶
Path | Synopsis |
---|---|
Package core is the core and generic part of the dependency injection framework.
|
Package core is the core and generic part of the dependency injection framework. |
Package serpent provides a way to combine the shaft framework with github.com/spf13/cobra nicely, providing a dependency injection style monolithc CLI interface.
|
Package serpent provides a way to combine the shaft framework with github.com/spf13/cobra nicely, providing a dependency injection style monolithc CLI interface. |