Skip to content

Commit ea8fc52

Browse files
committed
Move reflection functions to separate file
1 parent c1d38a4 commit ea8fc52

File tree

3 files changed

+113
-28
lines changed

3 files changed

+113
-28
lines changed

docker-gen.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"os/exec"
1717
"os/signal"
1818
"path/filepath"
19-
"reflect"
19+
2020
"strings"
2121
"syscall"
2222
"text/template"
@@ -72,33 +72,8 @@ func (c *ConfigFile) filterWatches() ConfigFile {
7272
}
7373
}
7474

75-
func deepGet(item interface{}, path string) interface{} {
76-
if path == "" {
77-
return item
78-
}
79-
80-
parts := strings.Split(path, ".")
81-
itemValue := reflect.ValueOf(item)
82-
83-
if len(parts) > 0 {
84-
switch itemValue.Kind() {
85-
case reflect.Struct:
86-
fieldValue := itemValue.FieldByName(parts[0])
87-
if fieldValue.IsValid() {
88-
return deepGet(fieldValue.Interface(), strings.Join(parts[1:], "."))
89-
}
90-
case reflect.Map:
91-
mapValue := itemValue.MapIndex(reflect.ValueOf(parts[0]))
92-
if mapValue.IsValid() {
93-
return deepGet(mapValue.Interface(), strings.Join(parts[1:], "."))
94-
}
95-
default:
96-
fmt.Printf("can't group by %s\n", path)
97-
}
98-
return nil
99-
}
100-
101-
return itemValue.Interface()
75+
func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {
76+
return r.ID == o.ID && r.Image == o.Image
10277
}
10378

10479
func groupBy(entries []*RuntimeContainer, key string) map[string][]*RuntimeContainer {

reflect.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"strings"
7+
)
8+
9+
func stripPrefix(s, prefix string) string {
10+
path := s
11+
for {
12+
if strings.HasPrefix(path, ".") {
13+
path = path[1:]
14+
continue
15+
}
16+
break
17+
}
18+
return path
19+
}
20+
21+
func deepGet(item interface{}, path string) interface{} {
22+
if path == "" {
23+
return item
24+
}
25+
26+
path = stripPrefix(path, ".")
27+
parts := strings.Split(path, ".")
28+
itemValue := reflect.ValueOf(item)
29+
30+
if len(parts) > 0 {
31+
switch itemValue.Kind() {
32+
case reflect.Struct:
33+
fieldValue := itemValue.FieldByName(parts[0])
34+
if fieldValue.IsValid() {
35+
return deepGet(fieldValue.Interface(), strings.Join(parts[1:], "."))
36+
}
37+
case reflect.Map:
38+
mapValue := itemValue.MapIndex(reflect.ValueOf(parts[0]))
39+
if mapValue.IsValid() {
40+
return deepGet(mapValue.Interface(), strings.Join(parts[1:], "."))
41+
}
42+
default:
43+
fmt.Printf("can't group by %s\n", path)
44+
}
45+
return nil
46+
}
47+
48+
return itemValue.Interface()
49+
}

reflect_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestDeepGetNoPath(t *testing.T) {
6+
item := RuntimeContainer{}
7+
value := deepGet(item, "")
8+
if _, ok := value.(RuntimeContainer); !ok {
9+
t.Fail()
10+
}
11+
12+
var returned RuntimeContainer
13+
returned = value.(RuntimeContainer)
14+
if !returned.Equals(item) {
15+
t.Fail()
16+
}
17+
}
18+
19+
func TestDeepGetSimple(t *testing.T) {
20+
item := RuntimeContainer{
21+
ID: "expected",
22+
}
23+
value := deepGet(item, "ID")
24+
if _, ok := value.(string); !ok {
25+
t.Errorf("expected: %u. got: %u", "expected", value)
26+
}
27+
28+
if value != "expected" {
29+
t.Errorf("expected: %s. got: %s", "expected", value)
30+
}
31+
}
32+
33+
func TestDeepGetSimpleDotPrefix(t *testing.T) {
34+
item := RuntimeContainer{
35+
ID: "expected",
36+
}
37+
value := deepGet(item, "...ID")
38+
if _, ok := value.(string); !ok {
39+
t.Errorf("expected: %u. got: %u", "expected", value)
40+
}
41+
42+
if value != "expected" {
43+
t.Errorf("expected: %s. got: %s", "expected", value)
44+
}
45+
}
46+
47+
func TestDeepGetMap(t *testing.T) {
48+
item := RuntimeContainer{
49+
Env: map[string]string{
50+
"key": "value",
51+
},
52+
}
53+
value := deepGet(item, "Env.key")
54+
if _, ok := value.(string); !ok {
55+
t.Errorf("expected: %u. got: %u", "value", value)
56+
}
57+
58+
if value != "value" {
59+
t.Errorf("expected: %s. got: %s", "value", value)
60+
}
61+
}

0 commit comments

Comments
 (0)