Skip to content

Commit 218a63d

Browse files
committed
fix: Don't dereference pointers in where, groupBy, sort, etc.
1 parent ba97c37 commit 218a63d

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

internal/template/groupby.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package template
22

33
import (
44
"fmt"
5-
"reflect"
65
"strings"
76

87
"github.com/nginx-proxy/docker-gen/internal/context"
@@ -18,7 +17,7 @@ func generalizedGroupBy(funcName string, entries interface{}, getValue func(inte
1817

1918
groups := make(map[string][]interface{})
2019
for i := 0; i < entriesVal.Len(); i++ {
21-
v := reflect.Indirect(entriesVal.Index(i)).Interface()
20+
v := entriesVal.Index(i).Interface()
2221
value, err := getValue(v)
2322
if err != nil {
2423
return nil, err
@@ -73,13 +72,13 @@ func groupByKeys(entries interface{}, key string) ([]string, error) {
7372
// groupByLabel is the same as groupBy but over a given label
7473
func groupByLabel(entries interface{}, label string) (map[string][]interface{}, error) {
7574
getLabel := func(v interface{}) (interface{}, error) {
76-
if container, ok := v.(context.RuntimeContainer); ok {
75+
if container, ok := v.(*context.RuntimeContainer); ok {
7776
if value, ok := container.Labels[label]; ok {
7877
return value, nil
7978
}
8079
return nil, nil
8180
}
82-
return nil, fmt.Errorf("must pass an array or slice of RuntimeContainer to 'groupByLabel'; received %v", v)
81+
return nil, fmt.Errorf("must pass an array or slice of *RuntimeContainer to 'groupByLabel'; received %v", v)
8382
}
8483
return generalizedGroupBy("groupByLabel", entries, getLabel, func(groups map[string][]interface{}, value interface{}, v interface{}) {
8584
groups[value.(string)] = append(groups[value.(string)], v)

internal/template/groupby_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestGroupByExistingKey(t *testing.T) {
3535
assert.Len(t, groups, 2)
3636
assert.Len(t, groups["demo1.localhost"], 2)
3737
assert.Len(t, groups["demo2.localhost"], 1)
38-
assert.Equal(t, "3", groups["demo2.localhost"][0].(context.RuntimeContainer).ID)
38+
assert.Equal(t, "3", groups["demo2.localhost"][0].(*context.RuntimeContainer).ID)
3939
}
4040

4141
func TestGroupByAfterWhere(t *testing.T) {
@@ -69,7 +69,7 @@ func TestGroupByAfterWhere(t *testing.T) {
6969
assert.Len(t, groups, 2)
7070
assert.Len(t, groups["demo1.localhost"], 1)
7171
assert.Len(t, groups["demo2.localhost"], 1)
72-
assert.Equal(t, "3", groups["demo2.localhost"][0].(context.RuntimeContainer).ID)
72+
assert.Equal(t, "3", groups["demo2.localhost"][0].(*context.RuntimeContainer).ID)
7373
}
7474

7575
func TestGroupByKeys(t *testing.T) {
@@ -149,7 +149,7 @@ func TestGroupByLabel(t *testing.T) {
149149
assert.Len(t, groups["one"], 2)
150150
assert.Len(t, groups[""], 1)
151151
assert.Len(t, groups["two"], 1)
152-
assert.Equal(t, "2", groups["two"][0].(context.RuntimeContainer).ID)
152+
assert.Equal(t, "2", groups["two"][0].(*context.RuntimeContainer).ID)
153153
}
154154

155155
func TestGroupByLabelError(t *testing.T) {
@@ -193,13 +193,13 @@ func TestGroupByMulti(t *testing.T) {
193193
if len(groups["demo2.localhost"]) != 1 {
194194
t.Fatalf("expected 1 got %d", len(groups["demo2.localhost"]))
195195
}
196-
if groups["demo2.localhost"][0].(context.RuntimeContainer).ID != "3" {
197-
t.Fatalf("expected 2 got %s", groups["demo2.localhost"][0].(context.RuntimeContainer).ID)
196+
if groups["demo2.localhost"][0].(*context.RuntimeContainer).ID != "3" {
197+
t.Fatalf("expected 2 got %s", groups["demo2.localhost"][0].(*context.RuntimeContainer).ID)
198198
}
199199
if len(groups["demo3.localhost"]) != 1 {
200200
t.Fatalf("expect 1 got %d", len(groups["demo3.localhost"]))
201201
}
202-
if groups["demo3.localhost"][0].(context.RuntimeContainer).ID != "2" {
203-
t.Fatalf("expected 2 got %s", groups["demo3.localhost"][0].(context.RuntimeContainer).ID)
202+
if groups["demo3.localhost"][0].(*context.RuntimeContainer).ID != "2" {
203+
t.Fatalf("expected 2 got %s", groups["demo3.localhost"][0].(*context.RuntimeContainer).ID)
204204
}
205205
}

internal/template/sort.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (s *sortableByKey) set(funcName string, entries interface{}) (err error) {
4747
}
4848
s.data = make([]interface{}, entriesVal.Len())
4949
for i := 0; i < entriesVal.Len(); i++ {
50-
s.data[i] = reflect.Indirect(entriesVal.Index(i)).Interface()
50+
s.data[i] = entriesVal.Index(i).Interface()
5151
}
5252
return
5353
}

internal/template/sort_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ func TestSortObjectsByKeysAsc(t *testing.T) {
4949

5050
assert.NoError(t, err)
5151
assert.Len(t, sorted, 4)
52-
assert.Equal(t, "foo.localhost", sorted[0].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
53-
assert.Equal(t, "9", sorted[3].(context.RuntimeContainer).ID)
52+
assert.Equal(t, "foo.localhost", sorted[0].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
53+
assert.Equal(t, "9", sorted[3].(*context.RuntimeContainer).ID)
5454

5555
sorted, err = sortObjectsByKeysAsc(sorted, "Env.VIRTUAL_HOST")
5656

5757
assert.NoError(t, err)
5858
assert.Len(t, sorted, 4)
59-
assert.Equal(t, "foo.localhost", sorted[3].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
60-
assert.Equal(t, "8", sorted[0].(context.RuntimeContainer).ID)
59+
assert.Equal(t, "foo.localhost", sorted[3].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
60+
assert.Equal(t, "8", sorted[0].(*context.RuntimeContainer).ID)
6161
}
6262

6363
func TestSortObjectsByKeysDesc(t *testing.T) {
@@ -90,13 +90,13 @@ func TestSortObjectsByKeysDesc(t *testing.T) {
9090

9191
assert.NoError(t, err)
9292
assert.Len(t, sorted, 4)
93-
assert.Equal(t, "bar.localhost", sorted[0].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
94-
assert.Equal(t, "1", sorted[3].(context.RuntimeContainer).ID)
93+
assert.Equal(t, "bar.localhost", sorted[0].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
94+
assert.Equal(t, "1", sorted[3].(*context.RuntimeContainer).ID)
9595

9696
sorted, err = sortObjectsByKeysDesc(sorted, "Env.VIRTUAL_HOST")
9797

9898
assert.NoError(t, err)
9999
assert.Len(t, sorted, 4)
100-
assert.Equal(t, "", sorted[3].(context.RuntimeContainer).Env["VIRTUAL_HOST"])
101-
assert.Equal(t, "1", sorted[0].(context.RuntimeContainer).ID)
100+
assert.Equal(t, "", sorted[3].(*context.RuntimeContainer).Env["VIRTUAL_HOST"])
101+
assert.Equal(t, "1", sorted[0].(*context.RuntimeContainer).ID)
102102
}

internal/template/where.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func generalizedWhere(funcName string, entries interface{}, key string, test fun
1818

1919
selection := make([]interface{}, 0)
2020
for i := 0; i < entriesVal.Len(); i++ {
21-
v := reflect.Indirect(entriesVal.Index(i)).Interface()
21+
v := entriesVal.Index(i).Interface()
2222

2323
value := deepGet(v, key)
2424
if test(value) {

0 commit comments

Comments
 (0)