...

Source file src/github.com/docker/go-events/filter.go

Documentation: github.com/docker/go-events

     1  package events
     2  
     3  // Matcher matches events.
     4  type Matcher interface {
     5  	Match(event Event) bool
     6  }
     7  
     8  // MatcherFunc implements matcher with just a function.
     9  type MatcherFunc func(event Event) bool
    10  
    11  // Match calls the wrapped function.
    12  func (fn MatcherFunc) Match(event Event) bool {
    13  	return fn(event)
    14  }
    15  
    16  // Filter provides an event sink that sends only events that are accepted by a
    17  // Matcher. No methods on filter are goroutine safe.
    18  type Filter struct {
    19  	dst     Sink
    20  	matcher Matcher
    21  	closed  bool
    22  }
    23  
    24  // NewFilter returns a new filter that will send to events to dst that return
    25  // true for Matcher.
    26  func NewFilter(dst Sink, matcher Matcher) Sink {
    27  	return &Filter{dst: dst, matcher: matcher}
    28  }
    29  
    30  // Write an event to the filter.
    31  func (f *Filter) Write(event Event) error {
    32  	if f.closed {
    33  		return ErrSinkClosed
    34  	}
    35  
    36  	if f.matcher.Match(event) {
    37  		return f.dst.Write(event)
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  // Close the filter and allow no more events to pass through.
    44  func (f *Filter) Close() error {
    45  	// TODO(stevvooe): Not all sinks should have Close.
    46  	if f.closed {
    47  		return nil
    48  	}
    49  
    50  	f.closed = true
    51  	return f.dst.Close()
    52  }
    53  

View as plain text