Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support boolean avoidSplitting property in rules #373

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/engine/config/v1alpha3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ func (f FilterNode) NonEmptyFields() []string {
// For every email, if the filter applies correctly, then the specified actions
// will be applied to it.
type Rule struct {
Filter FilterNode `json:"filter"`
Actions Actions `json:"actions"`
Filter FilterNode `json:"filter"`
Actions Actions `json:"actions"`
AvoidSplitting bool `json:"avoidSplitting,omitempty"`
}

// Author represents the owner of the gmail account.
Expand Down
10 changes: 5 additions & 5 deletions internal/engine/filter/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func FromRulesWithLimit(rs []parser.Rule, sizeLimit int) (Filters, error) {
// FromRule translates a rule into entries that map directly into Gmail filters.
func FromRule(rule parser.Rule, sizeLimit int) (Filters, error) {
var crits []Criteria
for _, c := range splitCriteria(rule.Criteria, sizeLimit) {
for _, c := range splitCriteria(rule.Criteria, sizeLimit, rule.AvoidSplitting) {
criteria, err := GenerateCriteria(c)
if err != nil {
return nil, fmt.Errorf("generating criteria: %w", err)
Expand Down Expand Up @@ -258,9 +258,9 @@ func quote(a string) string {
return a
}

func splitCriteria(tree parser.CriteriaAST, limit int) []parser.CriteriaAST {
func splitCriteria(tree parser.CriteriaAST, limit int, avoidSplitting bool) []parser.CriteriaAST {
var res []parser.CriteriaAST
for _, c := range splitRootOr(tree) {
for _, c := range splitRootOr(tree, avoidSplitting) {
res = append(res, splitBigCriteria(c, limit)...)
}
return res
Expand Down Expand Up @@ -420,7 +420,7 @@ func countNodes(tree parser.CriteriaAST) int {
return cv.res
}

func splitRootOr(tree parser.CriteriaAST) []parser.CriteriaAST {
func splitRootOr(tree parser.CriteriaAST, avoidSplitting bool) []parser.CriteriaAST {
// Since Gmail filters are all applied when they match, we can reduce
// the size of a rule and make it more readable by splitting a single
// rule where wee have an OR as the top-level operation, with a set of
Expand All @@ -432,7 +432,7 @@ func splitRootOr(tree parser.CriteriaAST) []parser.CriteriaAST {
// - to:b => archive
// - list:c => archive
root, ok := tree.(*parser.Node)
if !ok || root.Operation != parser.OperationOr {
if !ok || root.Operation != parser.OperationOr || avoidSplitting {
return []parser.CriteriaAST{tree}
}
return root.Children
Expand Down
10 changes: 6 additions & 4 deletions internal/engine/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

// Rule is an intermediate representation of a Gmail filter.
type Rule struct {
Criteria CriteriaAST
Actions Actions
Criteria CriteriaAST
Actions Actions
AvoidSplitting bool
}

// Actions contains the actions to be applied to a set of emails.
Expand Down Expand Up @@ -53,8 +54,9 @@ func parseRule(rule cfg.Rule) (Rule, error) {
}

return Rule{
Criteria: scrit,
Actions: Actions(rule.Actions),
Criteria: scrit,
Actions: Actions(rule.Actions),
AvoidSplitting: rule.AvoidSplitting,
}, nil
}

Expand Down