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

Support custom tag name for envPrefix #332

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
11 changes: 10 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ type Options struct {
// TagName specifies another tag name to use rather than the default 'env'.
TagName string

// PrefixTagName specifies another prefix tag name to use rather than the default 'envPrefix'.
PrefixTagName string

// DefaultValueTagName specifies another default tag name to use rather than the default 'envDefault'.
DefaultValueTagName string

Expand Down Expand Up @@ -173,6 +176,7 @@ func (opts *Options) getRawEnv(s string) string {
func defaultOptions() Options {
return Options{
TagName: "env",
PrefixTagName: "envPrefix",
DefaultValueTagName: "envDefault",
Environment: toMap(os.Environ()),
FuncMap: defaultTypeParsers(),
Expand All @@ -185,6 +189,9 @@ func customOptions(opt Options) Options {
if opt.TagName == "" {
opt.TagName = defOpts.TagName
}
if opt.PrefixTagName == "" {
opt.PrefixTagName = defOpts.PrefixTagName
}
if opt.DefaultValueTagName == "" {
opt.DefaultValueTagName = defOpts.DefaultValueTagName
}
Expand All @@ -209,6 +216,7 @@ func optionsWithSliceEnvPrefix(opts Options, index int) Options {
return Options{
Environment: opts.Environment,
TagName: opts.TagName,
PrefixTagName: opts.PrefixTagName,
DefaultValueTagName: opts.DefaultValueTagName,
RequiredIfNoDef: opts.RequiredIfNoDef,
OnSet: opts.OnSet,
Expand All @@ -223,10 +231,11 @@ func optionsWithEnvPrefix(field reflect.StructField, opts Options) Options {
return Options{
Environment: opts.Environment,
TagName: opts.TagName,
PrefixTagName: opts.PrefixTagName,
DefaultValueTagName: opts.DefaultValueTagName,
RequiredIfNoDef: opts.RequiredIfNoDef,
OnSet: opts.OnSet,
Prefix: opts.Prefix + field.Tag.Get("envPrefix"),
Prefix: opts.Prefix + field.Tag.Get(opts.PrefixTagName),
UseFieldNameByDefault: opts.UseFieldNameByDefault,
FuncMap: opts.FuncMap,
rawEnvVars: opts.rawEnvVars,
Expand Down
23 changes: 23 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2190,3 +2190,26 @@ func TestParseWithOptionsRenamedDefault(t *testing.T) {
isNoErr(t, Parse(cfg))
isEqual(t, "foo", cfg.Str)
}

func TestParseWithOptionsRenamedPrefix(t *testing.T) {
type Config struct {
Str string `env:"STR"`
}
type ComplexConfig struct {
Foo Config `envPrefix:"FOO_" myPrefix:"BAR_"`
}

t.Setenv("FOO_STR", "101")
t.Setenv("BAR_STR", "202")
t.Setenv("APP_BAR_STR", "303")

cfg := &ComplexConfig{}
isNoErr(t, ParseWithOptions(cfg, Options{PrefixTagName: "myPrefix"}))
isEqual(t, "202", cfg.Foo.Str)

isNoErr(t, ParseWithOptions(cfg, Options{PrefixTagName: "myPrefix", Prefix: "APP_"}))
isEqual(t, "303", cfg.Foo.Str)

isNoErr(t, Parse(cfg))
isEqual(t, "101", cfg.Foo.Str)
}
Loading