Skip to content

Commit

Permalink
ignore value if name splitter return empty (#111)
Browse files Browse the repository at this point in the history
Follow up #110
  • Loading branch information
ktong authored Feb 10, 2024
1 parent b366fb3 commit ce69ab9
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion provider/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func (e Env) Load() (map[string]any, error) {
// The environment variable with empty value is treated as unset.
continue
}
maps.Insert(values, e.splitter(key), value)

if keys := e.splitter(key); len(keys) > 1 || len(keys) == 1 && keys[0] != "" {
maps.Insert(values, keys, value)
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions provider/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ func TestEnv_Load(t *testing.T) {
},
},
},
{
description: "with nil splitter",
opts: []env.Option{
env.WithPrefix("P."),
env.WithNameSplitter(func(string) []string { return nil }),
},
expected: map[string]any{},
},
{
description: "with empty splitter",
opts: []env.Option{
env.WithPrefix("P."),
env.WithNameSplitter(func(string) []string { return []string{""} }),
},
expected: map[string]any{},
},
}

t.Setenv("P_K", "v")
Expand Down
1 change: 1 addition & 0 deletions provider/env/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func WithDelimiter(delimiter string) Option {
}

// WithNameSplitter provides the function used to split environment variable names into nested keys.
// If it returns an nil/[]string{}/[]string{""}, the variable will be ignored.
//
// For example, with the default splitter, an environment variable name like "PARENT_CHILD_KEY"
// would be split into "PARENT", "CHILD", and "KEY".
Expand Down
4 changes: 4 additions & 0 deletions provider/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func (f Flag) Load() (map[string]any, error) {
}

keys := f.splitter(flag.Name)
if len(keys) == 0 || len(keys) == 1 && keys[0] == "" {
return
}

val := flag.Value.String()
// Skip zero default value to avoid overriding values set by other loader.
if val == flag.DefValue && (f.konf.Exists(keys) || isZeroDefValue(flag)) {
Expand Down
16 changes: 16 additions & 0 deletions provider/flag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ func TestFlag_Load(t *testing.T) {
},
},
},
{
description: "with nil splitter",
opts: []kflag.Option{
kflag.WithPrefix("p_"),
kflag.WithNameSplitter(func(string) []string { return nil }),
},
expected: map[string]any{},
},
{
description: "with empty splitter",
opts: []kflag.Option{
kflag.WithPrefix("p_"),
kflag.WithNameSplitter(func(string) []string { return []string{""} }),
},
expected: map[string]any{},
},
{
description: "with prefix",
opts: []kflag.Option{kflag.WithPrefix("p.")},
Expand Down
1 change: 1 addition & 0 deletions provider/flag/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func WithDelimiter(delimiter string) Option {
}

// WithNameSplitter provides the function used to split environment variable names into nested keys.
// If it returns an nil/[]string{}/[]string{""}, the variable will be ignored.
//
// For example, with the default splitter, an flag name like "parent.child.key"
// would be split into "parent", "child", and "key".
Expand Down
1 change: 1 addition & 0 deletions provider/pflag/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func WithDelimiter(delimiter string) Option {
}

// WithNameSplitter provides the function used to split environment variable names into nested keys.
// If it returns an nil/[]string{}/[]string{""}, the variable will be ignored.
//
// For example, with the default splitter, an flag name like "parent.child.key"
// // would be split into "parent", "child", and "key".
Expand Down
4 changes: 4 additions & 0 deletions provider/pflag/pflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (f PFlag) Load() (map[string]any, error) {
}

keys := f.splitter(flag.Name)
if len(keys) == 0 || len(keys) == 1 && keys[0] == "" {
return
}

val := f.flagVal(flag)
// Skip zero default value to avoid overriding values set by other loader.
if !flag.Changed && (f.konf.Exists(keys) || reflect.ValueOf(val).IsZero()) {
Expand Down
16 changes: 16 additions & 0 deletions provider/pflag/pflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ func TestPFlag_Load(t *testing.T) {
},
},
},
{
description: "with nil splitter",
opts: []kflag.Option{
kflag.WithPrefix("p_"),
kflag.WithNameSplitter(func(string) []string { return nil }),
},
expected: map[string]any{},
},
{
description: "with empty splitter",
opts: []kflag.Option{
kflag.WithPrefix("p_"),
kflag.WithNameSplitter(func(string) []string { return []string{""} }),
},
expected: map[string]any{},
},
{
description: "with prefix",
opts: []kflag.Option{kflag.WithPrefix("p.")},
Expand Down

0 comments on commit ce69ab9

Please sign in to comment.