github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/event/filter/generic_filter.go (about)

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