watch: support parameter exemption

This commit is contained in:
Armon Dadgar 2014-08-20 16:38:15 -07:00
parent b8a5089710
commit 19b55fab59
2 changed files with 33 additions and 0 deletions

View File

@ -17,6 +17,7 @@ type WatchPlan struct {
Datacenter string
Token string
Type string
Exempt map[string][]string
Func WatchFunc
Handler HandlerFunc
@ -38,6 +39,12 @@ type HandlerFunc func(uint64, interface{})
// Parse takes a watch query and compiles it into a WatchPlan or an error
func Parse(query string) (*WatchPlan, error) {
return ParseExempt(query, nil)
}
// ParseExempt takes a watch query and compiles it into a WatchPlan or an error
// Any exempt parameters are stored in the Exempt map
func ParseExempt(query string, exempt []string) (*WatchPlan, error) {
tokens, err := tokenize(query)
if err != nil {
return nil, fmt.Errorf("Failed to parse: %v", err)
@ -77,6 +84,18 @@ func Parse(query string) (*WatchPlan, error) {
}
plan.Func = fn
// Remove the exempt parameters
if len(exempt) > 0 {
plan.Exempt = make(map[string][]string)
for _, ex := range exempt {
val, ok := params[ex]
if ok {
plan.Exempt[ex] = val
delete(params, ex)
}
}
}
// Ensure all parameters are consumed
if len(params) != 0 {
var bad []string

View File

@ -104,3 +104,17 @@ func TestParseBasic(t *testing.T) {
t.Fatalf("Bad: %#v", p)
}
}
func TestParse_exempt(t *testing.T) {
p, err := ParseExempt("type:key key:foo handler:foobar", []string{"handler"})
if err != nil {
t.Fatalf("err: %v", err)
}
if p.Type != "key" {
t.Fatalf("Bad: %#v", p)
}
ex := p.Exempt["handler"]
if len(ex) != 1 && ex[0] != "foobar" {
t.Fatalf("bad: %v", ex)
}
}