Skip to content

Commit

Permalink
fix: prefixes on pointers (#203)
Browse files Browse the repository at this point in the history
closes #202

Signed-off-by: Carlos A Becker <[email protected]>
  • Loading branch information
caarlos0 authored Nov 30, 2021
1 parent 2cf397a commit d8dc27c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
28 changes: 18 additions & 10 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,19 @@ func doParse(ref reflect.Value, funcMap map[reflect.Type]ParserFunc, opts []Opti
continue
}
if reflect.Ptr == refField.Kind() && !refField.IsNil() {
err := ParseWithFuncs(refField.Interface(), funcMap, opts...)
if err != nil {
if refField.Elem().Kind() == reflect.Struct {
if err := ParseWithFuncs(refField.Interface(), funcMap, optsWithPrefix(refType.Field(i), opts)...); err != nil {
return err
}
continue
}
if err := ParseWithFuncs(refField.Interface(), funcMap, opts...); err != nil {
return err
}
continue
}
if reflect.Struct == refField.Kind() && refField.CanAddr() && refField.Type().Name() == "" {
err := Parse(refField.Addr().Interface(), opts...)
if err != nil {
if err := Parse(refField.Addr().Interface(), opts...); err != nil {
return err
}
continue
Expand All @@ -224,12 +228,7 @@ func doParse(ref reflect.Value, funcMap map[reflect.Type]ParserFunc, opts []Opti
}
if value == "" {
if reflect.Struct == refField.Kind() {
subOpts := make([]Options, len(opts))
copy(subOpts, opts)
if prefix := refType.Field(i).Tag.Get("envPrefix"); prefix != "" {
subOpts[0].Prefix += prefix
}
if err := doParse(refField, funcMap, subOpts); err != nil {
if err := doParse(refField, funcMap, optsWithPrefix(refType.Field(i), opts)); err != nil {
return err
}
}
Expand Down Expand Up @@ -475,3 +474,12 @@ func (e parseError) Error() string {
func newNoParserError(sf reflect.StructField) error {
return fmt.Errorf(`env: no parser found for field "%s" of type "%s"`, sf.Name, sf.Type)
}

func optsWithPrefix(field reflect.StructField, opts []Options) []Options {
subOpts := make([]Options, len(opts))
copy(subOpts, opts)
if prefix := field.Tag.Get("envPrefix"); prefix != "" {
subOpts[0].Prefix += prefix
}
return subOpts
}
30 changes: 25 additions & 5 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,13 +1433,34 @@ func TestPrefix(t *testing.T) {
Clean Config
}
cfg := ComplexConfig{}
err := Parse(&cfg, Options{Environment: map[string]string{"FOO_HOME": "/foo", "BAR_HOME": "/bar", "HOME": "/clean"}})
is.NoErr(err)
is.NoErr(Parse(&cfg, Options{Environment: map[string]string{"FOO_HOME": "/foo", "BAR_HOME": "/bar", "HOME": "/clean"}}))
is.Equal("/foo", cfg.Foo.Home)
is.Equal("/bar", cfg.Bar.Home)
is.Equal("/clean", cfg.Clean.Home)
}

func TestPrefixPointers(t *testing.T) {
is := is.New(t)
type Test struct {
Str string `env:"TEST"`
}
type ComplexConfig struct {
Foo *Test `envPrefix:"FOO_"`
Bar *Test `envPrefix:"BAR_"`
Clean *Test
}

cfg := ComplexConfig{
Foo: &Test{},
Bar: &Test{},
Clean: &Test{},
}
is.NoErr(Parse(&cfg, Options{Environment: map[string]string{"FOO_TEST": "kek", "BAR_TEST": "lel", "TEST": "clean"}}))
is.Equal("kek", cfg.Foo.Str)
is.Equal("lel", cfg.Bar.Str)
is.Equal("clean", cfg.Clean.Str)
}

func TestComplePrefix(t *testing.T) {
is := is.New(t)
type Config struct {
Expand All @@ -1452,16 +1473,15 @@ func TestComplePrefix(t *testing.T) {
Blah string `env:"BLAH"`
}
cfg := ComplexConfig{}
err := Parse(&cfg, Options{
is.NoErr(Parse(&cfg, Options{
Prefix: "T_",
Environment: map[string]string{
"T_FOO_HOME": "/foo",
"T_BAR_HOME": "/bar",
"T_BLAH": "blahhh",
"T_HOME": "/clean",
},
})
is.NoErr(err)
}))
is.Equal("/foo", cfg.Foo.Home)
is.Equal("/bar", cfg.Bar.Home)
is.Equal("/clean", cfg.Clean.Home)
Expand Down

0 comments on commit d8dc27c

Please sign in to comment.