51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package rules
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
// OrRule combines multiple rules with OR logic
|
|
type OrRule struct {
|
|
Rules []Rule `json:"or"` // Child rules (extractors can be null/omitted)
|
|
Archivers []ArchiverConfig `json:"extractors,omitempty"` // Optional: only present on root rules, can be null for child rules (JSON tag kept for backward compatibility)
|
|
}
|
|
|
|
// Matches evaluates all child rules and returns archivers if ANY matches
|
|
func (r *OrRule) Matches(ctx context.Context, metadata *URLMetadata) ([]ArchiverConfig, error) {
|
|
if len(r.Rules) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
// Evaluate child rules - if any matches, return root rule's archivers
|
|
for _, childRule := range r.Rules {
|
|
archivers, err := childRule.Matches(ctx, metadata)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error evaluating child rule: %w", err)
|
|
}
|
|
// If child rule matches (returns non-nil, including empty slice), OR succeeds
|
|
// Empty slice means matched, nil means didn't match
|
|
if archivers != nil {
|
|
return r.Archivers, nil
|
|
}
|
|
}
|
|
|
|
// No child rules matched
|
|
return nil, nil
|
|
}
|
|
|
|
// IsValid validates that the rule is properly defined
|
|
func (r *OrRule) IsValid() error {
|
|
if len(r.Rules) == 0 {
|
|
return fmt.Errorf("or rule: at least one child rule is required")
|
|
}
|
|
|
|
// Validate all child rules
|
|
for i, childRule := range r.Rules {
|
|
if err := childRule.IsValid(); err != nil {
|
|
return fmt.Errorf("or rule: child rule %d is invalid: %w", i, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|