Skip to content

Commit 24ade10

Browse files
authored
Merge pull request #4 from bc-vincent-zhao/fill-slice-of-struct
add support to fill in slice of structs
2 parents 0fccfd9 + 1f59c8d commit 24ade10

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

defaults.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ func newDefaultFiller() *Filler {
7171
}
7272

7373
funcs[reflect.Slice] = func(field *FieldData) {
74-
if field.Value.Type().Elem().Kind() == reflect.Uint8 {
74+
k := field.Value.Type().Elem().Kind()
75+
switch k {
76+
case reflect.Uint8:
7577
if field.Value.Bytes() != nil {
7678
return
7779
}
78-
7980
field.Value.SetBytes([]byte(field.TagValue))
81+
case reflect.Struct:
82+
count := field.Value.Len()
83+
for i := 0; i < count; i++ {
84+
fields := getDefaultFiller().GetFieldsFromValue(field.Value.Index(i), nil)
85+
getDefaultFiller().SetDefaultValues(fields)
86+
}
8087
}
8188
}
8289

defaults_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ type DefaultsSuite struct{}
1414

1515
var _ = Suite(&DefaultsSuite{})
1616

17+
type Parent struct {
18+
Children []Child
19+
}
20+
21+
type Child struct {
22+
Name string
23+
Age int `default:"10"`
24+
}
25+
1726
type ExampleBasic struct {
1827
Bool bool `default:"true"`
1928
Integer int `default:"33"`
@@ -35,6 +44,7 @@ type ExampleBasic struct {
3544
Integer int `default:"33"`
3645
}
3746
Duration time.Duration `default:"1s"`
47+
Children []Child
3848
}
3949

4050
func (s *DefaultsSuite) TestSetDefaultsBasic(c *C) {
@@ -73,6 +83,7 @@ func (s *DefaultsSuite) assertTypes(c *C, foo *ExampleBasic) {
7383
c.Assert(foo.Float64, Equals, 6.4)
7484
c.Assert(foo.Struct.Bool, Equals, true)
7585
c.Assert(foo.Duration, Equals, time.Second)
86+
c.Assert(foo.Children, IsNil)
7687
}
7788

7889
func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
@@ -82,6 +93,7 @@ func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
8293
Float32: 9.9,
8394
String: "bar",
8495
Bytes: []byte("foo"),
96+
Children: []Child{{Name: "alice"}, {Name: "bob", Age: 2}},
8597
}
8698

8799
SetDefaults(foo)
@@ -91,6 +103,8 @@ func (s *DefaultsSuite) TestSetDefaultsWithValues(c *C) {
91103
c.Assert(foo.Float32, Equals, float32(9.9))
92104
c.Assert(foo.String, Equals, "bar")
93105
c.Assert(string(foo.Bytes), Equals, "foo")
106+
c.Assert(foo.Children[0].Age, Equals, 10)
107+
c.Assert(foo.Children[1].Age, Equals, 2)
94108
}
95109

96110
func (s *DefaultsSuite) BenchmarkLogic(c *C) {

filler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ func (f *Filler) isEmpty(field *FieldData) bool {
8585
return false
8686
}
8787
case reflect.Slice:
88-
if field.Value.Len() != 0 {
89-
return false
88+
switch field.Value.Type().Elem().Kind() {
89+
case reflect.Struct:
90+
// always assume the structs in the slice is empty and can be filled
91+
// the actually struct filling logic should take care of the rest
92+
return true
93+
default:
94+
return field.Value.Len() == 0
9095
}
9196
case reflect.String:
9297
if field.Value.String() != "" {

0 commit comments

Comments
 (0)