Deluan 0ab10e819f refactor: simplify criteria Expression interface
Replaced the Fields() type switch with a fields() method on the
Expression interface, eliminating the need to update a central switch
when adding new expression types. Removed the now-redundant
criteriaExpression() marker method since fields() alone suffices to
restrict the interface. Extracted a conjunction interface for the
ChildPlaylistIds() lookup used by All and Any.
2026-04-26 10:58:57 -04:00

38 lines
776 B
Go

package criteria
import "fmt"
type Visitor func(Expression) error
func Walk(expr Expression, visit Visitor) error {
if expr == nil {
return nil
}
if err := visit(expr); err != nil {
return err
}
switch e := expr.(type) {
case All:
for _, child := range e {
if err := Walk(child, visit); err != nil {
return err
}
}
case Any:
for _, child := range e {
if err := Walk(child, visit); err != nil {
return err
}
}
case Is, IsNot, Gt, Lt, Before, After, Contains, NotContains, StartsWith, EndsWith, InTheRange, InTheLast, NotInTheLast, InPlaylist, NotInPlaylist:
return nil
default:
return fmt.Errorf("unknown criteria expression type %T", expr)
}
return nil
}
func Fields(expr Expression) map[string]any {
return expr.fields()
}