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