Skip to content

Commit

Permalink
optimize filterByStateAndGroup to handle array "group" values
Browse files Browse the repository at this point in the history
  • Loading branch information
dickens7 committed Apr 25, 2024
1 parent 1e2af18 commit 8be9037
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 12 additions & 2 deletions client/xclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,18 @@ func filterByStateAndGroup(group string, servers map[string]string) {
if state := values.Get("state"); state == "inactive" {
delete(servers, k)
}
if group != "" && group != values.Get("group") {
delete(servers, k)
groups := values["group"] // Directly access the map to get all values associated with "group" as a slice
if group != "" {
found := false
for _, g := range groups {
if group == g {
found = true
break // A matching group is found, stop the search
}
}
if !found {
delete(servers, k) // If no matching group is found, delete the corresponding server from the map
}
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion client/xclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestXClient_IT(t *testing.T) {
}

func TestXClient_filterByStateAndGroup(t *testing.T) {
servers := map[string]string{"a": "", "b": "state=inactive&ops=10", "c": "ops=20", "d": "group=test&ops=20"}
servers := map[string]string{"a": "", "b": "state=inactive&ops=10", "c": "ops=20", "d": "group=test1&group=test&ops=20"}
filterByStateAndGroup("test", servers)
if _, ok := servers["b"]; ok {
t.Error("has not remove inactive node")
Expand All @@ -107,6 +107,12 @@ func TestXClient_filterByStateAndGroup(t *testing.T) {
if _, ok := servers["d"]; !ok {
t.Error("node must be removed")
}

filterByStateAndGroup("test1", servers)

if _, ok := servers["d"]; !ok {
t.Error("node must be removed")
}
}

func TestUncoverError(t *testing.T) {
Expand Down

0 comments on commit 8be9037

Please sign in to comment.