Skip to content

Commit 8be9037

Browse files
committed
optimize filterByStateAndGroup to handle array "group" values
1 parent 1e2af18 commit 8be9037

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

โ€Žclient/xclient.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,18 @@ func filterByStateAndGroup(group string, servers map[string]string) {
235235
if state := values.Get("state"); state == "inactive" {
236236
delete(servers, k)
237237
}
238-
if group != "" && group != values.Get("group") {
239-
delete(servers, k)
238+
groups := values["group"] // Directly access the map to get all values associated with "group" as a slice
239+
if group != "" {
240+
found := false
241+
for _, g := range groups {
242+
if group == g {
243+
found = true
244+
break // A matching group is found, stop the search
245+
}
246+
}
247+
if !found {
248+
delete(servers, k) // If no matching group is found, delete the corresponding server from the map
249+
}
240250
}
241251
}
242252
}

โ€Žclient/xclient_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestXClient_IT(t *testing.T) {
9393
}
9494

9595
func TestXClient_filterByStateAndGroup(t *testing.T) {
96-
servers := map[string]string{"a": "", "b": "state=inactive&ops=10", "c": "ops=20", "d": "group=test&ops=20"}
96+
servers := map[string]string{"a": "", "b": "state=inactive&ops=10", "c": "ops=20", "d": "group=test1&group=test&ops=20"}
9797
filterByStateAndGroup("test", servers)
9898
if _, ok := servers["b"]; ok {
9999
t.Error("has not remove inactive node")
@@ -107,6 +107,12 @@ func TestXClient_filterByStateAndGroup(t *testing.T) {
107107
if _, ok := servers["d"]; !ok {
108108
t.Error("node must be removed")
109109
}
110+
111+
filterByStateAndGroup("test1", servers)
112+
113+
if _, ok := servers["d"]; !ok {
114+
t.Error("node must be removed")
115+
}
110116
}
111117

112118
func TestUncoverError(t *testing.T) {

0 commit comments

Comments
ย (0)