filter.go

  1package cache
  2
  3import (
  4	"strings"
  5
  6	"github.com/MichaelMure/git-bug/bug"
  7)
  8
  9type Filter func(excerpt *BugExcerpt) bool
 10
 11func StatusFilter(query string) (Filter, error) {
 12	status, err := bug.StatusFromString(query)
 13	if err != nil {
 14		return nil, err
 15	}
 16
 17	return func(excerpt *BugExcerpt) bool {
 18		return excerpt.Status == status
 19	}, nil
 20}
 21
 22func AuthorFilter(query string) Filter {
 23	cleaned := strings.TrimFunc(query, func(r rune) bool {
 24		return r == '"' || r == '\''
 25	})
 26
 27	return func(excerpt *BugExcerpt) bool {
 28		return excerpt.Author.Match(cleaned)
 29	}
 30}
 31
 32func LabelFilter(label string) Filter {
 33	return func(excerpt *BugExcerpt) bool {
 34		for _, l := range excerpt.Labels {
 35			if string(l) == label {
 36				return true
 37			}
 38		}
 39		return false
 40	}
 41}
 42
 43func NoLabelFilter() Filter {
 44	return func(excerpt *BugExcerpt) bool {
 45		return len(excerpt.Labels) == 0
 46	}
 47}
 48
 49type Filters struct {
 50	Status    []Filter
 51	Author    []Filter
 52	Label     []Filter
 53	NoFilters []Filter
 54}
 55
 56// Match check if a bug match the set of filters
 57func (f *Filters) Match(excerpt *BugExcerpt) bool {
 58	if match := f.orMatch(f.Status, excerpt); !match {
 59		return false
 60	}
 61
 62	if match := f.orMatch(f.Author, excerpt); !match {
 63		return false
 64	}
 65
 66	if match := f.orMatch(f.Label, excerpt); !match {
 67		return false
 68	}
 69
 70	if match := f.andMatch(f.NoFilters, excerpt); !match {
 71		return false
 72	}
 73
 74	return true
 75}
 76
 77// Check if any of the filters provided match the bug
 78func (*Filters) orMatch(filters []Filter, excerpt *BugExcerpt) bool {
 79	if len(filters) == 0 {
 80		return true
 81	}
 82
 83	match := false
 84	for _, f := range filters {
 85		match = match || f(excerpt)
 86	}
 87
 88	return match
 89}
 90
 91// Check if all of the filters provided match the bug
 92func (*Filters) andMatch(filters []Filter, excerpt *BugExcerpt) bool {
 93	if len(filters) == 0 {
 94		return true
 95	}
 96
 97	match := true
 98	for _, f := range filters {
 99		match = match && f(excerpt)
100	}
101
102	return match
103}