Procyon|Docs
ComponentsConditions

Load Conditionally

Load components only when runtime state matches.

Conditions decide whether a registered component should be loaded into the container. They run before the component definition enters the runtime container, so optional components can be excluded before dependency resolution starts.

Use conditions for runtime inclusion rules:

  • enable a component only for a profile
  • load an adapter only when another component exists
  • switch implementations by environment
  • keep optional integrations out of the container
audit.go
func init() {
	component.Register(NewAuditService).
		Conditional(ProductionOnly{})
}

The condition itself implements component.Condition:

condition.go
type Condition interface {
	// Matches returns true when the component should be loaded.
	Matches(ctx ConditionContext) bool
}

Matches is evaluated while component definitions are being loaded. Return true to include the component in the container, or false to skip it for the current runtime state.

For example, a profile-based condition can check a runtime value:

condition.go
type ProductionOnly struct{}

func (ProductionOnly) Matches(ctx component.ConditionContext) bool {
	return ctx.Value("profile") == "production"
}

Flow

  1. Register the component.
  2. Attach one or more conditions with Conditional.
  3. Procyon evaluates the conditions before loading definitions.
  4. The component is added only when every condition matches.

Continue with custom conditions when you need to write a rule, context access when the rule needs runtime state, and attach conditions when you need to apply the rule to registered components.

On this page