From d79f1808f85d01ec54c5aec9122964ccf83a6d11 Mon Sep 17 00:00:00 2001 From: Veniamin Albaev Date: Sun, 24 Oct 2021 16:11:44 +0300 Subject: [PATCH] Rename new option --- envconfig.go | 54 +++++++++++++++++++++++------------------------ envconfig_test.go | 6 +++--- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/envconfig.go b/envconfig.go index 9eb983c..739a088 100644 --- a/envconfig.go +++ b/envconfig.go @@ -26,7 +26,7 @@ var ( type context struct { name string prefix string - prefixTag bool + prefixCustomName bool customName string defaultVal string usingDefault bool @@ -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. @@ -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: @@ -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 } @@ -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} diff --git a/envconfig_test.go b/envconfig_test.go index 3634264..ee5eaa8 100644 --- a/envconfig_test.go +++ b/envconfig_test.go @@ -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)) @@ -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)