signification change to FilteredRouter. only filters one (sub)router now.
parent
99f30eff97
commit
73c5e4bfaf
|
@ -1,11 +1,10 @@
|
|||
package flog
|
||||
|
||||
|
||||
func NewFilteredRouter() *FilteredRouter {
|
||||
registry := make([]struct{FilterFn func(string, map[string]interface{})bool ; Subrouter Router}, 0, 2)
|
||||
|
||||
func NewFilteredRouter(subrouter Router, filterFn func(string, map[string]interface{})bool) *FilteredRouter {
|
||||
router := FilteredRouter{
|
||||
registry:registry,
|
||||
subrouter:subrouter,
|
||||
filterFn:filterFn,
|
||||
}
|
||||
|
||||
return &router
|
||||
|
@ -13,26 +12,15 @@ func NewFilteredRouter() *FilteredRouter {
|
|||
|
||||
|
||||
type FilteredRouter struct {
|
||||
registry []struct{FilterFn func(string, map[string]interface{})bool ; Subrouter Router}
|
||||
subrouter Router
|
||||
filterFn func(string, map[string]interface{})bool
|
||||
}
|
||||
|
||||
|
||||
func (router *FilteredRouter) Route(message string, context map[string]interface{}) error {
|
||||
for _, datum := range router.registry {
|
||||
if datum.FilterFn(message, context) {
|
||||
return datum.Subrouter.Route(message, context)
|
||||
}
|
||||
if router.filterFn(message, context) {
|
||||
return router.subrouter.Route(message, context)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (router *FilteredRouter) Register(subrouter Router, filterFn func(string, map[string]interface{})bool) {
|
||||
datum := struct{FilterFn func(string, map[string]interface{})bool ; Subrouter Router}{
|
||||
FilterFn: filterFn,
|
||||
Subrouter: subrouter,
|
||||
}
|
||||
|
||||
router.registry = append(router.registry, datum)
|
||||
}
|
||||
|
|
|
@ -12,8 +12,7 @@ import (
|
|||
|
||||
func TestFilteredRouterJustCreated(t *testing.T) {
|
||||
|
||||
router := NewFilteredRouter()
|
||||
router.Register(NewDiscardRouter(), func(string, map[string]interface{}) bool {
|
||||
router := NewFilteredRouter(NewDiscardRouter(), func(string, map[string]interface{}) bool {
|
||||
return false
|
||||
})
|
||||
if nil == router {
|
||||
|
@ -32,9 +31,7 @@ func TestFilteredRouterJustFilterParameters(t *testing.T) {
|
|||
var filterContext map[string] interface{}
|
||||
var filterResult = false
|
||||
|
||||
router := NewFilteredRouter()
|
||||
|
||||
router.Register(NewDiscardRouter(), func(message string, context map[string]interface{}) bool {
|
||||
router := NewFilteredRouter(NewDiscardRouter(), func(message string, context map[string]interface{}) bool {
|
||||
filterMessage = message
|
||||
filterContext = context
|
||||
|
||||
|
@ -78,5 +75,4 @@ TLoop: for testNumber:=0; testNumber<NUM_TESTS; testNumber++ {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue