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 env.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,12 @@ func doParseField(
return nil
}
if refField.Kind() == reflect.Ptr && refField.Elem().Kind() == reflect.Struct && !refField.IsNil() {
return parseInternal(refField.Interface(), processField, optionsWithEnvPrefix(refTypeField, opts))
// If the field has its own env tag, don't recurse into the struct.
// Let it fall through to setField, which handles custom parsers.
ownKey, _ := parseKeyForOption(refTypeField.Tag.Get(opts.TagName))
if ownKey == "" && !opts.UseFieldNameByDefault {
return parseInternal(refField.Interface(), processField, optionsWithEnvPrefix(refTypeField, opts))
}
}
if refField.Kind() == reflect.Struct && refField.CanAddr() && refField.Type().Name() == "" {
return parseInternal(refField.Addr().Interface(), processField, optionsWithEnvPrefix(refTypeField, opts))
Expand Down
31 changes: 31 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2417,3 +2417,34 @@ func TestEnvBleed(t *testing.T) {
isEqual(t, "", cfg.Foo)
})
}

func TestCustomParserPointerToCustomType(t *testing.T) {
// Regression test for #385
type foo struct {
name string
}

t.Setenv("VAR_CUSTOM", "test")
t.Setenv("BLAH_CUSTOM", "test3")

type config struct {
Var foo `env:"VAR_CUSTOM"`
Foo *foo `env:"BLAH_CUSTOM"`
}

cfg := &config{
Var: foo{"var"},
Foo: &foo{"foo"},
}

err := ParseWithOptions(cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
reflect.TypeOf(foo{}): func(v string) (interface{}, error) {
return foo{name: v}, nil
},
}})

isNoErr(t, err)
isEqual(t, "test", cfg.Var.name)
isTrue(t, cfg.Foo != nil)
isEqual(t, "test3", cfg.Foo.name)
}