File tree Expand file tree Collapse file tree 3 files changed +40
-5
lines changed
Expand file tree Collapse file tree 3 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -241,13 +241,20 @@ func removeEmptyStructs(spec interface{}) error {
241241 for i := 0 ; i < s .NumField (); i ++ {
242242 f := s .Field (i )
243243
244- if f .Kind () == reflect .Ptr && f .Elem ().Kind () == reflect .Struct && ! f .IsNil () {
245- if err := removeEmptyStructs (f .Elem ().Addr ().Interface ()); err != nil {
244+ switch f .Kind () {
245+ case reflect .Struct :
246+ if err := removeEmptyStructs (f .Addr ().Interface ()); err != nil {
246247 return err
247248 }
249+ case reflect .Ptr :
250+ if f .Elem ().Kind () == reflect .Struct && ! f .IsNil () {
251+ if err := removeEmptyStructs (f .Elem ().Addr ().Interface ()); err != nil {
252+ return err
253+ }
248254
249- if f .Elem ().IsZero () {
250- f .Set (reflect .Zero (f .Type ()))
255+ if f .Elem ().IsZero () {
256+ f .Set (reflect .Zero (f .Type ()))
257+ }
251258 }
252259 }
253260 }
Original file line number Diff line number Diff line change @@ -876,15 +876,27 @@ func Test_removeEmptyStructs(t *testing.T) {
876876 type t1 struct {
877877 Field1 * t2
878878 }
879- t .Run ("remove_empty_struct" , func (t * testing.T ) {
880879
880+ type t4 struct {
881+ Field4 t1
882+ }
883+
884+ t .Run ("remove_empty_struct_all_pointers" , func (t * testing.T ) {
881885 v := t1 {Field1 : & t2 {Field2 : & t3 {}}}
882886
883887 err := removeEmptyStructs (& v )
884888 require .NoError (t , err )
885889 assert .Equal (t , t1 {}, v )
886890 })
887891
892+ t .Run ("remove_empty_struct_non_pointer_level" , func (t * testing.T ) {
893+ v := t4 {Field4 : t1 {Field1 : & t2 {Field2 : & t3 {}}}}
894+
895+ err := removeEmptyStructs (& v )
896+ require .NoError (t , err )
897+ assert .Equal (t , t4 {}, v )
898+ })
899+
888900 t .Run ("dont_remove_nonempty_struct" , func (t * testing.T ) {
889901 v := t1 {Field1 : & t2 {Field2 : & t3 {Field3 : "hello" }}}
890902
Original file line number Diff line number Diff line change 1+ github.com/davecgh/go-spew v1.1.0 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
2+ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c =
3+ github.com/davecgh/go-spew v1.1.1 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
4+ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
5+ github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
6+ github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
7+ github.com/stretchr/testify v1.6.1 /go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg =
8+ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY =
9+ github.com/stretchr/testify v1.7.0 /go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg =
10+ github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk =
11+ github.com/tj/assert v0.0.3 /go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk =
12+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM =
13+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 /go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0 =
14+ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c /go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM =
15+ gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo =
16+ gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c /go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM =
You can’t perform that action at this time.
0 commit comments