Skip to content

Commit 7e9ddbc

Browse files
committed
groupByLabel: empty string value is a valid group
1 parent f407e1d commit 7e9ddbc

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

template.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ func groupByKeys(entries interface{}, key string) ([]string, error) {
116116
func groupByLabel(entries interface{}, label string) (map[string][]interface{}, error) {
117117
getLabel := func(v interface{}) (interface{}, error) {
118118
if container, ok := v.(RuntimeContainer); ok {
119-
if container.Labels[label] == "" {
120-
return nil, nil
119+
if value, ok := container.Labels[label]; ok {
120+
return value, nil
121121
}
122-
return container.Labels[label], nil
122+
return nil, nil
123123
}
124124
return nil, fmt.Errorf("Must pass an array or slice of RuntimeContainer to 'groupByLabel'; received %v", v)
125125
}

template_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,29 @@ func TestGroupByLabel(t *testing.T) {
209209
&RuntimeContainer{
210210
ID: "4",
211211
},
212+
&RuntimeContainer{
213+
Labels: map[string]string{
214+
"com.docker.compose.project": "",
215+
},
216+
ID: "5",
217+
},
212218
}
213219

214220
groups, err := groupByLabel(containers, "com.docker.compose.project")
215221
if err != nil {
216222
t.FailNow()
217223
}
218224

219-
if len(groups) != 2 {
225+
if len(groups) != 3 {
220226
t.Fail()
221227
}
222228

223229
if len(groups["one"]) != 2 {
224230
t.Fail()
225231
}
232+
if len(groups[""]) != 1 {
233+
t.Fail()
234+
}
226235

227236
if len(groups["two"]) != 1 {
228237
t.FailNow()

0 commit comments

Comments
 (0)