2015-10-11 05:21:58 +00:00
|
|
|
package flog
|
|
|
|
|
|
|
|
|
|
|
|
// NewMappingRouter returns an initialized MappingRouter.
|
|
|
|
func NewMappingRouter(subrouter Router, fn func(string, map[string]interface{})(string, map[string]interface{})) *MappingRouter {
|
|
|
|
router := MappingRouter{
|
|
|
|
subrouter:subrouter,
|
|
|
|
fn:fn,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &router
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MappingRouter is a Router that can modify the message and context before
|
|
|
|
// re-routing it to its sub-router.
|
|
|
|
//
|
|
|
|
// Conceptually this is somewhat similar to "map" functions in functional
|
|
|
|
// programming.
|
|
|
|
type MappingRouter struct {
|
|
|
|
subrouter Router
|
|
|
|
fn func(string, map[string]interface{})(string, map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (router *MappingRouter) Route(message string, context map[string]interface{}) error {
|
2016-09-20 18:03:35 +00:00
|
|
|
if nil == router {
|
|
|
|
return errNilReceiver
|
|
|
|
}
|
|
|
|
|
2015-10-11 05:21:58 +00:00
|
|
|
return router.subrouter.Route(router.fn(message, context))
|
|
|
|
}
|