Documentation ¶
Index ¶
- func DefNameIsAllowed(name string) error
- func FormatDefName(name string) string
- func FormatPkgName(name string) string
- func GenerateContainer(provider Provider, outputDirectory string) error
- func GenerateContainerWithCustomPkgName(provider Provider, outputDirectory, pkgName string) error
- type AutoFill
- type BaseProvider
- func (p *BaseProvider) Add(i interface{}) error
- func (p *BaseProvider) AddDef(def Def) error
- func (p *BaseProvider) AddDefPtr(def *Def) error
- func (p *BaseProvider) AddDefPtrSlice(defs []*Def) error
- func (p *BaseProvider) AddDefSlice(defs []Def) error
- func (p *BaseProvider) Get(name string) (*Def, error)
- func (p *BaseProvider) Load() error
- func (p *BaseProvider) Names() []string
- type ContainerKey
- type Def
- type ParamInfo
- type ParamScanner
- type Params
- type Provider
- type Scan
- type ScannedDef
- func (def *ScannedDef) BuildDependsOnRawDef() bool
- func (def *ScannedDef) GenerateComment() string
- func (def *ScannedDef) GenerateCommentDescription() string
- func (def *ScannedDef) GenerateCommentParams() string
- func (def *ScannedDef) GenerateCommentScope() string
- func (def *ScannedDef) ParamsString() string
- type Scanner
- type Service
- type TypeManager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefNameIsAllowed ¶
DefNameIsAllowed returns an error if the definition name is not allowed.
func FormatDefName ¶
FormatDefName is the function used to turn the definition name into something that can be used in the generated container.
func FormatPkgName ¶
FormatPkgName formats a package name by keeping only the letters.
func GenerateContainer ¶
GenerateContainer generates a depedency injection container. The definitions are loaded from the Provider. The code is generated in the outputDirectory.
func GenerateContainerWithCustomPkgName ¶ added in v4.1.0
GenerateContainerWithCustomPkgName works like GenerateContainer but let you customize the package name in which files are generated. The package name must be a valid package name or the generation may fail unexpectedly.
Types ¶
type AutoFill ¶
type AutoFill bool
AutoFill can be used as Params value to avoid autofill (default is Autofill(true)). If a structure field is not in the Params map, the container will try to use a service from the container that has the same type. Setting the entry to AutoFill(false) will let the field empty in the structure.
type BaseProvider ¶
type BaseProvider struct {
// contains filtered or unexported fields
}
BaseProvider implements the Provider interface. It contains no definition, but you can use this to create your own Provider by redefining the Load method.
func (*BaseProvider) Add ¶
func (p *BaseProvider) Add(i interface{}) error
Add adds definitions inside the Provider. Allowed types are: dingo.Def, *dingo.Def, []dingo.Def, []*dingo.Def func() dingo.Def, func() *dingo.Def, func() []dingo.Def, func() []*dingo.Def
func (*BaseProvider) AddDef ¶
func (p *BaseProvider) AddDef(def Def) error
AddDef is the same as Add, but only for Def.
func (*BaseProvider) AddDefPtr ¶
func (p *BaseProvider) AddDefPtr(def *Def) error
AddDefPtr is the same as Add, but only for *Def.
func (*BaseProvider) AddDefPtrSlice ¶
func (p *BaseProvider) AddDefPtrSlice(defs []*Def) error
AddDefPtrSlice is the same as Add, but only for []*Def.
func (*BaseProvider) AddDefSlice ¶
func (p *BaseProvider) AddDefSlice(defs []Def) error
AddDefSlice is the same as Add, but only for []Def.
func (*BaseProvider) Get ¶
func (p *BaseProvider) Get(name string) (*Def, error)
Get returns the definition for a given service. If the definition does not exist, an error is returned.
func (*BaseProvider) Load ¶
func (p *BaseProvider) Load() error
Load registers the service definitions. You need to override this method to add the service definitions. You can use the Add method to add a definition inside the provider.
func (*BaseProvider) Names ¶
func (p *BaseProvider) Names() []string
Names returns the names of the definitions. The names are sorted by alphabetical order.
type ContainerKey ¶
type ContainerKey string
ContainerKey is a type that can be used as key in a context.Context. For example it can be use if you want to store a container in the Context of an http.Request. It is used in the generated C function.
type Def ¶
type Def struct { // Name is the key that is used to retrieve the object from the container. Name string // Scope determines in which container the object is stored. // Typical scopes are "app" and "request". Scope string // NotForAutoFill should be set to true if you // do not want to use this service automatically // as a dependency in other services. NotForAutoFill bool // Build defines the service constructor. It can be either: // - a pointer to a structure: (*MyStruct)(nil) // - a factory function: func(any, any, ...) (any, error) Build interface{} // Params are used to assist the service constructor. Params Params // Close should be a function: func(any) error. // With any being the type of the service. Close interface{} // They are singleton and the same instance will be returned each time "Get", "SafeGet" or "Fill" is called. // If you want to retrieve a new object every time, "Unshared" needs to be set to true. Unshared bool // Description is a text that describes the service. // If provided, the description is used in the comments of the generated code. Description string }
Def is the structure containing a service definition.
type ParamInfo ¶
type ParamInfo struct { Name string Index string ServiceName string Type reflect.Type TypeString string UndefinedStructParam bool Def *ScannedDef }
ParamInfo contains the parsed information about a parameter.
type ParamScanner ¶
type ParamScanner struct {
// contains filtered or unexported fields
}
ParamScanner helps the Scanner. It scans information about params.
func (*ParamScanner) Scan ¶
func (s *ParamScanner) Scan(scan *Scan) error
Scan updates the given Scan with data about params.
type Params ¶
type Params map[string]interface{}
Params are used to assist the service constructor. If the Def.Build field is a pointer to a structure, the keys of the map should be among the names of the structure fields. These fields will be filled with the associated value in the map. If the Def.Build field is a function, it works the same way. But the key of the map should be the index of the function parameters (e.g.: "0", "1", ...).
key=fieldName¦paramIndex value=any¦dingo.Service|dingo.AutoFill
func NewFuncParams ¶ added in v4.2.0
func NewFuncParams(params ...interface{}) Params
NewFuncParams creates a Params instance where the key of the map, is the index of the given parameter. It is usefull when using a Build function, to provide all the function parameters. e.g.: NewFuncParams("a", "b", "c") return Params{"0": "a", "1": "b", "2": "c"}
type Provider ¶
Provider is the interface used to store the definitions. The provider is used while generating the dependency injection container, but also while executing the code of the container.
type Scan ¶
type Scan struct { TypeManager *TypeManager ImportsWithoutParams map[string]string Defs []*ScannedDef ProviderPackage string ProviderName string }
Scan contains the parsed information about the service definitions.
type ScannedDef ¶
type ScannedDef struct { Def *Def Name string FormattedName string Scope string ObjectType reflect.Type ObjectTypeString string BuildIsFunc bool BuildTypeString string Params map[string]*ParamInfo CloseTypeString string }
ScannedDef contains the parsed information about a service definition.
func (*ScannedDef) BuildDependsOnRawDef ¶
func (def *ScannedDef) BuildDependsOnRawDef() bool
BuildDependsOnRawDef returns true if the service constructor needs the definition contained in the Provider.
func (*ScannedDef) GenerateComment ¶ added in v4.1.1
func (def *ScannedDef) GenerateComment() string
GenerateComment returns the text used in the comments of the generated code.
func (*ScannedDef) GenerateCommentDescription ¶ added in v4.1.1
func (def *ScannedDef) GenerateCommentDescription() string
GenerateCommentDescription returns the description as it should be printed in the generated comments.
func (*ScannedDef) GenerateCommentParams ¶ added in v4.1.1
func (def *ScannedDef) GenerateCommentParams() string
GenerateCommentParams returns the params as they should be printed in the generated comments.
func (*ScannedDef) GenerateCommentScope ¶ added in v4.1.1
func (def *ScannedDef) GenerateCommentScope() string
GenerateCommentScope returns the scope as it should be printed in the generated comments.
func (*ScannedDef) ParamsString ¶
func (def *ScannedDef) ParamsString() string
ParamsString returns the parameters as they should appear in a structure inside a go file.
type Scanner ¶
type Scanner struct { Provider Provider ParamScanner ParamScanner // contains filtered or unexported fields }
Scanner analyzes the definitions provided by a Provider.
type Service ¶
type Service string
Service can be used as Params value. It means that the field (or parameter) should be replaced by an other service. This service should be retrieved from the container.
type TypeManager ¶
type TypeManager struct {
// contains filtered or unexported fields
}
TypeManager maintains a list of all the import paths that are used in the types that it has registered. It associates a unique alias to all the import paths.
func (*TypeManager) Imports ¶
func (tm *TypeManager) Imports() map[string]string
Imports returns a map with all the imports that are used in the registered types. The key is the import path and the value is the alias that has been given by the TypeManager.