diff --git a/rules/byHost.go b/rules/byHost.go index 9366260..88c32e4 100644 --- a/rules/byHost.go +++ b/rules/byHost.go @@ -32,12 +32,16 @@ func (instance *ByHost) All(consumer func(Rule) error) error { func (instance *ByHost) Find(host string, path []string) (Rules, error) { if existing, ok := instance.values[host]; ok { - return existing.Find(path) - } else if instance.fallback != nil { - return instance.fallback.Find(path) - } else { - return rules{}, nil + if r, err := existing.Find(path); r != nil && r.Len() > 0 && err == nil { + return r, err + } + } + if instance.fallback != nil { + if r, err := instance.fallback.Find(path); r != nil && r.Len() > 0 && err == nil { + return r, err + } } + return rules{}, nil } func (instance *ByHost) Put(r Rule) error { diff --git a/rules/tree/tree.go b/rules/tree/tree.go index e5c3b31..3857e7b 100644 --- a/rules/tree/tree.go +++ b/rules/tree/tree.go @@ -186,6 +186,7 @@ func (instance *Tree) find(path []string) (result []interface{}) { current := instance.root for _, key := range path { + matchInPart := false if current.elements != nil { if candidate, ok := current.elements[key]; ok { result = candidate @@ -194,8 +195,12 @@ func (instance *Tree) find(path []string) (result []interface{}) { if current.children != nil { if child, ok := current.children[key]; ok { current = child + matchInPart = true } } + if !matchInPart { + break + } } return diff --git a/rules/tree/tree_find_test.go b/rules/tree/tree_find_test.go index 13f046d..7b8b1a3 100644 --- a/rules/tree/tree_find_test.go +++ b/rules/tree/tree_find_test.go @@ -86,6 +86,8 @@ func Test_Node_Find(t *testing.T) { executeTestFindRun(t, "/a2/b2/c4", "A2B2") executeTestFindRun(t, "/a3/b2/c4", "A3") executeTestFindRun(t, "/a1/b1/c1/x1", "A1B1C1") + executeTestFindRun(t, "/a2", "A2") + executeTestFindRun(t, "/xxx/a2", "ROOT") } func executeTestFindRun(t *testing.T, path string, expectedElements ...interface{}) {