Skip to content

fix: apply FuncMap parsers to pointer-to-custom-type fields - #426

Open
Solaris-star wants to merge 1 commit into
caarlos0:mainfrom
Solaris-star:fix/385-custom-parser-ptr-to-struct
Open

fix: apply FuncMap parsers to pointer-to-custom-type fields#426
Solaris-star wants to merge 1 commit into
caarlos0:mainfrom
Solaris-star:fix/385-custom-parser-ptr-to-struct

Conversation

@Solaris-star

Copy link
Copy Markdown

Summary

When a field is a pointer to a custom struct type and has its own env tag, doParseField always recursed into nested fields and never applied FuncMap parsers / setField for that pointer field.

That made this fail:

type foo struct{ name string }
type config struct {
  Foo *foo `env:"BLAH_CUSTOM"`
}
ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
  reflect.TypeOf(foo{}): func(v string) (any, error) { return foo{name: v}, nil },
}})

Foo kept its pre-parse value instead of being rebuilt from the env string.

Fix

  1. In doParseField, only recurse into *Struct when the field has no own env key (and field-name defaulting is off). Own keys fall through to setField.
  2. In set, allocate a nil pointer target before writing a custom-parser value (so NilFoo *foo works too).

Test plan

  • TestCustomParserPointerToCustomType (includes nested Other *bar + nil pointer target)
  • full go test . (with ambient PORT unset)

Fixes #385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not support pointer to user type

1 participant