Load evaluates the conditions of each component and registers its definition into the container only if all conditions are satisfied. Components that fail condition checks are skipped. Returns an error if any eligible component fails to register.
(ctx context.Context)
| 52 | // only if all conditions are satisfied. Components that fail condition checks are skipped. |
| 53 | // Returns an error if any eligible component fails to register. |
| 54 | func (l *ConditionalLoader) Load(ctx context.Context) error { |
| 55 | skipped := make([]*Component, 0) |
| 56 | |
| 57 | for _, comp := range l.components { |
| 58 | if !l.evaluator.evaluate(ctx, comp.Conditions()) { |
| 59 | skipped = append(skipped, comp) |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | def := comp.definition |
| 64 | if err := l.container.RegisterDefinition(def); err != nil { |
| 65 | return fmt.Errorf("failed to register component %q: %w", def.Name(), err) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if len(skipped) > 0 && len(skipped) < len(l.components) { |
| 70 | loader := NewConditionalLoader(l.container, skipped) |
| 71 | return loader.Load(ctx) |
| 72 | } |
| 73 | |
| 74 | return nil |
| 75 | } |