github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/event/filter/generic_filter.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package filter
    13  
    14  type Generic struct {
    15  	Str1, Str2, Str3 string
    16  	Data             map[string]struct{}
    17  
    18  	Fn func(data interface{})
    19  }
    20  
    21  // self = registered, f = incoming
    22  func (self Generic) Compare(f Filter) bool {
    23  	var strMatch, dataMatch = true, true
    24  
    25  	filter := f.(Generic)
    26  	if (len(self.Str1) > 0 && filter.Str1 != self.Str1) ||
    27  		(len(self.Str2) > 0 && filter.Str2 != self.Str2) ||
    28  		(len(self.Str3) > 0 && filter.Str3 != self.Str3) {
    29  		strMatch = false
    30  	}
    31  
    32  	for k := range self.Data {
    33  		if _, ok := filter.Data[k]; !ok {
    34  			return false
    35  		}
    36  	}
    37  
    38  	return strMatch && dataMatch
    39  }
    40  
    41  func (self Generic) Trigger(data interface{}) {
    42  	self.Fn(data)
    43  }