From 0d08d36bf576babd74f21b83ccde019acde4d9c7 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Wed, 2 Apr 2014 20:04:33 -0700 Subject: [PATCH] beam: new routing functions Route.KeyEquals, Route.KeyIncludes, Route.NoKey Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- pkg/beam/router.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/beam/router.go b/pkg/beam/router.go index 3dcc3cc727..e19ec9d449 100644 --- a/pkg/beam/router.go +++ b/pkg/beam/router.go @@ -117,6 +117,41 @@ func (r *Route) KeyStartsWith(k string, beginning ...string) *Route { return r } +func (r *Route) KeyEquals(k string, full...string) *Route { + r.rules = append(r.rules, func(payload []byte, attachment *os.File) bool { + values := data.Message(payload).Get(k) + if len(values) != len(full) { + return false + } + for i, v := range full { + if v != values[i] { + return false + } + } + return true + }) + return r +} + +func (r *Route) KeyIncludes(k, v string) *Route { + r.rules = append(r.rules, func(payload []byte, attachment *os.File) bool { + for _, val := range data.Message(payload).Get(k) { + if val == v { + return true + } + } + return false + }) + return r +} + +func (r *Route) NoKey(k string) *Route { + r.rules = append(r.rules, func(payload []byte, attachment *os.File) bool { + return len(data.Message(payload).Get(k)) == 0 + }) + return r +} + func (r *Route) Passthrough(dst Sender) *Route { r.handler = func(payload []byte, attachment *os.File) error { return dst.Send(payload, attachment)