Skip to content

Commit

Permalink
fix: flag and pflag always add the default value even the key exists (#…
Browse files Browse the repository at this point in the history
…228)

since konf.Exists uses empty delimiter for empty Config
  • Loading branch information
ktong authored Mar 10, 2024
1 parent 2da490f commit 64cd693
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.9.1] - 2024--3-10

### Fixed

- flag and pflag always add the default value even the key already exists
since konf.Exists uses empty delimiter for empty Config (#228).

## [0.9.0] - 2024-03-10

### Added
Expand Down
21 changes: 10 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,19 @@ func (c *Config) log(ctx context.Context, level slog.Level, message string, attr
}

func (c *Config) sub(values map[string]any, path string) any {
delimiter := c.delimiter
if delimiter == "" {
delimiter = "."
}
if !c.caseSensitive {
path = toLower(path)
}

return maps.Sub(values, strings.Split(path, delimiter))
return maps.Sub(values, strings.Split(path, c.delim()))
}

func (c *Config) delim() string {
if c.delimiter == "" {
return "."
}

return c.delimiter
}

func (c *Config) transformKeys(m map[string]any) map[string]any {
Expand Down Expand Up @@ -185,14 +189,9 @@ func (c *Config) Explain(path string) string {
}

func (c *Config) explain(explanation *strings.Builder, path string, value any) {
delimiter := c.delimiter
if delimiter == "" {
delimiter = "."
}

if values, ok := value.(map[string]any); ok {
for k, v := range values {
c.explain(explanation, path+delimiter+k, v)
c.explain(explanation, path+c.delim()+k, v)
}

return
Expand Down
2 changes: 1 addition & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ func (c *Config) Exists(path []string) bool {

c.nocopy.Check()

return c.sub(c.values, strings.Join(path, c.delimiter)) != nil
return c.sub(c.values, strings.Join(path, c.delim())) != nil
}
9 changes: 7 additions & 2 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import (

func TestConfig_Exists(t *testing.T) {
var config konf.Config
assert.NoError(t, config.Load(mapLoader{"config": "string"}))
assert.True(t, config.Exists([]string{"config"}))
assert.NoError(t, config.Load(mapLoader{"config": map[string]any{"a": "string"}}))
assert.True(t, config.Exists([]string{"config", "a"}))
assert.True(t, !config.Exists([]string{"other"}))

config = *konf.New(konf.WithDelimiter("/"))
assert.NoError(t, config.Load(mapLoader{"config": map[string]any{"a": "string"}}))
assert.True(t, config.Exists([]string{"config", "a"}))
assert.True(t, !config.Exists([]string{"other"}))
}

0 comments on commit 64cd693

Please sign in to comment.