Skip to content

Commit

Permalink
Fix rule path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Jul 17, 2020
1 parent cea371f commit a18bdf5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions rules/byHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions rules/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions rules/tree/tree_find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down

0 comments on commit a18bdf5

Please sign in to comment.