(c *container.C, globals map[string]interface{}, nodes []config.Node)
| 528 | } |
| 529 | |
| 530 | func RegisterModules(c *container.C, globals map[string]interface{}, nodes []config.Node) (err error) { |
| 531 | var endpoints []struct { |
| 532 | Endpoint container.LifetimeModule |
| 533 | Cfg *config.Map |
| 534 | } |
| 535 | |
| 536 | for _, block := range nodes { |
| 537 | var instName string |
| 538 | var modAliases []string |
| 539 | if len(block.Args) == 0 { |
| 540 | instName = block.Name |
| 541 | } else { |
| 542 | instName = block.Args[0] |
| 543 | modAliases = block.Args[1:] |
| 544 | } |
| 545 | |
| 546 | modName := block.Name |
| 547 | |
| 548 | endpFactory := modules.GetEndpoint(modName) |
| 549 | if endpFactory != nil { |
| 550 | inst, err := endpFactory(c, modName, block.Args) |
| 551 | if err != nil { |
| 552 | return err |
| 553 | } |
| 554 | |
| 555 | endpoints = append(endpoints, struct { |
| 556 | Endpoint container.LifetimeModule |
| 557 | Cfg *config.Map |
| 558 | }{Endpoint: inst, Cfg: config.NewMap(globals, block)}) |
| 559 | continue |
| 560 | } |
| 561 | |
| 562 | factory := modules.Get(modName) |
| 563 | if factory == nil { |
| 564 | return config.NodeErr(block, "unknown module or global directive: %s", modName) |
| 565 | } |
| 566 | |
| 567 | inst, err := factory(c, modName, instName) |
| 568 | if err != nil { |
| 569 | return err |
| 570 | } |
| 571 | |
| 572 | err = c.Modules.Register(inst, func() error { |
| 573 | err := inst.Configure(nil, config.NewMap(globals, block)) |
| 574 | if err != nil { |
| 575 | return err |
| 576 | } |
| 577 | |
| 578 | if lt, ok := inst.(container.LifetimeModule); ok { |
| 579 | c.Lifetime.Add(lt) |
| 580 | } |
| 581 | return nil |
| 582 | }) |
| 583 | if err != nil { |
| 584 | if errors.Is(err, container.ErrInstanceNameDuplicate) { |
| 585 | return config.NodeErr(block, "config block named %s already exists", inst.InstanceName()) |
| 586 | } |
| 587 | return err |
no test coverage detected