Skip to content

Commit 68d793e

Browse files
committed
fix nested struct conflict & should not flatten nested field
1 parent 69c2730 commit 68d793e

7 files changed

Lines changed: 47 additions & 10 deletions

File tree

env_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ func setEnvVars(t *testing.T, structName, prefix string) {
9090
"INTERVAL": "10s",
9191
"ID": "1234567890",
9292
"LABELS": "123,456",
93+
"LOG_FILE": "/var/log/global.log",
9394
"POSTGRES_ENABLED": "true",
9495
"POSTGRES_PORT": "5432",
9596
"POSTGRES_HOSTS": "192.168.2.1,192.168.2.2,192.168.2.3",
9697
"POSTGRES_DBNAME": "configdb",
9798
"POSTGRES_AVAILABILITYRATIO": "8.23",
99+
"POSTGRES_LOG_FILE": "/var/log/postgres.log",
98100
"POSTGRES_FOO": "8.23,9.12,11,90",
99101
}
100102
case "CamelCaseServer":
@@ -112,6 +114,7 @@ func setEnvVars(t *testing.T, structName, prefix string) {
112114
"HOSTS": "192.168.2.1,192.168.2.2,192.168.2.3",
113115
"DBNAME": "configdb",
114116
"AVAILABILITYRATIO": "8.23",
117+
"LOG_FILE": "/var/log/postgres.log",
115118
"FOO": "8.23,9.12,11,90",
116119
}
117120
}

