Skip to content

Commit

Permalink
Rename new option
Browse files Browse the repository at this point in the history
  • Loading branch information
Veniamin Albaev committed Oct 24, 2021
1 parent c4030ea commit d79f180
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
54 changes: 27 additions & 27 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
type context struct {
name string
prefix string
prefixTag bool
prefixCustomName bool
customName string
defaultVal string
usingDefault bool
Expand All @@ -46,8 +46,8 @@ type Options struct {
// Prefix allows specifying a prefix for each key.
Prefix string

// PrefixTag enables prefixes for the custom names set via struct tags
PrefixTag bool
// PrefixCustomName enables prefixes for the custom names set via struct tags
PrefixCustomName bool

// AllOptional determines whether to not throw errors by default for any key
// that is not found. AllOptional=true means errors will not be thrown.
Expand Down Expand Up @@ -101,12 +101,12 @@ func InitWithOptions(conf interface{}, opts Options) error {
elem := value.Elem()

ctx := context{
name: opts.Prefix,
prefix: opts.Prefix,
prefixTag: opts.PrefixTag,
optional: opts.AllOptional,
leaveNil: opts.LeaveNil,
allowUnexported: opts.AllowUnexported,
name: opts.Prefix,
prefix: opts.Prefix,
prefixCustomName: opts.PrefixCustomName,
optional: opts.AllOptional,
leaveNil: opts.LeaveNil,
allowUnexported: opts.AllowUnexported,
}
switch elem.Kind() {
case reflect.Ptr:
Expand Down Expand Up @@ -182,28 +182,28 @@ func readStruct(value reflect.Value, ctx *context) (nonNil bool, err error) {
case field.Kind() == reflect.Struct && !isUnmarshaler(fieldType):
var nonNilIn bool
nonNilIn, err = readStruct(field, &context{
name: combineName(ctx.name, name),
prefix: ctx.prefix,
prefixTag: ctx.prefixTag,
optional: ctx.optional || tag.optional,
defaultVal: tag.defaultVal,
parents: parents,
leaveNil: ctx.leaveNil,
allowUnexported: ctx.allowUnexported,
name: combineName(ctx.name, name),
prefix: ctx.prefix,
prefixCustomName: ctx.prefixCustomName,
optional: ctx.optional || tag.optional,
defaultVal: tag.defaultVal,
parents: parents,
leaveNil: ctx.leaveNil,
allowUnexported: ctx.allowUnexported,
})
nonNil = nonNil || nonNilIn
default:
var ok bool
ok, err = setField(field, &context{
name: combineName(ctx.name, name),
prefix: ctx.prefix,
prefixTag: ctx.prefixTag,
customName: tag.customName,
optional: ctx.optional || tag.optional,
defaultVal: tag.defaultVal,
parents: parents,
leaveNil: ctx.leaveNil,
allowUnexported: ctx.allowUnexported,
name: combineName(ctx.name, name),
prefix: ctx.prefix,
prefixCustomName: ctx.prefixCustomName,
customName: tag.customName,
optional: ctx.optional || tag.optional,
defaultVal: tag.defaultVal,
parents: parents,
leaveNil: ctx.leaveNil,
allowUnexported: ctx.allowUnexported,
})
nonNil = nonNil || ok
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func readValue(ctx *context) (string, error) {

func makeAllPossibleKeys(ctx *context) (res []string) {
if ctx.customName != "" {
if ctx.prefixTag {
if ctx.prefixCustomName {
return []string{ctx.prefix + "_" + ctx.customName}
}
return []string{ctx.customName}
Expand Down
6 changes: 3 additions & 3 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func TestInitWithPrefix(t *testing.T) {
TestEnvNodeBar1: "D1",
},
}

require.NoError(t, os.Setenv("TEST_ENV_FOO1", expect.TestEnvFoo1))
require.NoError(t, os.Setenv("TESTPREFIX1_TEST_ENV_BAR1", expect.TestEnvBar1))
require.NoError(t, os.Setenv("TEST_ENV_NODE_FOO1", expect.Node1.TestEnvNodeFoo1))
Expand Down Expand Up @@ -693,8 +693,8 @@ func TestInitWithPrefix(t *testing.T) {

var conf Root2
err := envconfig.InitWithOptions(&conf, envconfig.Options{
Prefix: "TESTPREFIX2",
PrefixTag: true,
Prefix: "TESTPREFIX2",
PrefixCustomName: true,
})

require.NoError(t, err)
Expand Down

0 comments on commit d79f180

Please sign in to comment.