Skip to content

Commit

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

* Rename DefaultTagName to DefaultValueTagName
  • Loading branch information
dnovikoff authored Aug 24, 2024
1 parent 3afa723 commit 17fdb91
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
19 changes: 14 additions & 5 deletions 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

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

// RequiredIfNoDef automatically sets all fields as required if they do not
// declare 'envDefault'.
RequiredIfNoDef bool
Expand Down Expand Up @@ -169,10 +172,11 @@ func (opts *Options) getRawEnv(s string) string {

func defaultOptions() Options {
return Options{
TagName: "env",
Environment: toMap(os.Environ()),
FuncMap: defaultTypeParsers(),
rawEnvVars: make(map[string]string),
TagName: "env",
DefaultValueTagName: "envDefault",
Environment: toMap(os.Environ()),
FuncMap: defaultTypeParsers(),
rawEnvVars: make(map[string]string),
}
}

Expand All @@ -181,6 +185,9 @@ func customOptions(opt Options) Options {
if opt.TagName == "" {
opt.TagName = defOpts.TagName
}
if opt.DefaultValueTagName == "" {
opt.DefaultValueTagName = defOpts.DefaultValueTagName
}
if opt.Environment == nil {
opt.Environment = defOpts.Environment
}
Expand All @@ -202,6 +209,7 @@ func optionsWithSliceEnvPrefix(opts Options, index int) Options {
return Options{
Environment: opts.Environment,
TagName: opts.TagName,
DefaultValueTagName: opts.DefaultValueTagName,
RequiredIfNoDef: opts.RequiredIfNoDef,
OnSet: opts.OnSet,
Prefix: fmt.Sprintf("%s%d_", opts.Prefix, index),
Expand All @@ -215,6 +223,7 @@ func optionsWithEnvPrefix(field reflect.StructField, opts Options) Options {
return Options{
Environment: opts.Environment,
TagName: opts.TagName,
DefaultValueTagName: opts.DefaultValueTagName,
RequiredIfNoDef: opts.RequiredIfNoDef,
OnSet: opts.OnSet,
Prefix: opts.Prefix + field.Tag.Get("envPrefix"),
Expand Down Expand Up @@ -513,7 +522,7 @@ func parseFieldParams(field reflect.StructField, opts Options) (FieldParams, err
ownKey = toEnvName(field.Name)
}

defaultValue, hasDefaultValue := field.Tag.Lookup("envDefault")
defaultValue, hasDefaultValue := field.Tag.Lookup(opts.DefaultValueTagName)

result := FieldParams{
OwnKey: ownKey,
Expand Down
13 changes: 13 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2177,3 +2177,16 @@ func TestIssue320(t *testing.T) {
isEqual(t, cfg.Bar, nil)
isEqual(t, cfg.Baz, nil)
}

func TestParseWithOptionsRenamedDefault(t *testing.T) {
type config struct {
Str string `env:"STR" envDefault:"foo" myDefault:"bar"`
}

cfg := &config{}
isNoErr(t, ParseWithOptions(cfg, Options{DefaultValueTagName: "myDefault"}))
isEqual(t, "bar", cfg.Str)

isNoErr(t, Parse(cfg))
isEqual(t, "foo", cfg.Str)
}

0 comments on commit 17fdb91

Please sign in to comment.