Skip to content
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
7 changes: 6 additions & 1 deletion envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@ func gatherInfo(prefix string, spec interface{}, env map[string]string, isInside
}
}

f.Set(reflect.MakeSlice(f.Type(), l, l))
if l != 0 {
f.Set(reflect.MakeSlice(f.Type(), l, l))
} else {
l = f.Len()
}
Comment on lines +187 to +191
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should extend the existing slice if l > f.Len(), wdyt? We could append some empty structs to it and process all env vars.


for i := 0; i < l; i++ {
var structPtrValue reflect.Value

Expand Down
58 changes: 58 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,66 @@ import (
"strings"
"testing"
"time"

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

type (
Config struct {
Generators []struct {
Input Input
}
}

Input struct {
Type string `envconfig:"TEST"`
}
)

func TestStilLoadsSecrets(t *testing.T) {
os.Clearenv()
os.Setenv("WORKER_GENERATORS_0_INPUT_TEST", "test")

config := Config{
Generators: []struct {
Input Input
}{
{
Input: Input{
Type: "foo",
},
},
{
Input: Input{
Type: "haha",
},
},
Comment on lines +46 to +50
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removed now, right?

},
}

require.NoError(t, Process("WORKER", &config))
require.Len(t, config.Generators, 1)
require.Equal(t, "test", config.Generators[0].Input.Type)
}

func TestNotNukesStructs(t *testing.T) {
config := Config{
Generators: []struct {
Input Input
}{
{
Input: Input{
Type: "foo",
},
},
},
}

require.NoError(t, Process("WORKER", &config))
require.Len(t, config.Generators, 1)
require.Equal(t, "foo", config.Generators[0].Input.Type)
}

type HonorDecodeInStruct struct {
Value string
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/colega/envconfig

go 1.14

require github.com/stretchr/testify v1.8.1 // indirect
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=