flag.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (f *FlagLoader) Load(s interface{}) error {
6565
f.flagSet = flagSet
6666

6767
for _, field := range strct.Fields() {
68-
f.processField(field.Name(), field)
68+
f.processField(field.Name(), field, 0)
6969
}
7070

7171
flagSet.Usage = func() {
@@ -92,7 +92,7 @@ func filterArgs(args []string) []string {
9292
r := []string{}
9393
for i := 0; i < len(args); i++ {
9494
if strings.Index(args[i], "test.") >= 0 {
95-
if i + 1 < len(args) && strings.Index(args[i + 1], "-") == -1 {
95+
if i+1 < len(args) && strings.Index(args[i+1], "-") == -1 {
9696
i++
9797
}
9898
i++
@@ -106,7 +106,7 @@ func filterArgs(args []string) []string {
106106
// processField generates a flag based on the given field and fieldName. If a
107107
// nested struct is detected, a flag for each field of that nested struct is
108108
// generated too.
109-
func (f *FlagLoader) processField(fieldName string, field *structs.Field) error {
109+
func (f *FlagLoader) processField(fieldName string, field *structs.Field, nestedLevel int) error {
110110
if f.CamelCase {
111111
fieldName = strings.Join(camelcase.Split(fieldName), "-")
112112
fieldName = strings.Replace(fieldName, "---", "-", -1)
@@ -115,9 +115,9 @@ func (f *FlagLoader) processField(fieldName string, field *structs.Field) error
115115
switch field.Kind() {
116116
case reflect.Struct:
117117
for _, ff := range field.Fields() {
118-
flagName := field.Name() + "-" + ff.Name()
118+
flagName := fieldName + "-" + ff.Name()
119119

120-
if f.Flatten {
120+
if f.Flatten && nestedLevel == 0 {
121121
// first check if it's set or not, because if we have duplicate
122122
// we don't want to break the flag. Panic by giving a readable
123123
// output
@@ -131,7 +131,7 @@ func (f *FlagLoader) processField(fieldName string, field *structs.Field) error
131131
flagName = ff.Name()
132132
}
133133

134-
if err := f.processField(flagName, ff); err != nil {
134+
if err := f.processField(flagName, ff, nestedLevel+1); err != nil {
135135
return err
136136
}
137137
}

flag_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,13 @@ func getFlags(t *testing.T, structName, prefix string) []string {
201201
"-interval": "10s",
202202
"-id": "1234567890",
203203
"-labels": "123,456",
204+
"-log-file": "/var/log/global.log",
204205
"-postgres-enabled": "",
205206
"-postgres-port": "5432",
206207
"-postgres-hosts": "192.168.2.1,192.168.2.2,192.168.2.3",
207208
"-postgres-dbname": "configdb",
208209
"-postgres-availabilityratio": "8.23",
210+
"-postgres-log-file": "/var/log/postgres.log",
209211
}
210212
case "FlattenedServer":
211213
flags = map[string]string{
@@ -214,6 +216,7 @@ func getFlags(t *testing.T, structName, prefix string) []string {
214216
"--hosts": "192.168.2.1,192.168.2.2,192.168.2.3",
215217
"--dbname": "configdb",
216218
"--availabilityratio": "8.23",
219+
"-log-file": "/var/log/postgres.log",
217220
}
218221
case "FlattenedCamelCaseServer":
219222
flags = map[string]string{

multiconfig_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type (
1313
Labels []int
1414
Enabled bool
1515
Users []string
16+
Log Log
1617
Postgres Postgres
1718
unexported string
1819
Interval time.Duration
@@ -25,13 +26,18 @@ type (
2526
Hosts []string `required:"true"`
2627
DBName string `default:"configdb"`
2728
AvailabilityRatio float64
29+
Log Log
2830
unexported string
2931
}
3032

3133
TaggedServer struct {
3234
Name string `required:"true"`
3335
Postgres `structs:",flatten"`
3436
}
37+
38+
Log struct {
39+
File string
40+
}
3541
)
3642

3743
type FlattenedServer struct {
@@ -60,12 +66,14 @@ func getDefaultServer() *Server {
6066
Labels: []int{123, 456},
6167
Users: []string{"ankara", "istanbul"},
6268
Interval: 10 * time.Second,
69+
Log: Log{File: "/var/log/global.log"},
6370
Postgres: Postgres{
6471
Enabled: true,
6572
Port: 5432,
6673
Hosts: []string{"192.168.2.1", "192.168.2.2", "192.168.2.3"},
6774
DBName: "configdb",
6875
AvailabilityRatio: 8.23,
76+
Log: Log{File: "/var/log/postgres.log"},
6977
},
7078
}
7179
}
@@ -154,6 +162,10 @@ func testStruct(t *testing.T, s *Server, d *Server) {
154162
}
155163
}
156164

165+
if s.Log.File != d.Log.File {
166+
t.Errorf("Log value is wrong: %s, want: %s", s.Log.File, d.Log.File)
167+
}
168+
157169
testPostgres(t, s.Postgres, d.Postgres)
158170
}
159171

@@ -180,6 +192,10 @@ func testPostgres(t *testing.T, s Postgres, d Postgres) {
180192
t.Errorf("AvailabilityRatio is wrong: %f, want: %f", s.AvailabilityRatio, d.AvailabilityRatio)
181193
}
182194

195+
if s.Log.File != d.Log.File {
196+
t.Errorf("Postgres Log value is wrong: %s, want: %s", s.Log.File, d.Log.File)
197+
}
198+
183199
if len(s.Hosts) != len(d.Hosts) {
184200
// do not continue testing if this fails, because others is depending on this test
185201
t.Fatalf("Hosts len is wrong: %v, want: %v", s.Hosts, d.Hosts)

testdata/config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"ankara",
1212
"istanbul"
1313
],
14+
"Log": {
15+
"File": "/var/log/global.log"
16+
},
1417
"Postgres": {
1518
"Enabled": true,
1619
"Port": 5432,
@@ -19,6 +22,9 @@
1922
"192.168.2.2",
2023
"192.168.2.3"
2124
],
22-
"AvailabilityRatio": 8.23
25+
"AvailabilityRatio": 8.23,
26+
"Log": {
27+
"File": "/var/log/postgres.log"
28+
}
2329
}
2430
}

testdata/config.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
Name = "koding"
22
Enabled = true
33
Users = ["ankara", "istanbul"]
4-
Interval = 10000000000
5-
ID = 1234567890
6-
Labels = [123,456]
4+
Interval = 10000000000
5+
ID = 1234567890
6+
Labels = [123,456]
7+
[Log]
8+
File = "/var/log/global.log"
79

810
[Postgres]
911
Enabled = true
1012
Port = 5432
1113
Hosts = ["192.168.2.1", "192.168.2.2", "192.168.2.3"]
1214
AvailabilityRatio = 8.23
15+
[Postgres.Log]
16+
File = "/var/log/postgres.log"

testdata/config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ labels:
1616
- 123
1717
- 456
1818

19+
log:
20+
file: /var/log/global.log
21+
1922
# postgres configure
2023
postgres:
2124
enabled: true
@@ -25,4 +28,6 @@ postgres:
2528
- 192.168.2.2
2629
- 192.168.2.3
2730
availabilityratio: 8.23
31+
log:
32+
file: /var/log/postgres.log
2833

0 commit comments

Comments
 (0)