Skip to content

Commit

Permalink
refactor(rules): filter directly on notifications (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 authored Jul 22, 2024
1 parent 240e076 commit 428cef7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ func filter(notifications notifications.Notifications) (notifications.Notificati
if ruleFlag != "" {
found := false

var err error
for _, rule := range config.Data.Rules {
if rule.Name == ruleFlag {
found = true
seletedIds, err := rule.FilterIds(notifications)
notifications, err = rule.Filter(notifications)
if err != nil {
return nil, err
}
notifications = notifications.FilterFromIds(seletedIds)
}
}

Expand Down
7 changes: 3 additions & 4 deletions internal/config/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func (r Rule) Test() error {
return nil
}

// FilterIds filters the notifications with the jq filters and returns the IDs.
// TODO: return the notifications instead of the IDs.
func (r Rule) FilterIds(n notifications.Notifications) ([]string, error) {
// Filter filters the notifications with the jq filters and returns the IDs.
func (r Rule) Filter(n notifications.Notifications) (notifications.Notifications, error) {
var err error

// TODO: accept only a single filter so that we can use the jq.Filter function
Expand All @@ -67,5 +66,5 @@ func (r Rule) FilterIds(n notifications.Notifications) ([]string, error) {
}
}

return n.IDList(), nil
return n, nil
}
4 changes: 2 additions & 2 deletions internal/config/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestFilterIds(t *testing.T) {

for _, test := range tests {
t.Run(test.r.Name, func(t *testing.T) {
got, err := test.r.FilterIds(test.n)
got, err := test.r.Filter(test.n)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
Expand All @@ -170,7 +170,7 @@ func TestFilterIds(t *testing.T) {
t.Fatalf("want %#v, but got %#v", test.want, got)
}

if !slices.Equal(got, test.want) {
if !slices.Equal(got.IDList(), test.want) {
t.Fatalf("want %#v, but got %#v", test.want, got)
}
})
Expand Down
2 changes: 1 addition & 1 deletion internal/jq/jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Filter(filter string, n notifications.Notifications) (notifications.Notific
}

// Extract the IDs from the notifications that match the filter.
// The actual selection will be done by the parent.
// FilterFromIds will be used to get back the notifications from the IDs.
query, err := gojq.Parse(fmt.Sprintf(".[] | select(%s) | .id", filter))
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func (m *Manager) Apply() error {
continue
}

selectedIds, err := rule.FilterIds(m.Notifications)
selectedNotifications, err := rule.Filter(m.Notifications)
if err != nil {
return err
}

slog.Debug("apply rule", "name", rule.Name, "count", len(selectedIds))
slog.Debug("apply rule", "name", rule.Name, "count", len(selectedNotifications))

for _, notification := range m.Notifications.FilterFromIds(selectedIds) {
for _, notification := range selectedNotifications {
if notification.Meta.Done && !m.ForceStrategy.Has(ForceApply) {
slog.Debug("skipping done notification", "id", notification.Id)
continue
Expand Down

0 comments on commit 428cef7

Please sign in to comment.