AddModule adds a module to the core. A Module is a group of functionality. It must provide some runnable stuff: http handlers, grpc handlers, cron jobs, one-time command, etc. Optionally if the module embeds di.In, its fields will be injected by DI container. The semantics of injection follows the
(module interface{})
| 208 | // Note that the module added in this way will not retain any original field |
| 209 | // values, i.e. the module will only contain fields populated by DI container. |
| 210 | func (c *C) AddModule(module interface{}) { |
| 211 | t := reflect.TypeOf(module) |
| 212 | if t.Kind() == reflect.Ptr && dig.IsIn(t.Elem()) { |
| 213 | err := di.IntoPopulator(c.di).Populate(module) |
| 214 | if err != nil { |
| 215 | panic(err) |
| 216 | } |
| 217 | c.Container.AddModule(module) |
| 218 | return |
| 219 | } |
| 220 | if dig.IsIn(t) { |
| 221 | copy := reflect.New(t) |
| 222 | err := di.IntoPopulator(c.di).Populate(copy.Interface()) |
| 223 | if err != nil { |
| 224 | panic(err) |
| 225 | } |
| 226 | c.Container.AddModule(copy.Elem().Interface()) |
| 227 | return |
| 228 | } |
| 229 | c.Container.AddModule(module) |
| 230 | } |
| 231 | |
| 232 | // Provide adds dependencies provider to the core. Note the dependency provider |
| 233 | // must be a function in the form of: |