Skip to content

Commit

Permalink
feat: set custom tag name for envPrefix (#332)
Browse files Browse the repository at this point in the history
* Support other names for envPrefix

Co-authored-by: Sergey Kozlov <[email protected]>
  • Loading branch information
sv-kozlov and Sergey Kozlov authored Oct 8, 2024
1 parent 76faca5 commit 1cb1967
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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 @@ -178,6 +181,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 @@ -190,6 +194,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 @@ -214,6 +221,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 @@ -228,10 +236,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 @@ -2191,3 +2191,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)
}

0 comments on commit 1cb1967

Please sign in to comment.