Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary initialization of struct pointers fix #201

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,42 @@ func Process(prefix string, spec interface{}) error {
}
}

return err
return removeEmptyStructs(spec)
}

func removeEmptyStructs(spec interface{}) error {
s := reflect.ValueOf(spec)

if s.Kind() != reflect.Ptr {
return ErrInvalidSpecification
}
s = s.Elem()
if s.Kind() != reflect.Struct {
return ErrInvalidSpecification
}

for i := 0; i < s.NumField(); i++ {
f := s.Field(i)

switch f.Kind() {
case reflect.Struct:
if err := removeEmptyStructs(f.Addr().Interface()); err != nil {
return err
}
case reflect.Ptr:
if f.Elem().Kind() == reflect.Struct && !f.IsNil() {
if err := removeEmptyStructs(f.Elem().Addr().Interface()); err != nil {
return err
}

if f.Elem().IsZero() {
f.Set(reflect.Zero(f.Type()))
}
}
}
}

return nil
}

// MustProcess is the same as Process but panics if an error occurs
Expand Down
45 changes: 44 additions & 1 deletion envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type HonorDecodeInStruct struct {
Expand Down Expand Up @@ -794,7 +797,7 @@ func TestCheckDisallowedIgnored(t *testing.T) {

func TestErrorMessageForRequiredAltVar(t *testing.T) {
var s struct {
Foo string `envconfig:"BAR" required:"true"`
Foo string `envconfig:"BAR" required:"true"`
}

os.Clearenv()
Expand Down Expand Up @@ -862,3 +865,43 @@ func BenchmarkGatherInfo(b *testing.B) {
gatherInfo("env_config", &s)
}
}

func Test_removeEmptyStructs(t *testing.T) {
type t3 struct {
Field3 string
}
type t2 struct {
Field2 *t3
}
type t1 struct {
Field1 *t2
}

type t4 struct {
Field4 t1
}

t.Run("remove_empty_struct_all_pointers", func(t *testing.T) {
v := t1{Field1: &t2{Field2: &t3{}}}

err := removeEmptyStructs(&v)
require.NoError(t, err)
assert.Equal(t, t1{}, v)
})

t.Run("remove_empty_struct_non_pointer_level", func(t *testing.T) {
v := t4{Field4: t1{Field1: &t2{Field2: &t3{}}}}

err := removeEmptyStructs(&v)
require.NoError(t, err)
assert.Equal(t, t4{}, v)
})

t.Run("dont_remove_nonempty_struct", func(t *testing.T) {
v := t1{Field1: &t2{Field2: &t3{Field3: "hello"}}}

err := removeEmptyStructs(&v)
require.NoError(t, err)
assert.Equal(t, t1{Field1: &t2{Field2: &t3{Field3: "hello"}}}, v)
})
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
module github.com/kelseyhightower/envconfig

go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c // indirect
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=