Skip to content

Commit

Permalink
enhance gogf#3221
Browse files Browse the repository at this point in the history
  • Loading branch information
oldme-git committed Dec 21, 2023
1 parent 3f36236 commit 5e0ab2a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion util/gutil/gutil_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func FillStructWithDefault(structPtr interface{}) error {
}
fields, err := gstructs.Fields(gstructs.FieldsInput{
Pointer: reflectValue,
RecursiveOption: gstructs.RecursiveOptionNone,
RecursiveOption: gstructs.RecursiveOptionEmbedded,
})
if err != nil {
return err
Expand Down
32 changes: 24 additions & 8 deletions util/gutil/gutil_z_unit_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,32 @@ func Test_StructToSlice(t *testing.T) {
func Test_FillStructWithDefault(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type A struct {
V1 int `d:"1.01"`
V2 string `d:"1.01"`
V3 float32 `d:"1.01"`
A1 int `d:"1.01"`
A2 string `d:"1.01"`
A3 float32 `d:"1.01"`
}
a := A{}
err := gutil.FillStructWithDefault(&a)
type B struct {
B1 bool `d:"true"`
B2 string
A A
}
type C struct {
C1 float64 `d:"1.01"`
B
C2 bool
A A
}

c := C{}
err := gutil.FillStructWithDefault(&c)
t.AssertNil(err)

t.Assert(a.V1, `1`)
t.Assert(a.V2, `1.01`)
t.Assert(a.V3, `1.01`)
t.Assert(c.C1, `1.01`)
t.Assert(c.C2, false)
t.Assert(c.B1, true)
t.Assert(c.B2, ``)
t.Assert(c.A.A1, `1`)
t.Assert(c.A.A2, `1.01`)
t.Assert(c.A.A3, `1.01`)
})
}

0 comments on commit 5e0ab2a

Please sign in to comment.