Procyon|Docs
ComponentsConditions

Custom Conditions

Implement component.Condition for your own runtime rule.

A custom condition is a small type that implements component.Condition.

condition.go
type Condition interface {
	// Matches decides whether the registered component belongs in this runtime.
	Matches(ctx ConditionContext) bool
}

The contract has one job: answer whether the component should be included for the current runtime state. The implementation can be any small Go type:

profile_condition.go
type ProfileCondition struct {
	Profile string
}

func (c ProfileCondition) Matches(ctx component.ConditionContext) bool {
	return ctx.Value("profile") == c.Profile
}

Matches returns true when the component should be loaded. Return false when the component should be skipped.

The method should only inspect runtime state. It should not create components, open connections, mutate configuration, or perform business work.

Parameterize the rule

Use fields on the condition type when the rule needs configuration.

condition.go
type PropertyEquals struct {
	Key   string
	Value string
}

func (c PropertyEquals) Matches(ctx component.ConditionContext) bool {
	return ctx.Value(c.Key) == c.Value
}

Then reuse the same condition type with different values:

register.go
func init() {
	component.Register(NewSmtpMailer).
		Conditional(PropertyEquals{
			Key:   "mail.provider",
			Value: "smtp",
		})

	component.Register(NewConsoleMailer).
		Conditional(PropertyEquals{
			Key:   "mail.provider",
			Value: "console",
		})
}

Keep conditions simple

Conditions should be fast and side-effect free. They are inclusion rules, not business logic. Avoid network calls, file writes, and expensive work inside Matches.